(all insn and peephole patterns): Rewrite without using arm_output_asm_insn.
[official-gcc.git] / gcc / jump.c
blob40258a5c0a2771d2228ea8184e30d0959d227d50
1 /* Optimize jump instructions, for GNU compiler.
2 Copyright (C) 1987, 88, 89, 91, 92, 93, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This is the jump-optimization pass of the compiler.
22 It is run two or three times: once before cse, sometimes once after cse,
23 and once after reload (before final).
25 jump_optimize deletes unreachable code and labels that are not used.
26 It also deletes jumps that jump to the following insn,
27 and simplifies jumps around unconditional jumps and jumps
28 to unconditional jumps.
30 Each CODE_LABEL has a count of the times it is used
31 stored in the LABEL_NUSES internal field, and each JUMP_INSN
32 has one label that it refers to stored in the
33 JUMP_LABEL internal field. With this we can detect labels that
34 become unused because of the deletion of all the jumps that
35 formerly used them. The JUMP_LABEL info is sometimes looked
36 at by later passes.
38 Optionally, cross-jumping can be done. Currently it is done
39 only the last time (when after reload and before final).
40 In fact, the code for cross-jumping now assumes that register
41 allocation has been done, since it uses `rtx_renumbered_equal_p'.
43 Jump optimization is done after cse when cse's constant-propagation
44 causes jumps to become unconditional or to be deleted.
46 Unreachable loops are not detected here, because the labels
47 have references and the insns appear reachable from the labels.
48 find_basic_blocks in flow.c finds and deletes such loops.
50 The subroutines delete_insn, redirect_jump, and invert_jump are used
51 from other passes as well. */
53 #include "config.h"
54 #include "rtl.h"
55 #include "flags.h"
56 #include "hard-reg-set.h"
57 #include "regs.h"
58 #include "expr.h"
59 #include "insn-config.h"
60 #include "insn-flags.h"
61 #include "real.h"
63 /* ??? Eventually must record somehow the labels used by jumps
64 from nested functions. */
65 /* Pre-record the next or previous real insn for each label?
66 No, this pass is very fast anyway. */
67 /* Condense consecutive labels?
68 This would make life analysis faster, maybe. */
69 /* Optimize jump y; x: ... y: jumpif... x?
70 Don't know if it is worth bothering with. */
71 /* Optimize two cases of conditional jump to conditional jump?
72 This can never delete any instruction or make anything dead,
73 or even change what is live at any point.
74 So perhaps let combiner do it. */
76 /* Vector indexed by uid.
77 For each CODE_LABEL, index by its uid to get first unconditional jump
78 that jumps to the label.
79 For each JUMP_INSN, index by its uid to get the next unconditional jump
80 that jumps to the same label.
81 Element 0 is the start of a chain of all return insns.
82 (It is safe to use element 0 because insn uid 0 is not used. */
84 static rtx *jump_chain;
86 /* List of labels referred to from initializers.
87 These can never be deleted. */
88 rtx forced_labels;
90 /* Maximum index in jump_chain. */
92 static int max_jump_chain;
94 /* Set nonzero by jump_optimize if control can fall through
95 to the end of the function. */
96 int can_reach_end;
98 /* Indicates whether death notes are significant in cross jump analysis.
99 Normally they are not significant, because of A and B jump to C,
100 and R dies in A, it must die in B. But this might not be true after
101 stack register conversion, and we must compare death notes in that
102 case. */
104 static int cross_jump_death_matters = 0;
106 static int duplicate_loop_exit_test PROTO((rtx));
107 static void find_cross_jump PROTO((rtx, rtx, int, rtx *, rtx *));
108 static void do_cross_jump PROTO((rtx, rtx, rtx));
109 static int jump_back_p PROTO((rtx, rtx));
110 static int tension_vector_labels PROTO((rtx, int));
111 static void mark_jump_label PROTO((rtx, rtx, int));
112 static void delete_computation PROTO((rtx));
113 static void delete_from_jump_chain PROTO((rtx));
114 static int delete_labelref_insn PROTO((rtx, rtx, int));
115 static void redirect_tablejump PROTO((rtx, rtx));
117 /* Delete no-op jumps and optimize jumps to jumps
118 and jumps around jumps.
119 Delete unused labels and unreachable code.
121 If CROSS_JUMP is 1, detect matching code
122 before a jump and its destination and unify them.
123 If CROSS_JUMP is 2, do cross-jumping, but pay attention to death notes.
125 If NOOP_MOVES is nonzero, delete no-op move insns.
127 If AFTER_REGSCAN is nonzero, then this jump pass is being run immediately
128 after regscan, and it is safe to use regno_first_uid and regno_last_uid.
130 If `optimize' is zero, don't change any code,
131 just determine whether control drops off the end of the function.
132 This case occurs when we have -W and not -O.
133 It works because `delete_insn' checks the value of `optimize'
134 and refrains from actually deleting when that is 0. */
136 void
137 jump_optimize (f, cross_jump, noop_moves, after_regscan)
138 rtx f;
139 int cross_jump;
140 int noop_moves;
141 int after_regscan;
143 register rtx insn, next, note;
144 int changed;
145 int first = 1;
146 int max_uid = 0;
147 rtx last_insn;
149 cross_jump_death_matters = (cross_jump == 2);
151 /* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
152 notes whose labels don't occur in the insn any more. */
154 for (insn = f; insn; insn = NEXT_INSN (insn))
156 if (GET_CODE (insn) == CODE_LABEL)
157 LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
158 else if (GET_CODE (insn) == JUMP_INSN)
159 JUMP_LABEL (insn) = 0;
160 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
161 for (note = REG_NOTES (insn); note; note = next)
163 next = XEXP (note, 1);
164 if (REG_NOTE_KIND (note) == REG_LABEL
165 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
166 remove_note (insn, note);
169 if (INSN_UID (insn) > max_uid)
170 max_uid = INSN_UID (insn);
173 max_uid++;
175 /* Delete insns following barriers, up to next label. */
177 for (insn = f; insn;)
179 if (GET_CODE (insn) == BARRIER)
181 insn = NEXT_INSN (insn);
182 while (insn != 0 && GET_CODE (insn) != CODE_LABEL)
184 if (GET_CODE (insn) == NOTE
185 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
186 insn = NEXT_INSN (insn);
187 else
188 insn = delete_insn (insn);
190 /* INSN is now the code_label. */
192 else
193 insn = NEXT_INSN (insn);
196 /* Leave some extra room for labels and duplicate exit test insns
197 we make. */
198 max_jump_chain = max_uid * 14 / 10;
199 jump_chain = (rtx *) alloca (max_jump_chain * sizeof (rtx));
200 bzero (jump_chain, max_jump_chain * sizeof (rtx));
202 /* Mark the label each jump jumps to.
203 Combine consecutive labels, and count uses of labels.
205 For each label, make a chain (using `jump_chain')
206 of all the *unconditional* jumps that jump to it;
207 also make a chain of all returns. */
209 for (insn = f; insn; insn = NEXT_INSN (insn))
210 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
211 && ! INSN_DELETED_P (insn))
213 mark_jump_label (PATTERN (insn), insn, cross_jump);
214 if (GET_CODE (insn) == JUMP_INSN)
216 if (JUMP_LABEL (insn) != 0 && simplejump_p (insn))
218 jump_chain[INSN_UID (insn)]
219 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
220 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
222 if (GET_CODE (PATTERN (insn)) == RETURN)
224 jump_chain[INSN_UID (insn)] = jump_chain[0];
225 jump_chain[0] = insn;
230 /* Keep track of labels used from static data;
231 they cannot ever be deleted. */
233 for (insn = forced_labels; insn; insn = XEXP (insn, 1))
234 LABEL_NUSES (XEXP (insn, 0))++;
236 /* Delete all labels already not referenced.
237 Also find the last insn. */
239 last_insn = 0;
240 for (insn = f; insn; )
242 if (GET_CODE (insn) == CODE_LABEL && LABEL_NUSES (insn) == 0)
243 insn = delete_insn (insn);
244 else
246 last_insn = insn;
247 insn = NEXT_INSN (insn);
251 if (!optimize)
253 /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
254 If so record that this function can drop off the end. */
256 insn = last_insn;
258 int n_labels = 1;
259 while (insn
260 /* One label can follow the end-note: the return label. */
261 && ((GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
262 /* Ordinary insns can follow it if returning a structure. */
263 || GET_CODE (insn) == INSN
264 /* If machine uses explicit RETURN insns, no epilogue,
265 then one of them follows the note. */
266 || (GET_CODE (insn) == JUMP_INSN
267 && GET_CODE (PATTERN (insn)) == RETURN)
268 /* Other kinds of notes can follow also. */
269 || (GET_CODE (insn) == NOTE
270 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)))
271 insn = PREV_INSN (insn);
274 /* Report if control can fall through at the end of the function. */
275 if (insn && GET_CODE (insn) == NOTE
276 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END
277 && ! INSN_DELETED_P (insn))
278 can_reach_end = 1;
280 /* Zero the "deleted" flag of all the "deleted" insns. */
281 for (insn = f; insn; insn = NEXT_INSN (insn))
282 INSN_DELETED_P (insn) = 0;
283 return;
286 #ifdef HAVE_return
287 if (HAVE_return)
289 /* If we fall through to the epilogue, see if we can insert a RETURN insn
290 in front of it. If the machine allows it at this point (we might be
291 after reload for a leaf routine), it will improve optimization for it
292 to be there. */
293 insn = get_last_insn ();
294 while (insn && GET_CODE (insn) == NOTE)
295 insn = PREV_INSN (insn);
297 if (insn && GET_CODE (insn) != BARRIER)
299 emit_jump_insn (gen_return ());
300 emit_barrier ();
303 #endif
305 if (noop_moves)
306 for (insn = f; insn; )
308 next = NEXT_INSN (insn);
310 if (GET_CODE (insn) == INSN)
312 register rtx body = PATTERN (insn);
314 /* Combine stack_adjusts with following push_insns. */
315 #ifdef PUSH_ROUNDING
316 if (GET_CODE (body) == SET
317 && SET_DEST (body) == stack_pointer_rtx
318 && GET_CODE (SET_SRC (body)) == PLUS
319 && XEXP (SET_SRC (body), 0) == stack_pointer_rtx
320 && GET_CODE (XEXP (SET_SRC (body), 1)) == CONST_INT
321 && INTVAL (XEXP (SET_SRC (body), 1)) > 0)
323 rtx p;
324 rtx stack_adjust_insn = insn;
325 int stack_adjust_amount = INTVAL (XEXP (SET_SRC (body), 1));
326 int total_pushed = 0;
327 int pushes = 0;
329 /* Find all successive push insns. */
330 p = insn;
331 /* Don't convert more than three pushes;
332 that starts adding too many displaced addresses
333 and the whole thing starts becoming a losing
334 proposition. */
335 while (pushes < 3)
337 rtx pbody, dest;
338 p = next_nonnote_insn (p);
339 if (p == 0 || GET_CODE (p) != INSN)
340 break;
341 pbody = PATTERN (p);
342 if (GET_CODE (pbody) != SET)
343 break;
344 dest = SET_DEST (pbody);
345 /* Allow a no-op move between the adjust and the push. */
346 if (GET_CODE (dest) == REG
347 && GET_CODE (SET_SRC (pbody)) == REG
348 && REGNO (dest) == REGNO (SET_SRC (pbody)))
349 continue;
350 if (! (GET_CODE (dest) == MEM
351 && GET_CODE (XEXP (dest, 0)) == POST_INC
352 && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
353 break;
354 pushes++;
355 if (total_pushed + GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)))
356 > stack_adjust_amount)
357 break;
358 total_pushed += GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)));
361 /* Discard the amount pushed from the stack adjust;
362 maybe eliminate it entirely. */
363 if (total_pushed >= stack_adjust_amount)
365 delete_computation (stack_adjust_insn);
366 total_pushed = stack_adjust_amount;
368 else
369 XEXP (SET_SRC (PATTERN (stack_adjust_insn)), 1)
370 = GEN_INT (stack_adjust_amount - total_pushed);
372 /* Change the appropriate push insns to ordinary stores. */
373 p = insn;
374 while (total_pushed > 0)
376 rtx pbody, dest;
377 p = next_nonnote_insn (p);
378 if (GET_CODE (p) != INSN)
379 break;
380 pbody = PATTERN (p);
381 if (GET_CODE (pbody) == SET)
382 break;
383 dest = SET_DEST (pbody);
384 if (! (GET_CODE (dest) == MEM
385 && GET_CODE (XEXP (dest, 0)) == POST_INC
386 && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
387 break;
388 total_pushed -= GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)));
389 /* If this push doesn't fully fit in the space
390 of the stack adjust that we deleted,
391 make another stack adjust here for what we
392 didn't use up. There should be peepholes
393 to recognize the resulting sequence of insns. */
394 if (total_pushed < 0)
396 emit_insn_before (gen_add2_insn (stack_pointer_rtx,
397 GEN_INT (- total_pushed)),
399 break;
401 XEXP (dest, 0)
402 = plus_constant (stack_pointer_rtx, total_pushed);
405 #endif
407 /* Detect and delete no-op move instructions
408 resulting from not allocating a parameter in a register. */
410 if (GET_CODE (body) == SET
411 && (SET_DEST (body) == SET_SRC (body)
412 || (GET_CODE (SET_DEST (body)) == MEM
413 && GET_CODE (SET_SRC (body)) == MEM
414 && rtx_equal_p (SET_SRC (body), SET_DEST (body))))
415 && ! (GET_CODE (SET_DEST (body)) == MEM
416 && MEM_VOLATILE_P (SET_DEST (body)))
417 && ! (GET_CODE (SET_SRC (body)) == MEM
418 && MEM_VOLATILE_P (SET_SRC (body))))
419 delete_computation (insn);
421 /* Detect and ignore no-op move instructions
422 resulting from smart or fortuitous register allocation. */
424 else if (GET_CODE (body) == SET)
426 int sreg = true_regnum (SET_SRC (body));
427 int dreg = true_regnum (SET_DEST (body));
429 if (sreg == dreg && sreg >= 0)
430 delete_insn (insn);
431 else if (sreg >= 0 && dreg >= 0)
433 rtx trial;
434 rtx tem = find_equiv_reg (NULL_RTX, insn, 0,
435 sreg, NULL_PTR, dreg,
436 GET_MODE (SET_SRC (body)));
438 #ifdef PRESERVE_DEATH_INFO_REGNO_P
439 /* Deleting insn could lose a death-note for SREG or DREG
440 so don't do it if final needs accurate death-notes. */
441 if (! PRESERVE_DEATH_INFO_REGNO_P (sreg)
442 && ! PRESERVE_DEATH_INFO_REGNO_P (dreg))
443 #endif
445 /* DREG may have been the target of a REG_DEAD note in
446 the insn which makes INSN redundant. If so, reorg
447 would still think it is dead. So search for such a
448 note and delete it if we find it. */
449 for (trial = prev_nonnote_insn (insn);
450 trial && GET_CODE (trial) != CODE_LABEL;
451 trial = prev_nonnote_insn (trial))
452 if (find_regno_note (trial, REG_DEAD, dreg))
454 remove_death (dreg, trial);
455 break;
458 if (tem != 0
459 && GET_MODE (tem) == GET_MODE (SET_DEST (body)))
460 delete_insn (insn);
463 else if (dreg >= 0 && CONSTANT_P (SET_SRC (body))
464 && find_equiv_reg (SET_SRC (body), insn, 0, dreg,
465 NULL_PTR, 0,
466 GET_MODE (SET_DEST (body))))
468 /* This handles the case where we have two consecutive
469 assignments of the same constant to pseudos that didn't
470 get a hard reg. Each SET from the constant will be
471 converted into a SET of the spill register and an
472 output reload will be made following it. This produces
473 two loads of the same constant into the same spill
474 register. */
476 rtx in_insn = insn;
478 /* Look back for a death note for the first reg.
479 If there is one, it is no longer accurate. */
480 while (in_insn && GET_CODE (in_insn) != CODE_LABEL)
482 if ((GET_CODE (in_insn) == INSN
483 || GET_CODE (in_insn) == JUMP_INSN)
484 && find_regno_note (in_insn, REG_DEAD, dreg))
486 remove_death (dreg, in_insn);
487 break;
489 in_insn = PREV_INSN (in_insn);
492 /* Delete the second load of the value. */
493 delete_insn (insn);
496 else if (GET_CODE (body) == PARALLEL)
498 /* If each part is a set between two identical registers or
499 a USE or CLOBBER, delete the insn. */
500 int i, sreg, dreg;
501 rtx tem;
503 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
505 tem = XVECEXP (body, 0, i);
506 if (GET_CODE (tem) == USE || GET_CODE (tem) == CLOBBER)
507 continue;
509 if (GET_CODE (tem) != SET
510 || (sreg = true_regnum (SET_SRC (tem))) < 0
511 || (dreg = true_regnum (SET_DEST (tem))) < 0
512 || dreg != sreg)
513 break;
516 if (i < 0)
517 delete_insn (insn);
519 #if !BYTES_BIG_ENDIAN /* Not worth the hair to detect this
520 in the big-endian case. */
521 /* Also delete insns to store bit fields if they are no-ops. */
522 else if (GET_CODE (body) == SET
523 && GET_CODE (SET_DEST (body)) == ZERO_EXTRACT
524 && XEXP (SET_DEST (body), 2) == const0_rtx
525 && XEXP (SET_DEST (body), 0) == SET_SRC (body)
526 && ! (GET_CODE (SET_SRC (body)) == MEM
527 && MEM_VOLATILE_P (SET_SRC (body))))
528 delete_insn (insn);
529 #endif /* not BYTES_BIG_ENDIAN */
531 insn = next;
534 /* If we haven't yet gotten to reload and we have just run regscan,
535 delete any insn that sets a register that isn't used elsewhere.
536 This helps some of the optimizations below by having less insns
537 being jumped around. */
539 if (! reload_completed && after_regscan)
540 for (insn = f; insn; insn = next)
542 rtx set = single_set (insn);
544 next = NEXT_INSN (insn);
546 if (set && GET_CODE (SET_DEST (set)) == REG
547 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
548 && regno_first_uid[REGNO (SET_DEST (set))] == INSN_UID (insn)
549 /* We use regno_last_note_uid so as not to delete the setting
550 of a reg that's used in notes. A subsequent optimization
551 might arrange to use that reg for real. */
552 && regno_last_note_uid[REGNO (SET_DEST (set))] == INSN_UID (insn)
553 && ! side_effects_p (SET_SRC (set)))
554 delete_insn (insn);
557 /* Now iterate optimizing jumps until nothing changes over one pass. */
558 changed = 1;
559 while (changed)
561 changed = 0;
563 for (insn = f; insn; insn = next)
565 rtx reallabelprev;
566 rtx temp, temp1, temp2, temp3, temp4, temp5, temp6;
567 rtx nlabel;
568 int this_is_simplejump, this_is_condjump, reversep;
569 #if 0
570 /* If NOT the first iteration, if this is the last jump pass
571 (just before final), do the special peephole optimizations.
572 Avoiding the first iteration gives ordinary jump opts
573 a chance to work before peephole opts. */
575 if (reload_completed && !first && !flag_no_peephole)
576 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
577 peephole (insn);
578 #endif
580 /* That could have deleted some insns after INSN, so check now
581 what the following insn is. */
583 next = NEXT_INSN (insn);
585 /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
586 jump. Try to optimize by duplicating the loop exit test if so.
587 This is only safe immediately after regscan, because it uses
588 the values of regno_first_uid and regno_last_uid. */
589 if (after_regscan && GET_CODE (insn) == NOTE
590 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
591 && (temp1 = next_nonnote_insn (insn)) != 0
592 && simplejump_p (temp1))
594 temp = PREV_INSN (insn);
595 if (duplicate_loop_exit_test (insn))
597 changed = 1;
598 next = NEXT_INSN (temp);
599 continue;
603 if (GET_CODE (insn) != JUMP_INSN)
604 continue;
606 this_is_simplejump = simplejump_p (insn);
607 this_is_condjump = condjump_p (insn);
609 /* Tension the labels in dispatch tables. */
611 if (GET_CODE (PATTERN (insn)) == ADDR_VEC)
612 changed |= tension_vector_labels (PATTERN (insn), 0);
613 if (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
614 changed |= tension_vector_labels (PATTERN (insn), 1);
616 /* If a dispatch table always goes to the same place,
617 get rid of it and replace the insn that uses it. */
619 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
620 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
622 int i;
623 rtx pat = PATTERN (insn);
624 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
625 int len = XVECLEN (pat, diff_vec_p);
626 rtx dispatch = prev_real_insn (insn);
628 for (i = 0; i < len; i++)
629 if (XEXP (XVECEXP (pat, diff_vec_p, i), 0)
630 != XEXP (XVECEXP (pat, diff_vec_p, 0), 0))
631 break;
632 if (i == len
633 && dispatch != 0
634 && GET_CODE (dispatch) == JUMP_INSN
635 && JUMP_LABEL (dispatch) != 0
636 /* Don't mess with a casesi insn. */
637 && !(GET_CODE (PATTERN (dispatch)) == SET
638 && (GET_CODE (SET_SRC (PATTERN (dispatch)))
639 == IF_THEN_ELSE))
640 && next_real_insn (JUMP_LABEL (dispatch)) == insn)
642 redirect_tablejump (dispatch,
643 XEXP (XVECEXP (pat, diff_vec_p, 0), 0));
644 changed = 1;
648 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
650 /* If a jump references the end of the function, try to turn
651 it into a RETURN insn, possibly a conditional one. */
652 if (JUMP_LABEL (insn)
653 && (next_active_insn (JUMP_LABEL (insn)) == 0
654 || GET_CODE (PATTERN (next_active_insn (JUMP_LABEL (insn))))
655 == RETURN))
656 changed |= redirect_jump (insn, NULL_RTX);
658 /* Detect jump to following insn. */
659 if (reallabelprev == insn && condjump_p (insn))
661 delete_jump (insn);
662 changed = 1;
663 continue;
666 /* If we have an unconditional jump preceded by a USE, try to put
667 the USE before the target and jump there. This simplifies many
668 of the optimizations below since we don't have to worry about
669 dealing with these USE insns. We only do this if the label
670 being branch to already has the identical USE or if code
671 never falls through to that label. */
673 if (this_is_simplejump
674 && (temp = prev_nonnote_insn (insn)) != 0
675 && GET_CODE (temp) == INSN && GET_CODE (PATTERN (temp)) == USE
676 && (temp1 = prev_nonnote_insn (JUMP_LABEL (insn))) != 0
677 && (GET_CODE (temp1) == BARRIER
678 || (GET_CODE (temp1) == INSN
679 && rtx_equal_p (PATTERN (temp), PATTERN (temp1)))))
681 if (GET_CODE (temp1) == BARRIER)
683 emit_insn_after (PATTERN (temp), temp1);
684 temp1 = NEXT_INSN (temp1);
687 delete_insn (temp);
688 redirect_jump (insn, get_label_before (temp1));
689 reallabelprev = prev_real_insn (temp1);
690 changed = 1;
693 /* Simplify if (...) x = a; else x = b; by converting it
694 to x = b; if (...) x = a;
695 if B is sufficiently simple, the test doesn't involve X,
696 and nothing in the test modifies B or X.
698 If we have small register classes, we also can't do this if X
699 is a hard register.
701 If the "x = b;" insn has any REG_NOTES, we don't do this because
702 of the possibility that we are running after CSE and there is a
703 REG_EQUAL note that is only valid if the branch has already been
704 taken. If we move the insn with the REG_EQUAL note, we may
705 fold the comparison to always be false in a later CSE pass.
706 (We could also delete the REG_NOTES when moving the insn, but it
707 seems simpler to not move it.) An exception is that we can move
708 the insn if the only note is a REG_EQUAL or REG_EQUIV whose
709 value is the same as "b".
711 INSN is the branch over the `else' part.
713 We set:
715 TEMP to the jump insn preceding "x = a;"
716 TEMP1 to X
717 TEMP2 to the insn that sets "x = b;"
718 TEMP3 to the insn that sets "x = a;"
719 TEMP4 to the set of "x = b"; */
721 if (this_is_simplejump
722 && (temp3 = prev_active_insn (insn)) != 0
723 && GET_CODE (temp3) == INSN
724 && (temp4 = single_set (temp3)) != 0
725 && GET_CODE (temp1 = SET_DEST (temp4)) == REG
726 #ifdef SMALL_REGISTER_CLASSES
727 && REGNO (temp1) >= FIRST_PSEUDO_REGISTER
728 #endif
729 && (temp2 = next_active_insn (insn)) != 0
730 && GET_CODE (temp2) == INSN
731 && (temp4 = single_set (temp2)) != 0
732 && rtx_equal_p (SET_DEST (temp4), temp1)
733 && (GET_CODE (SET_SRC (temp4)) == REG
734 || GET_CODE (SET_SRC (temp4)) == SUBREG
735 || CONSTANT_P (SET_SRC (temp4)))
736 && (REG_NOTES (temp2) == 0
737 || ((REG_NOTE_KIND (REG_NOTES (temp2)) == REG_EQUAL
738 || REG_NOTE_KIND (REG_NOTES (temp2)) == REG_EQUIV)
739 && XEXP (REG_NOTES (temp2), 1) == 0
740 && rtx_equal_p (XEXP (REG_NOTES (temp2), 0),
741 SET_SRC (temp4))))
742 && (temp = prev_active_insn (temp3)) != 0
743 && condjump_p (temp) && ! simplejump_p (temp)
744 /* TEMP must skip over the "x = a;" insn */
745 && prev_real_insn (JUMP_LABEL (temp)) == insn
746 && no_labels_between_p (insn, JUMP_LABEL (temp))
747 /* There must be no other entries to the "x = b;" insn. */
748 && no_labels_between_p (JUMP_LABEL (temp), temp2)
749 /* INSN must either branch to the insn after TEMP2 or the insn
750 after TEMP2 must branch to the same place as INSN. */
751 && (reallabelprev == temp2
752 || ((temp5 = next_active_insn (temp2)) != 0
753 && simplejump_p (temp5)
754 && JUMP_LABEL (temp5) == JUMP_LABEL (insn))))
756 /* The test expression, X, may be a complicated test with
757 multiple branches. See if we can find all the uses of
758 the label that TEMP branches to without hitting a CALL_INSN
759 or a jump to somewhere else. */
760 rtx target = JUMP_LABEL (temp);
761 int nuses = LABEL_NUSES (target);
762 rtx p, q;
764 /* Set P to the first jump insn that goes around "x = a;". */
765 for (p = temp; nuses && p; p = prev_nonnote_insn (p))
767 if (GET_CODE (p) == JUMP_INSN)
769 if (condjump_p (p) && ! simplejump_p (p)
770 && JUMP_LABEL (p) == target)
772 nuses--;
773 if (nuses == 0)
774 break;
776 else
777 break;
779 else if (GET_CODE (p) == CALL_INSN)
780 break;
783 #ifdef HAVE_cc0
784 /* We cannot insert anything between a set of cc and its use
785 so if P uses cc0, we must back up to the previous insn. */
786 q = prev_nonnote_insn (p);
787 if (q && GET_RTX_CLASS (GET_CODE (q)) == 'i'
788 && sets_cc0_p (PATTERN (q)))
789 p = q;
790 #endif
792 if (p)
793 p = PREV_INSN (p);
795 /* If we found all the uses and there was no data conflict, we
796 can move the assignment unless we can branch into the middle
797 from somewhere. */
798 if (nuses == 0 && p
799 && no_labels_between_p (p, insn)
800 && ! reg_referenced_between_p (temp1, p, NEXT_INSN (temp3))
801 && ! reg_set_between_p (temp1, p, temp3)
802 && (GET_CODE (SET_SRC (temp4)) == CONST_INT
803 || ! reg_set_between_p (SET_SRC (temp4), p, temp2)))
805 emit_insn_after_with_line_notes (PATTERN (temp2), p, temp2);
806 delete_insn (temp2);
808 /* Set NEXT to an insn that we know won't go away. */
809 next = next_active_insn (insn);
811 /* Delete the jump around the set. Note that we must do
812 this before we redirect the test jumps so that it won't
813 delete the code immediately following the assignment
814 we moved (which might be a jump). */
816 delete_insn (insn);
818 /* We either have two consecutive labels or a jump to
819 a jump, so adjust all the JUMP_INSNs to branch to where
820 INSN branches to. */
821 for (p = NEXT_INSN (p); p != next; p = NEXT_INSN (p))
822 if (GET_CODE (p) == JUMP_INSN)
823 redirect_jump (p, target);
825 changed = 1;
826 continue;
830 #ifndef HAVE_cc0
831 /* If we have if (...) x = exp; and branches are expensive,
832 EXP is a single insn, does not have any side effects, cannot
833 trap, and is not too costly, convert this to
834 t = exp; if (...) x = t;
836 Don't do this when we have CC0 because it is unlikely to help
837 and we'd need to worry about where to place the new insn and
838 the potential for conflicts. We also can't do this when we have
839 notes on the insn for the same reason as above.
841 We set:
843 TEMP to the "x = exp;" insn.
844 TEMP1 to the single set in the "x = exp; insn.
845 TEMP2 to "x". */
847 if (! reload_completed
848 && this_is_condjump && ! this_is_simplejump
849 && BRANCH_COST >= 3
850 && (temp = next_nonnote_insn (insn)) != 0
851 && GET_CODE (temp) == INSN
852 && REG_NOTES (temp) == 0
853 && (reallabelprev == temp
854 || ((temp2 = next_active_insn (temp)) != 0
855 && simplejump_p (temp2)
856 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
857 && (temp1 = single_set (temp)) != 0
858 && (temp2 = SET_DEST (temp1), GET_CODE (temp2) == REG)
859 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
860 #ifdef SMALL_REGISTER_CLASSES
861 && REGNO (temp2) >= FIRST_PSEUDO_REGISTER
862 #endif
863 && GET_CODE (SET_SRC (temp1)) != REG
864 && GET_CODE (SET_SRC (temp1)) != SUBREG
865 && GET_CODE (SET_SRC (temp1)) != CONST_INT
866 && ! side_effects_p (SET_SRC (temp1))
867 && ! may_trap_p (SET_SRC (temp1))
868 && rtx_cost (SET_SRC (temp1)) < 10)
870 rtx new = gen_reg_rtx (GET_MODE (temp2));
872 if (validate_change (temp, &SET_DEST (temp1), new, 0))
874 next = emit_insn_after (gen_move_insn (temp2, new), insn);
875 emit_insn_after_with_line_notes (PATTERN (temp),
876 PREV_INSN (insn), temp);
877 delete_insn (temp);
878 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
882 /* Similarly, if it takes two insns to compute EXP but they
883 have the same destination. Here TEMP3 will be the second
884 insn and TEMP4 the SET from that insn. */
886 if (! reload_completed
887 && this_is_condjump && ! this_is_simplejump
888 && BRANCH_COST >= 4
889 && (temp = next_nonnote_insn (insn)) != 0
890 && GET_CODE (temp) == INSN
891 && REG_NOTES (temp) == 0
892 && (temp3 = next_nonnote_insn (temp)) != 0
893 && GET_CODE (temp3) == INSN
894 && REG_NOTES (temp3) == 0
895 && (reallabelprev == temp3
896 || ((temp2 = next_active_insn (temp3)) != 0
897 && simplejump_p (temp2)
898 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
899 && (temp1 = single_set (temp)) != 0
900 && (temp2 = SET_DEST (temp1), GET_CODE (temp2) == REG)
901 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
902 #ifdef SMALL_REGISTER_CLASSES
903 && REGNO (temp2) >= FIRST_PSEUDO_REGISTER
904 #endif
905 && ! side_effects_p (SET_SRC (temp1))
906 && ! may_trap_p (SET_SRC (temp1))
907 && rtx_cost (SET_SRC (temp1)) < 10
908 && (temp4 = single_set (temp3)) != 0
909 && rtx_equal_p (SET_DEST (temp4), temp2)
910 && ! side_effects_p (SET_SRC (temp4))
911 && ! may_trap_p (SET_SRC (temp4))
912 && rtx_cost (SET_SRC (temp4)) < 10)
914 rtx new = gen_reg_rtx (GET_MODE (temp2));
916 if (validate_change (temp, &SET_DEST (temp1), new, 0))
918 next = emit_insn_after (gen_move_insn (temp2, new), insn);
919 emit_insn_after_with_line_notes (PATTERN (temp),
920 PREV_INSN (insn), temp);
921 emit_insn_after_with_line_notes
922 (replace_rtx (PATTERN (temp3), temp2, new),
923 PREV_INSN (insn), temp3);
924 delete_insn (temp);
925 delete_insn (temp3);
926 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
930 /* Finally, handle the case where two insns are used to
931 compute EXP but a temporary register is used. Here we must
932 ensure that the temporary register is not used anywhere else. */
934 if (! reload_completed
935 && after_regscan
936 && this_is_condjump && ! this_is_simplejump
937 && BRANCH_COST >= 4
938 && (temp = next_nonnote_insn (insn)) != 0
939 && GET_CODE (temp) == INSN
940 && REG_NOTES (temp) == 0
941 && (temp3 = next_nonnote_insn (temp)) != 0
942 && GET_CODE (temp3) == INSN
943 && REG_NOTES (temp3) == 0
944 && (reallabelprev == temp3
945 || ((temp2 = next_active_insn (temp3)) != 0
946 && simplejump_p (temp2)
947 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
948 && (temp1 = single_set (temp)) != 0
949 && (temp5 = SET_DEST (temp1),
950 (GET_CODE (temp5) == REG
951 || (GET_CODE (temp5) == SUBREG
952 && (temp5 = SUBREG_REG (temp5),
953 GET_CODE (temp5) == REG))))
954 && REGNO (temp5) >= FIRST_PSEUDO_REGISTER
955 && regno_first_uid[REGNO (temp5)] == INSN_UID (temp)
956 && regno_last_uid[REGNO (temp5)] == INSN_UID (temp3)
957 && ! side_effects_p (SET_SRC (temp1))
958 && ! may_trap_p (SET_SRC (temp1))
959 && rtx_cost (SET_SRC (temp1)) < 10
960 && (temp4 = single_set (temp3)) != 0
961 && (temp2 = SET_DEST (temp4), GET_CODE (temp2) == REG)
962 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
963 #ifdef SMALL_REGISTER_CLASSES
964 && REGNO (temp2) >= FIRST_PSEUDO_REGISTER
965 #endif
966 && rtx_equal_p (SET_DEST (temp4), temp2)
967 && ! side_effects_p (SET_SRC (temp4))
968 && ! may_trap_p (SET_SRC (temp4))
969 && rtx_cost (SET_SRC (temp4)) < 10)
971 rtx new = gen_reg_rtx (GET_MODE (temp2));
973 if (validate_change (temp3, &SET_DEST (temp4), new, 0))
975 next = emit_insn_after (gen_move_insn (temp2, new), insn);
976 emit_insn_after_with_line_notes (PATTERN (temp),
977 PREV_INSN (insn), temp);
978 emit_insn_after_with_line_notes (PATTERN (temp3),
979 PREV_INSN (insn), temp3);
980 delete_insn (temp);
981 delete_insn (temp3);
982 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
985 #endif /* HAVE_cc0 */
987 /* We deal with four cases:
989 1) x = a; if (...) x = b; and either A or B is zero,
990 2) if (...) x = 0; and jumps are expensive,
991 3) x = a; if (...) x = b; and A and B are constants where all the
992 set bits in A are also set in B and jumps are expensive, and
993 4) x = a; if (...) x = b; and A and B non-zero, and jumps are
994 more expensive.
995 5) if (...) x = b; if jumps are even more expensive.
997 In each of these try to use a store-flag insn to avoid the jump.
998 (If the jump would be faster, the machine should not have
999 defined the scc insns!). These cases are often made by the
1000 previous optimization.
1002 INSN here is the jump around the store. We set:
1004 TEMP to the "x = b;" insn.
1005 TEMP1 to X.
1006 TEMP2 to B (const0_rtx in the second case).
1007 TEMP3 to A (X in the second case).
1008 TEMP4 to the condition being tested.
1009 TEMP5 to the earliest insn used to find the condition. */
1011 if (/* We can't do this after reload has completed. */
1012 ! reload_completed
1013 && this_is_condjump && ! this_is_simplejump
1014 /* Set TEMP to the "x = b;" insn. */
1015 && (temp = next_nonnote_insn (insn)) != 0
1016 && GET_CODE (temp) == INSN
1017 && GET_CODE (PATTERN (temp)) == SET
1018 && GET_CODE (temp1 = SET_DEST (PATTERN (temp))) == REG
1019 #ifdef SMALL_REGISTER_CLASSES
1020 && REGNO (temp1) >= FIRST_PSEUDO_REGISTER
1021 #endif
1022 && GET_MODE_CLASS (GET_MODE (temp1)) == MODE_INT
1023 && (GET_CODE (temp2 = SET_SRC (PATTERN (temp))) == REG
1024 || GET_CODE (temp2) == SUBREG
1025 || GET_CODE (temp2) == CONST_INT)
1026 /* Allow either form, but prefer the former if both apply.
1027 There is no point in using the old value of TEMP1 if
1028 it is a register, since cse will alias them. It can
1029 lose if the old value were a hard register since CSE
1030 won't replace hard registers. */
1031 && (((temp3 = reg_set_last (temp1, insn)) != 0
1032 && GET_CODE (temp3) == CONST_INT)
1033 /* Make the latter case look like x = x; if (...) x = 0; */
1034 || (temp3 = temp1,
1035 ((BRANCH_COST >= 2
1036 && temp2 == const0_rtx)
1037 #ifdef HAVE_conditional_move
1038 || 1
1039 #endif
1040 || BRANCH_COST >= 3)))
1041 /* INSN must either branch to the insn after TEMP or the insn
1042 after TEMP must branch to the same place as INSN. */
1043 && (reallabelprev == temp
1044 || ((temp4 = next_active_insn (temp)) != 0
1045 && simplejump_p (temp4)
1046 && JUMP_LABEL (temp4) == JUMP_LABEL (insn)))
1047 && (temp4 = get_condition (insn, &temp5)) != 0
1048 /* We must be comparing objects whose modes imply the size.
1049 We could handle BLKmode if (1) emit_store_flag could
1050 and (2) we could find the size reliably. */
1051 && GET_MODE (XEXP (temp4, 0)) != BLKmode
1053 /* If B is zero, OK; if A is zero, can only do (1) if we
1054 can reverse the condition. See if (3) applies possibly
1055 by reversing the condition. Prefer reversing to (4) when
1056 branches are very expensive. */
1057 && ((reversep = 0, temp2 == const0_rtx)
1058 || (temp3 == const0_rtx
1059 && (reversep = can_reverse_comparison_p (temp4, insn)))
1060 || (BRANCH_COST >= 2
1061 && GET_CODE (temp2) == CONST_INT
1062 && GET_CODE (temp3) == CONST_INT
1063 && ((INTVAL (temp2) & INTVAL (temp3)) == INTVAL (temp2)
1064 || ((INTVAL (temp2) & INTVAL (temp3)) == INTVAL (temp3)
1065 && (reversep = can_reverse_comparison_p (temp4,
1066 insn)))))
1067 #ifdef HAVE_conditional_move
1068 || 1
1069 #endif
1070 || BRANCH_COST >= 3)
1071 #ifdef HAVE_cc0
1072 /* If the previous insn sets CC0 and something else, we can't
1073 do this since we are going to delete that insn. */
1075 && ! ((temp6 = prev_nonnote_insn (insn)) != 0
1076 && GET_CODE (temp6) == INSN
1077 && (sets_cc0_p (PATTERN (temp6)) == -1
1078 || (sets_cc0_p (PATTERN (temp6)) == 1
1079 && FIND_REG_INC_NOTE (temp6, NULL_RTX))))
1080 #endif
1083 enum rtx_code code = GET_CODE (temp4);
1084 rtx uval, cval, var = temp1;
1085 int normalizep;
1086 rtx target;
1088 /* If necessary, reverse the condition. */
1089 if (reversep)
1090 code = reverse_condition (code), uval = temp2, cval = temp3;
1091 else
1092 uval = temp3, cval = temp2;
1094 /* See if we can do this with a store-flag insn. */
1095 start_sequence ();
1097 /* If CVAL is non-zero, normalize to -1. Otherwise,
1098 if UVAL is the constant 1, it is best to just compute
1099 the result directly. If UVAL is constant and STORE_FLAG_VALUE
1100 includes all of its bits, it is best to compute the flag
1101 value unnormalized and `and' it with UVAL. Otherwise,
1102 normalize to -1 and `and' with UVAL. */
1103 normalizep = (cval != const0_rtx ? -1
1104 : (uval == const1_rtx ? 1
1105 : (GET_CODE (uval) == CONST_INT
1106 && (INTVAL (uval) & ~STORE_FLAG_VALUE) == 0)
1107 ? 0 : -1));
1109 /* We will be putting the store-flag insn immediately in
1110 front of the comparison that was originally being done,
1111 so we know all the variables in TEMP4 will be valid.
1112 However, this might be in front of the assignment of
1113 A to VAR. If it is, it would clobber the store-flag
1114 we will be emitting.
1116 Therefore, emit into a temporary which will be copied to
1117 VAR immediately after TEMP. */
1119 target = emit_store_flag (gen_reg_rtx (GET_MODE (var)), code,
1120 XEXP (temp4, 0), XEXP (temp4, 1),
1121 VOIDmode,
1122 (code == LTU || code == LEU
1123 || code == GEU || code == GTU),
1124 normalizep);
1125 if (target)
1127 rtx before = insn;
1128 rtx seq;
1130 /* Put the store-flag insns in front of the first insn
1131 used to compute the condition to ensure that we
1132 use the same values of them as the current
1133 comparison. However, the remainder of the insns we
1134 generate will be placed directly in front of the
1135 jump insn, in case any of the pseudos we use
1136 are modified earlier. */
1138 seq = get_insns ();
1139 end_sequence ();
1141 emit_insns_before (seq, temp5);
1143 start_sequence ();
1145 /* Both CVAL and UVAL are non-zero. */
1146 if (cval != const0_rtx && uval != const0_rtx)
1148 rtx tem1, tem2;
1150 tem1 = expand_and (uval, target, NULL_RTX);
1151 if (GET_CODE (cval) == CONST_INT
1152 && GET_CODE (uval) == CONST_INT
1153 && (INTVAL (cval) & INTVAL (uval)) == INTVAL (cval))
1154 tem2 = cval;
1155 else
1157 tem2 = expand_unop (GET_MODE (var), one_cmpl_optab,
1158 target, NULL_RTX, 0);
1159 tem2 = expand_and (cval, tem2,
1160 (GET_CODE (tem2) == REG
1161 ? tem2 : 0));
1164 /* If we usually make new pseudos, do so here. This
1165 turns out to help machines that have conditional
1166 move insns. */
1168 if (flag_expensive_optimizations)
1169 target = 0;
1171 target = expand_binop (GET_MODE (var), ior_optab,
1172 tem1, tem2, target,
1173 1, OPTAB_WIDEN);
1175 else if (normalizep != 1)
1177 /* We know that either CVAL or UVAL is zero. If
1178 UVAL is zero, negate TARGET and `and' with CVAL.
1179 Otherwise, `and' with UVAL. */
1180 if (uval == const0_rtx)
1182 target = expand_unop (GET_MODE (var), one_cmpl_optab,
1183 target, NULL_RTX, 0);
1184 uval = cval;
1187 target = expand_and (uval, target,
1188 (GET_CODE (target) == REG
1189 && ! preserve_subexpressions_p ()
1190 ? target : NULL_RTX));
1193 emit_move_insn (var, target);
1194 seq = get_insns ();
1195 end_sequence ();
1197 #ifdef HAVE_cc0
1198 /* If INSN uses CC0, we must not separate it from the
1199 insn that sets cc0. */
1201 if (reg_mentioned_p (cc0_rtx, PATTERN (before)))
1202 before = prev_nonnote_insn (before);
1203 #endif
1205 emit_insns_before (seq, before);
1207 delete_insn (temp);
1208 next = NEXT_INSN (insn);
1210 delete_jump (insn);
1211 changed = 1;
1212 continue;
1214 else
1215 end_sequence ();
1218 /* If branches are expensive, convert
1219 if (foo) bar++; to bar += (foo != 0);
1220 and similarly for "bar--;"
1222 INSN is the conditional branch around the arithmetic. We set:
1224 TEMP is the arithmetic insn.
1225 TEMP1 is the SET doing the arithmetic.
1226 TEMP2 is the operand being incremented or decremented.
1227 TEMP3 to the condition being tested.
1228 TEMP4 to the earliest insn used to find the condition. */
1230 if ((BRANCH_COST >= 2
1231 #ifdef HAVE_incscc
1232 || HAVE_incscc
1233 #endif
1234 #ifdef HAVE_decscc
1235 || HAVE_decscc
1236 #endif
1238 && ! reload_completed
1239 && this_is_condjump && ! this_is_simplejump
1240 && (temp = next_nonnote_insn (insn)) != 0
1241 && (temp1 = single_set (temp)) != 0
1242 && (temp2 = SET_DEST (temp1),
1243 GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT)
1244 && GET_CODE (SET_SRC (temp1)) == PLUS
1245 && (XEXP (SET_SRC (temp1), 1) == const1_rtx
1246 || XEXP (SET_SRC (temp1), 1) == constm1_rtx)
1247 && rtx_equal_p (temp2, XEXP (SET_SRC (temp1), 0))
1248 /* INSN must either branch to the insn after TEMP or the insn
1249 after TEMP must branch to the same place as INSN. */
1250 && (reallabelprev == temp
1251 || ((temp3 = next_active_insn (temp)) != 0
1252 && simplejump_p (temp3)
1253 && JUMP_LABEL (temp3) == JUMP_LABEL (insn)))
1254 && (temp3 = get_condition (insn, &temp4)) != 0
1255 /* We must be comparing objects whose modes imply the size.
1256 We could handle BLKmode if (1) emit_store_flag could
1257 and (2) we could find the size reliably. */
1258 && GET_MODE (XEXP (temp3, 0)) != BLKmode
1259 && can_reverse_comparison_p (temp3, insn))
1261 rtx temp6, target = 0, seq, init_insn = 0, init = temp2;
1262 enum rtx_code code = reverse_condition (GET_CODE (temp3));
1264 start_sequence ();
1266 /* It must be the case that TEMP2 is not modified in the range
1267 [TEMP4, INSN). The one exception we make is if the insn
1268 before INSN sets TEMP2 to something which is also unchanged
1269 in that range. In that case, we can move the initialization
1270 into our sequence. */
1272 if ((temp5 = prev_active_insn (insn)) != 0
1273 && GET_CODE (temp5) == INSN
1274 && (temp6 = single_set (temp5)) != 0
1275 && rtx_equal_p (temp2, SET_DEST (temp6))
1276 && (CONSTANT_P (SET_SRC (temp6))
1277 || GET_CODE (SET_SRC (temp6)) == REG
1278 || GET_CODE (SET_SRC (temp6)) == SUBREG))
1280 emit_insn (PATTERN (temp5));
1281 init_insn = temp5;
1282 init = SET_SRC (temp6);
1285 if (CONSTANT_P (init)
1286 || ! reg_set_between_p (init, PREV_INSN (temp4), insn))
1287 target = emit_store_flag (gen_reg_rtx (GET_MODE (temp2)), code,
1288 XEXP (temp3, 0), XEXP (temp3, 1),
1289 VOIDmode,
1290 (code == LTU || code == LEU
1291 || code == GTU || code == GEU), 1);
1293 /* If we can do the store-flag, do the addition or
1294 subtraction. */
1296 if (target)
1297 target = expand_binop (GET_MODE (temp2),
1298 (XEXP (SET_SRC (temp1), 1) == const1_rtx
1299 ? add_optab : sub_optab),
1300 temp2, target, temp2, 0, OPTAB_WIDEN);
1302 if (target != 0)
1304 /* Put the result back in temp2 in case it isn't already.
1305 Then replace the jump, possible a CC0-setting insn in
1306 front of the jump, and TEMP, with the sequence we have
1307 made. */
1309 if (target != temp2)
1310 emit_move_insn (temp2, target);
1312 seq = get_insns ();
1313 end_sequence ();
1315 emit_insns_before (seq, temp4);
1316 delete_insn (temp);
1318 if (init_insn)
1319 delete_insn (init_insn);
1321 next = NEXT_INSN (insn);
1322 #ifdef HAVE_cc0
1323 delete_insn (prev_nonnote_insn (insn));
1324 #endif
1325 delete_insn (insn);
1326 changed = 1;
1327 continue;
1329 else
1330 end_sequence ();
1333 /* Simplify if (...) x = 1; else {...} if (x) ...
1334 We recognize this case scanning backwards as well.
1336 TEMP is the assignment to x;
1337 TEMP1 is the label at the head of the second if. */
1338 /* ?? This should call get_condition to find the values being
1339 compared, instead of looking for a COMPARE insn when HAVE_cc0
1340 is not defined. This would allow it to work on the m88k. */
1341 /* ?? This optimization is only safe before cse is run if HAVE_cc0
1342 is not defined and the condition is tested by a separate compare
1343 insn. This is because the code below assumes that the result
1344 of the compare dies in the following branch.
1346 Not only that, but there might be other insns between the
1347 compare and branch whose results are live. Those insns need
1348 to be executed.
1350 A way to fix this is to move the insns at JUMP_LABEL (insn)
1351 to before INSN. If we are running before flow, they will
1352 be deleted if they aren't needed. But this doesn't work
1353 well after flow.
1355 This is really a special-case of jump threading, anyway. The
1356 right thing to do is to replace this and jump threading with
1357 much simpler code in cse.
1359 This code has been turned off in the non-cc0 case in the
1360 meantime. */
1362 #ifdef HAVE_cc0
1363 else if (this_is_simplejump
1364 /* Safe to skip USE and CLOBBER insns here
1365 since they will not be deleted. */
1366 && (temp = prev_active_insn (insn))
1367 && no_labels_between_p (temp, insn)
1368 && GET_CODE (temp) == INSN
1369 && GET_CODE (PATTERN (temp)) == SET
1370 && GET_CODE (SET_DEST (PATTERN (temp))) == REG
1371 && CONSTANT_P (SET_SRC (PATTERN (temp)))
1372 && (temp1 = next_active_insn (JUMP_LABEL (insn)))
1373 /* If we find that the next value tested is `x'
1374 (TEMP1 is the insn where this happens), win. */
1375 && GET_CODE (temp1) == INSN
1376 && GET_CODE (PATTERN (temp1)) == SET
1377 #ifdef HAVE_cc0
1378 /* Does temp1 `tst' the value of x? */
1379 && SET_SRC (PATTERN (temp1)) == SET_DEST (PATTERN (temp))
1380 && SET_DEST (PATTERN (temp1)) == cc0_rtx
1381 && (temp1 = next_nonnote_insn (temp1))
1382 #else
1383 /* Does temp1 compare the value of x against zero? */
1384 && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
1385 && XEXP (SET_SRC (PATTERN (temp1)), 1) == const0_rtx
1386 && (XEXP (SET_SRC (PATTERN (temp1)), 0)
1387 == SET_DEST (PATTERN (temp)))
1388 && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
1389 && (temp1 = find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
1390 #endif
1391 && condjump_p (temp1))
1393 /* Get the if_then_else from the condjump. */
1394 rtx choice = SET_SRC (PATTERN (temp1));
1395 if (GET_CODE (choice) == IF_THEN_ELSE)
1397 enum rtx_code code = GET_CODE (XEXP (choice, 0));
1398 rtx val = SET_SRC (PATTERN (temp));
1399 rtx cond
1400 = simplify_relational_operation (code, GET_MODE (SET_DEST (PATTERN (temp))),
1401 val, const0_rtx);
1402 rtx ultimate;
1404 if (cond == const_true_rtx)
1405 ultimate = XEXP (choice, 1);
1406 else if (cond == const0_rtx)
1407 ultimate = XEXP (choice, 2);
1408 else
1409 ultimate = 0;
1411 if (ultimate == pc_rtx)
1412 ultimate = get_label_after (temp1);
1413 else if (ultimate && GET_CODE (ultimate) != RETURN)
1414 ultimate = XEXP (ultimate, 0);
1416 if (ultimate)
1417 changed |= redirect_jump (insn, ultimate);
1420 #endif
1422 #if 0
1423 /* @@ This needs a bit of work before it will be right.
1425 Any type of comparison can be accepted for the first and
1426 second compare. When rewriting the first jump, we must
1427 compute the what conditions can reach label3, and use the
1428 appropriate code. We can not simply reverse/swap the code
1429 of the first jump. In some cases, the second jump must be
1430 rewritten also.
1432 For example,
1433 < == converts to > ==
1434 < != converts to == >
1435 etc.
1437 If the code is written to only accept an '==' test for the second
1438 compare, then all that needs to be done is to swap the condition
1439 of the first branch.
1441 It is questionable whether we want this optimization anyways,
1442 since if the user wrote code like this because he/she knew that
1443 the jump to label1 is taken most of the time, then rewriting
1444 this gives slower code. */
1445 /* @@ This should call get_condition to find the values being
1446 compared, instead of looking for a COMPARE insn when HAVE_cc0
1447 is not defined. This would allow it to work on the m88k. */
1448 /* @@ This optimization is only safe before cse is run if HAVE_cc0
1449 is not defined and the condition is tested by a separate compare
1450 insn. This is because the code below assumes that the result
1451 of the compare dies in the following branch. */
1453 /* Simplify test a ~= b
1454 condjump label1;
1455 test a == b
1456 condjump label2;
1457 jump label3;
1458 label1:
1460 rewriting as
1461 test a ~~= b
1462 condjump label3
1463 test a == b
1464 condjump label2
1465 label1:
1467 where ~= is an inequality, e.g. >, and ~~= is the swapped
1468 inequality, e.g. <.
1470 We recognize this case scanning backwards.
1472 TEMP is the conditional jump to `label2';
1473 TEMP1 is the test for `a == b';
1474 TEMP2 is the conditional jump to `label1';
1475 TEMP3 is the test for `a ~= b'. */
1476 else if (this_is_simplejump
1477 && (temp = prev_active_insn (insn))
1478 && no_labels_between_p (temp, insn)
1479 && condjump_p (temp)
1480 && (temp1 = prev_active_insn (temp))
1481 && no_labels_between_p (temp1, temp)
1482 && GET_CODE (temp1) == INSN
1483 && GET_CODE (PATTERN (temp1)) == SET
1484 #ifdef HAVE_cc0
1485 && sets_cc0_p (PATTERN (temp1)) == 1
1486 #else
1487 && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
1488 && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
1489 && (temp == find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
1490 #endif
1491 && (temp2 = prev_active_insn (temp1))
1492 && no_labels_between_p (temp2, temp1)
1493 && condjump_p (temp2)
1494 && JUMP_LABEL (temp2) == next_nonnote_insn (NEXT_INSN (insn))
1495 && (temp3 = prev_active_insn (temp2))
1496 && no_labels_between_p (temp3, temp2)
1497 && GET_CODE (PATTERN (temp3)) == SET
1498 && rtx_equal_p (SET_DEST (PATTERN (temp3)),
1499 SET_DEST (PATTERN (temp1)))
1500 && rtx_equal_p (SET_SRC (PATTERN (temp1)),
1501 SET_SRC (PATTERN (temp3)))
1502 && ! inequality_comparisons_p (PATTERN (temp))
1503 && inequality_comparisons_p (PATTERN (temp2)))
1505 rtx fallthrough_label = JUMP_LABEL (temp2);
1507 ++LABEL_NUSES (fallthrough_label);
1508 if (swap_jump (temp2, JUMP_LABEL (insn)))
1510 delete_insn (insn);
1511 changed = 1;
1514 if (--LABEL_NUSES (fallthrough_label) == 0)
1515 delete_insn (fallthrough_label);
1517 #endif
1518 /* Simplify if (...) {... x = 1;} if (x) ...
1520 We recognize this case backwards.
1522 TEMP is the test of `x';
1523 TEMP1 is the assignment to `x' at the end of the
1524 previous statement. */
1525 /* @@ This should call get_condition to find the values being
1526 compared, instead of looking for a COMPARE insn when HAVE_cc0
1527 is not defined. This would allow it to work on the m88k. */
1528 /* @@ This optimization is only safe before cse is run if HAVE_cc0
1529 is not defined and the condition is tested by a separate compare
1530 insn. This is because the code below assumes that the result
1531 of the compare dies in the following branch. */
1533 /* ??? This has to be turned off. The problem is that the
1534 unconditional jump might indirectly end up branching to the
1535 label between TEMP1 and TEMP. We can't detect this, in general,
1536 since it may become a jump to there after further optimizations.
1537 If that jump is done, it will be deleted, so we will retry
1538 this optimization in the next pass, thus an infinite loop.
1540 The present code prevents this by putting the jump after the
1541 label, but this is not logically correct. */
1542 #if 0
1543 else if (this_is_condjump
1544 /* Safe to skip USE and CLOBBER insns here
1545 since they will not be deleted. */
1546 && (temp = prev_active_insn (insn))
1547 && no_labels_between_p (temp, insn)
1548 && GET_CODE (temp) == INSN
1549 && GET_CODE (PATTERN (temp)) == SET
1550 #ifdef HAVE_cc0
1551 && sets_cc0_p (PATTERN (temp)) == 1
1552 && GET_CODE (SET_SRC (PATTERN (temp))) == REG
1553 #else
1554 /* Temp must be a compare insn, we can not accept a register
1555 to register move here, since it may not be simply a
1556 tst insn. */
1557 && GET_CODE (SET_SRC (PATTERN (temp))) == COMPARE
1558 && XEXP (SET_SRC (PATTERN (temp)), 1) == const0_rtx
1559 && GET_CODE (XEXP (SET_SRC (PATTERN (temp)), 0)) == REG
1560 && GET_CODE (SET_DEST (PATTERN (temp))) == REG
1561 && insn == find_next_ref (SET_DEST (PATTERN (temp)), temp)
1562 #endif
1563 /* May skip USE or CLOBBER insns here
1564 for checking for opportunity, since we
1565 take care of them later. */
1566 && (temp1 = prev_active_insn (temp))
1567 && GET_CODE (temp1) == INSN
1568 && GET_CODE (PATTERN (temp1)) == SET
1569 #ifdef HAVE_cc0
1570 && SET_SRC (PATTERN (temp)) == SET_DEST (PATTERN (temp1))
1571 #else
1572 && (XEXP (SET_SRC (PATTERN (temp)), 0)
1573 == SET_DEST (PATTERN (temp1)))
1574 #endif
1575 && CONSTANT_P (SET_SRC (PATTERN (temp1)))
1576 /* If this isn't true, cse will do the job. */
1577 && ! no_labels_between_p (temp1, temp))
1579 /* Get the if_then_else from the condjump. */
1580 rtx choice = SET_SRC (PATTERN (insn));
1581 if (GET_CODE (choice) == IF_THEN_ELSE
1582 && (GET_CODE (XEXP (choice, 0)) == EQ
1583 || GET_CODE (XEXP (choice, 0)) == NE))
1585 int want_nonzero = (GET_CODE (XEXP (choice, 0)) == NE);
1586 rtx last_insn;
1587 rtx ultimate;
1588 rtx p;
1590 /* Get the place that condjump will jump to
1591 if it is reached from here. */
1592 if ((SET_SRC (PATTERN (temp1)) != const0_rtx)
1593 == want_nonzero)
1594 ultimate = XEXP (choice, 1);
1595 else
1596 ultimate = XEXP (choice, 2);
1597 /* Get it as a CODE_LABEL. */
1598 if (ultimate == pc_rtx)
1599 ultimate = get_label_after (insn);
1600 else
1601 /* Get the label out of the LABEL_REF. */
1602 ultimate = XEXP (ultimate, 0);
1604 /* Insert the jump immediately before TEMP, specifically
1605 after the label that is between TEMP1 and TEMP. */
1606 last_insn = PREV_INSN (temp);
1608 /* If we would be branching to the next insn, the jump
1609 would immediately be deleted and the re-inserted in
1610 a subsequent pass over the code. So don't do anything
1611 in that case. */
1612 if (next_active_insn (last_insn)
1613 != next_active_insn (ultimate))
1615 emit_barrier_after (last_insn);
1616 p = emit_jump_insn_after (gen_jump (ultimate),
1617 last_insn);
1618 JUMP_LABEL (p) = ultimate;
1619 ++LABEL_NUSES (ultimate);
1620 if (INSN_UID (ultimate) < max_jump_chain
1621 && INSN_CODE (p) < max_jump_chain)
1623 jump_chain[INSN_UID (p)]
1624 = jump_chain[INSN_UID (ultimate)];
1625 jump_chain[INSN_UID (ultimate)] = p;
1627 changed = 1;
1628 continue;
1632 #endif
1633 /* Detect a conditional jump going to the same place
1634 as an immediately following unconditional jump. */
1635 else if (this_is_condjump
1636 && (temp = next_active_insn (insn)) != 0
1637 && simplejump_p (temp)
1638 && (next_active_insn (JUMP_LABEL (insn))
1639 == next_active_insn (JUMP_LABEL (temp))))
1641 delete_jump (insn);
1642 changed = 1;
1643 continue;
1645 /* Detect a conditional jump jumping over an unconditional jump. */
1647 else if (this_is_condjump && ! this_is_simplejump
1648 && reallabelprev != 0
1649 && GET_CODE (reallabelprev) == JUMP_INSN
1650 && prev_active_insn (reallabelprev) == insn
1651 && no_labels_between_p (insn, reallabelprev)
1652 && simplejump_p (reallabelprev))
1654 /* When we invert the unconditional jump, we will be
1655 decrementing the usage count of its old label.
1656 Make sure that we don't delete it now because that
1657 might cause the following code to be deleted. */
1658 rtx prev_uses = prev_nonnote_insn (reallabelprev);
1659 rtx prev_label = JUMP_LABEL (insn);
1661 if (prev_label)
1662 ++LABEL_NUSES (prev_label);
1664 if (invert_jump (insn, JUMP_LABEL (reallabelprev)))
1666 /* It is very likely that if there are USE insns before
1667 this jump, they hold REG_DEAD notes. These REG_DEAD
1668 notes are no longer valid due to this optimization,
1669 and will cause the life-analysis that following passes
1670 (notably delayed-branch scheduling) to think that
1671 these registers are dead when they are not.
1673 To prevent this trouble, we just remove the USE insns
1674 from the insn chain. */
1676 while (prev_uses && GET_CODE (prev_uses) == INSN
1677 && GET_CODE (PATTERN (prev_uses)) == USE)
1679 rtx useless = prev_uses;
1680 prev_uses = prev_nonnote_insn (prev_uses);
1681 delete_insn (useless);
1684 delete_insn (reallabelprev);
1685 next = insn;
1686 changed = 1;
1689 /* We can now safely delete the label if it is unreferenced
1690 since the delete_insn above has deleted the BARRIER. */
1691 if (prev_label && --LABEL_NUSES (prev_label) == 0)
1692 delete_insn (prev_label);
1693 continue;
1695 else
1697 /* Detect a jump to a jump. */
1699 nlabel = follow_jumps (JUMP_LABEL (insn));
1700 if (nlabel != JUMP_LABEL (insn)
1701 && redirect_jump (insn, nlabel))
1703 changed = 1;
1704 next = insn;
1707 /* Look for if (foo) bar; else break; */
1708 /* The insns look like this:
1709 insn = condjump label1;
1710 ...range1 (some insns)...
1711 jump label2;
1712 label1:
1713 ...range2 (some insns)...
1714 jump somewhere unconditionally
1715 label2: */
1717 rtx label1 = next_label (insn);
1718 rtx range1end = label1 ? prev_active_insn (label1) : 0;
1719 /* Don't do this optimization on the first round, so that
1720 jump-around-a-jump gets simplified before we ask here
1721 whether a jump is unconditional.
1723 Also don't do it when we are called after reload since
1724 it will confuse reorg. */
1725 if (! first
1726 && (reload_completed ? ! flag_delayed_branch : 1)
1727 /* Make sure INSN is something we can invert. */
1728 && condjump_p (insn)
1729 && label1 != 0
1730 && JUMP_LABEL (insn) == label1
1731 && LABEL_NUSES (label1) == 1
1732 && GET_CODE (range1end) == JUMP_INSN
1733 && simplejump_p (range1end))
1735 rtx label2 = next_label (label1);
1736 rtx range2end = label2 ? prev_active_insn (label2) : 0;
1737 if (range1end != range2end
1738 && JUMP_LABEL (range1end) == label2
1739 && GET_CODE (range2end) == JUMP_INSN
1740 && GET_CODE (NEXT_INSN (range2end)) == BARRIER
1741 /* Invert the jump condition, so we
1742 still execute the same insns in each case. */
1743 && invert_jump (insn, label1))
1745 rtx range1beg = next_active_insn (insn);
1746 rtx range2beg = next_active_insn (label1);
1747 rtx range1after, range2after;
1748 rtx range1before, range2before;
1750 /* Include in each range any notes before it, to be
1751 sure that we get the line number note if any, even
1752 if there are other notes here. */
1753 while (PREV_INSN (range1beg)
1754 && GET_CODE (PREV_INSN (range1beg)) == NOTE)
1755 range1beg = PREV_INSN (range1beg);
1757 while (PREV_INSN (range2beg)
1758 && GET_CODE (PREV_INSN (range2beg)) == NOTE)
1759 range2beg = PREV_INSN (range2beg);
1761 /* Don't move NOTEs for blocks or loops; shift them
1762 outside the ranges, where they'll stay put. */
1763 range1beg = squeeze_notes (range1beg, range1end);
1764 range2beg = squeeze_notes (range2beg, range2end);
1766 /* Get current surrounds of the 2 ranges. */
1767 range1before = PREV_INSN (range1beg);
1768 range2before = PREV_INSN (range2beg);
1769 range1after = NEXT_INSN (range1end);
1770 range2after = NEXT_INSN (range2end);
1772 /* Splice range2 where range1 was. */
1773 NEXT_INSN (range1before) = range2beg;
1774 PREV_INSN (range2beg) = range1before;
1775 NEXT_INSN (range2end) = range1after;
1776 PREV_INSN (range1after) = range2end;
1777 /* Splice range1 where range2 was. */
1778 NEXT_INSN (range2before) = range1beg;
1779 PREV_INSN (range1beg) = range2before;
1780 NEXT_INSN (range1end) = range2after;
1781 PREV_INSN (range2after) = range1end;
1782 changed = 1;
1783 continue;
1788 /* Now that the jump has been tensioned,
1789 try cross jumping: check for identical code
1790 before the jump and before its target label. */
1792 /* First, cross jumping of conditional jumps: */
1794 if (cross_jump && condjump_p (insn))
1796 rtx newjpos, newlpos;
1797 rtx x = prev_real_insn (JUMP_LABEL (insn));
1799 /* A conditional jump may be crossjumped
1800 only if the place it jumps to follows
1801 an opposing jump that comes back here. */
1803 if (x != 0 && ! jump_back_p (x, insn))
1804 /* We have no opposing jump;
1805 cannot cross jump this insn. */
1806 x = 0;
1808 newjpos = 0;
1809 /* TARGET is nonzero if it is ok to cross jump
1810 to code before TARGET. If so, see if matches. */
1811 if (x != 0)
1812 find_cross_jump (insn, x, 2,
1813 &newjpos, &newlpos);
1815 if (newjpos != 0)
1817 do_cross_jump (insn, newjpos, newlpos);
1818 /* Make the old conditional jump
1819 into an unconditional one. */
1820 SET_SRC (PATTERN (insn))
1821 = gen_rtx (LABEL_REF, VOIDmode, JUMP_LABEL (insn));
1822 INSN_CODE (insn) = -1;
1823 emit_barrier_after (insn);
1824 /* Add to jump_chain unless this is a new label
1825 whose UID is too large. */
1826 if (INSN_UID (JUMP_LABEL (insn)) < max_jump_chain)
1828 jump_chain[INSN_UID (insn)]
1829 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
1830 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
1832 changed = 1;
1833 next = insn;
1837 /* Cross jumping of unconditional jumps:
1838 a few differences. */
1840 if (cross_jump && simplejump_p (insn))
1842 rtx newjpos, newlpos;
1843 rtx target;
1845 newjpos = 0;
1847 /* TARGET is nonzero if it is ok to cross jump
1848 to code before TARGET. If so, see if matches. */
1849 find_cross_jump (insn, JUMP_LABEL (insn), 1,
1850 &newjpos, &newlpos);
1852 /* If cannot cross jump to code before the label,
1853 see if we can cross jump to another jump to
1854 the same label. */
1855 /* Try each other jump to this label. */
1856 if (INSN_UID (JUMP_LABEL (insn)) < max_uid)
1857 for (target = jump_chain[INSN_UID (JUMP_LABEL (insn))];
1858 target != 0 && newjpos == 0;
1859 target = jump_chain[INSN_UID (target)])
1860 if (target != insn
1861 && JUMP_LABEL (target) == JUMP_LABEL (insn)
1862 /* Ignore TARGET if it's deleted. */
1863 && ! INSN_DELETED_P (target))
1864 find_cross_jump (insn, target, 2,
1865 &newjpos, &newlpos);
1867 if (newjpos != 0)
1869 do_cross_jump (insn, newjpos, newlpos);
1870 changed = 1;
1871 next = insn;
1875 /* This code was dead in the previous jump.c! */
1876 if (cross_jump && GET_CODE (PATTERN (insn)) == RETURN)
1878 /* Return insns all "jump to the same place"
1879 so we can cross-jump between any two of them. */
1881 rtx newjpos, newlpos, target;
1883 newjpos = 0;
1885 /* If cannot cross jump to code before the label,
1886 see if we can cross jump to another jump to
1887 the same label. */
1888 /* Try each other jump to this label. */
1889 for (target = jump_chain[0];
1890 target != 0 && newjpos == 0;
1891 target = jump_chain[INSN_UID (target)])
1892 if (target != insn
1893 && ! INSN_DELETED_P (target)
1894 && GET_CODE (PATTERN (target)) == RETURN)
1895 find_cross_jump (insn, target, 2,
1896 &newjpos, &newlpos);
1898 if (newjpos != 0)
1900 do_cross_jump (insn, newjpos, newlpos);
1901 changed = 1;
1902 next = insn;
1908 first = 0;
1911 /* Delete extraneous line number notes.
1912 Note that two consecutive notes for different lines are not really
1913 extraneous. There should be some indication where that line belonged,
1914 even if it became empty. */
1917 rtx last_note = 0;
1919 for (insn = f; insn; insn = NEXT_INSN (insn))
1920 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) >= 0)
1922 /* Delete this note if it is identical to previous note. */
1923 if (last_note
1924 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
1925 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note))
1927 delete_insn (insn);
1928 continue;
1931 last_note = insn;
1935 #ifdef HAVE_return
1936 if (HAVE_return)
1938 /* If we fall through to the epilogue, see if we can insert a RETURN insn
1939 in front of it. If the machine allows it at this point (we might be
1940 after reload for a leaf routine), it will improve optimization for it
1941 to be there. We do this both here and at the start of this pass since
1942 the RETURN might have been deleted by some of our optimizations. */
1943 insn = get_last_insn ();
1944 while (insn && GET_CODE (insn) == NOTE)
1945 insn = PREV_INSN (insn);
1947 if (insn && GET_CODE (insn) != BARRIER)
1949 emit_jump_insn (gen_return ());
1950 emit_barrier ();
1953 #endif
1955 /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
1956 If so, delete it, and record that this function can drop off the end. */
1958 insn = last_insn;
1960 int n_labels = 1;
1961 while (insn
1962 /* One label can follow the end-note: the return label. */
1963 && ((GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
1964 /* Ordinary insns can follow it if returning a structure. */
1965 || GET_CODE (insn) == INSN
1966 /* If machine uses explicit RETURN insns, no epilogue,
1967 then one of them follows the note. */
1968 || (GET_CODE (insn) == JUMP_INSN
1969 && GET_CODE (PATTERN (insn)) == RETURN)
1970 /* Other kinds of notes can follow also. */
1971 || (GET_CODE (insn) == NOTE
1972 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)))
1973 insn = PREV_INSN (insn);
1976 /* Report if control can fall through at the end of the function. */
1977 if (insn && GET_CODE (insn) == NOTE
1978 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END)
1980 can_reach_end = 1;
1981 delete_insn (insn);
1984 /* Show JUMP_CHAIN no longer valid. */
1985 jump_chain = 0;
1988 /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
1989 jump. Assume that this unconditional jump is to the exit test code. If
1990 the code is sufficiently simple, make a copy of it before INSN,
1991 followed by a jump to the exit of the loop. Then delete the unconditional
1992 jump after INSN.
1994 Note that it is possible we can get confused here if the jump immediately
1995 after the loop start branches outside the loop but within an outer loop.
1996 If we are near the exit of that loop, we will copy its exit test. This
1997 will not generate incorrect code, but could suppress some optimizations.
1998 However, such cases are degenerate loops anyway.
2000 Return 1 if we made the change, else 0.
2002 This is only safe immediately after a regscan pass because it uses the
2003 values of regno_first_uid and regno_last_uid. */
2005 static int
2006 duplicate_loop_exit_test (loop_start)
2007 rtx loop_start;
2009 rtx insn, set, p;
2010 rtx copy, link;
2011 int num_insns = 0;
2012 rtx exitcode = NEXT_INSN (JUMP_LABEL (next_nonnote_insn (loop_start)));
2013 rtx lastexit;
2014 int max_reg = max_reg_num ();
2015 rtx *reg_map = 0;
2017 /* Scan the exit code. We do not perform this optimization if any insn:
2019 is a CALL_INSN
2020 is a CODE_LABEL
2021 has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
2022 is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
2023 is a NOTE_INSN_BLOCK_{BEG,END} because duplicating these notes
2024 are not valid
2026 Also, don't do this if the exit code is more than 20 insns. */
2028 for (insn = exitcode;
2029 insn
2030 && ! (GET_CODE (insn) == NOTE
2031 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
2032 insn = NEXT_INSN (insn))
2034 switch (GET_CODE (insn))
2036 case CODE_LABEL:
2037 case CALL_INSN:
2038 return 0;
2039 case NOTE:
2040 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
2041 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2042 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
2043 return 0;
2044 break;
2045 case JUMP_INSN:
2046 case INSN:
2047 if (++num_insns > 20
2048 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
2049 || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
2050 return 0;
2051 break;
2055 /* Unless INSN is zero, we can do the optimization. */
2056 if (insn == 0)
2057 return 0;
2059 lastexit = insn;
2061 /* See if any insn sets a register only used in the loop exit code and
2062 not a user variable. If so, replace it with a new register. */
2063 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
2064 if (GET_CODE (insn) == INSN
2065 && (set = single_set (insn)) != 0
2066 && GET_CODE (SET_DEST (set)) == REG
2067 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
2068 && regno_first_uid[REGNO (SET_DEST (set))] == INSN_UID (insn))
2070 for (p = NEXT_INSN (insn); p != lastexit; p = NEXT_INSN (p))
2071 if (regno_last_uid[REGNO (SET_DEST (set))] == INSN_UID (p))
2072 break;
2074 if (p != lastexit)
2076 /* We can do the replacement. Allocate reg_map if this is the
2077 first replacement we found. */
2078 if (reg_map == 0)
2080 reg_map = (rtx *) alloca (max_reg * sizeof (rtx));
2081 bzero (reg_map, max_reg * sizeof (rtx));
2084 REG_LOOP_TEST_P (SET_DEST (set)) = 1;
2086 reg_map[REGNO (SET_DEST (set))]
2087 = gen_reg_rtx (GET_MODE (SET_DEST (set)));
2091 /* Now copy each insn. */
2092 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
2093 switch (GET_CODE (insn))
2095 case BARRIER:
2096 copy = emit_barrier_before (loop_start);
2097 break;
2098 case NOTE:
2099 /* Only copy line-number notes. */
2100 if (NOTE_LINE_NUMBER (insn) >= 0)
2102 copy = emit_note_before (NOTE_LINE_NUMBER (insn), loop_start);
2103 NOTE_SOURCE_FILE (copy) = NOTE_SOURCE_FILE (insn);
2105 break;
2107 case INSN:
2108 copy = emit_insn_before (copy_rtx (PATTERN (insn)), loop_start);
2109 if (reg_map)
2110 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
2112 mark_jump_label (PATTERN (copy), copy, 0);
2114 /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
2115 make them. */
2116 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2117 if (REG_NOTE_KIND (link) != REG_LABEL)
2118 REG_NOTES (copy)
2119 = copy_rtx (gen_rtx (EXPR_LIST, REG_NOTE_KIND (link),
2120 XEXP (link, 0), REG_NOTES (copy)));
2121 if (reg_map && REG_NOTES (copy))
2122 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
2123 break;
2125 case JUMP_INSN:
2126 copy = emit_jump_insn_before (copy_rtx (PATTERN (insn)), loop_start);
2127 if (reg_map)
2128 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
2129 mark_jump_label (PATTERN (copy), copy, 0);
2130 if (REG_NOTES (insn))
2132 REG_NOTES (copy) = copy_rtx (REG_NOTES (insn));
2133 if (reg_map)
2134 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
2137 /* If this is a simple jump, add it to the jump chain. */
2139 if (INSN_UID (copy) < max_jump_chain && JUMP_LABEL (copy)
2140 && simplejump_p (copy))
2142 jump_chain[INSN_UID (copy)]
2143 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
2144 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
2146 break;
2148 default:
2149 abort ();
2152 /* Now clean up by emitting a jump to the end label and deleting the jump
2153 at the start of the loop. */
2154 if (GET_CODE (copy) != BARRIER)
2156 copy = emit_jump_insn_before (gen_jump (get_label_after (insn)),
2157 loop_start);
2158 mark_jump_label (PATTERN (copy), copy, 0);
2159 if (INSN_UID (copy) < max_jump_chain
2160 && INSN_UID (JUMP_LABEL (copy)) < max_jump_chain)
2162 jump_chain[INSN_UID (copy)]
2163 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
2164 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
2166 emit_barrier_before (loop_start);
2169 delete_insn (next_nonnote_insn (loop_start));
2171 /* Mark the exit code as the virtual top of the converted loop. */
2172 emit_note_before (NOTE_INSN_LOOP_VTOP, exitcode);
2174 return 1;
2177 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, and
2178 loop-end notes between START and END out before START. Assume that
2179 END is not such a note. START may be such a note. Returns the value
2180 of the new starting insn, which may be different if the original start
2181 was such a note. */
2184 squeeze_notes (start, end)
2185 rtx start, end;
2187 rtx insn;
2188 rtx next;
2190 for (insn = start; insn != end; insn = next)
2192 next = NEXT_INSN (insn);
2193 if (GET_CODE (insn) == NOTE
2194 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
2195 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2196 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
2197 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
2198 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
2199 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP))
2201 if (insn == start)
2202 start = next;
2203 else
2205 rtx prev = PREV_INSN (insn);
2206 PREV_INSN (insn) = PREV_INSN (start);
2207 NEXT_INSN (insn) = start;
2208 NEXT_INSN (PREV_INSN (insn)) = insn;
2209 PREV_INSN (NEXT_INSN (insn)) = insn;
2210 NEXT_INSN (prev) = next;
2211 PREV_INSN (next) = prev;
2216 return start;
2219 /* Compare the instructions before insn E1 with those before E2
2220 to find an opportunity for cross jumping.
2221 (This means detecting identical sequences of insns followed by
2222 jumps to the same place, or followed by a label and a jump
2223 to that label, and replacing one with a jump to the other.)
2225 Assume E1 is a jump that jumps to label E2
2226 (that is not always true but it might as well be).
2227 Find the longest possible equivalent sequences
2228 and store the first insns of those sequences into *F1 and *F2.
2229 Store zero there if no equivalent preceding instructions are found.
2231 We give up if we find a label in stream 1.
2232 Actually we could transfer that label into stream 2. */
2234 static void
2235 find_cross_jump (e1, e2, minimum, f1, f2)
2236 rtx e1, e2;
2237 int minimum;
2238 rtx *f1, *f2;
2240 register rtx i1 = e1, i2 = e2;
2241 register rtx p1, p2;
2242 int lose = 0;
2244 rtx last1 = 0, last2 = 0;
2245 rtx afterlast1 = 0, afterlast2 = 0;
2246 rtx prev1;
2248 *f1 = 0;
2249 *f2 = 0;
2251 while (1)
2253 i1 = prev_nonnote_insn (i1);
2255 i2 = PREV_INSN (i2);
2256 while (i2 && (GET_CODE (i2) == NOTE || GET_CODE (i2) == CODE_LABEL))
2257 i2 = PREV_INSN (i2);
2259 if (i1 == 0)
2260 break;
2262 /* Don't allow the range of insns preceding E1 or E2
2263 to include the other (E2 or E1). */
2264 if (i2 == e1 || i1 == e2)
2265 break;
2267 /* If we will get to this code by jumping, those jumps will be
2268 tensioned to go directly to the new label (before I2),
2269 so this cross-jumping won't cost extra. So reduce the minimum. */
2270 if (GET_CODE (i1) == CODE_LABEL)
2272 --minimum;
2273 break;
2276 if (i2 == 0 || GET_CODE (i1) != GET_CODE (i2))
2277 break;
2279 p1 = PATTERN (i1);
2280 p2 = PATTERN (i2);
2282 /* If this is a CALL_INSN, compare register usage information.
2283 If we don't check this on stack register machines, the two
2284 CALL_INSNs might be merged leaving reg-stack.c with mismatching
2285 numbers of stack registers in the same basic block.
2286 If we don't check this on machines with delay slots, a delay slot may
2287 be filled that clobbers a parameter expected by the subroutine.
2289 ??? We take the simple route for now and assume that if they're
2290 equal, they were constructed identically. */
2292 if (GET_CODE (i1) == CALL_INSN
2293 && ! rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
2294 CALL_INSN_FUNCTION_USAGE (i2)))
2295 lose = 1;
2297 #ifdef STACK_REGS
2298 /* If cross_jump_death_matters is not 0, the insn's mode
2299 indicates whether or not the insn contains any stack-like
2300 regs. */
2302 if (!lose && cross_jump_death_matters && GET_MODE (i1) == QImode)
2304 /* If register stack conversion has already been done, then
2305 death notes must also be compared before it is certain that
2306 the two instruction streams match. */
2308 rtx note;
2309 HARD_REG_SET i1_regset, i2_regset;
2311 CLEAR_HARD_REG_SET (i1_regset);
2312 CLEAR_HARD_REG_SET (i2_regset);
2314 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
2315 if (REG_NOTE_KIND (note) == REG_DEAD
2316 && STACK_REG_P (XEXP (note, 0)))
2317 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
2319 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
2320 if (REG_NOTE_KIND (note) == REG_DEAD
2321 && STACK_REG_P (XEXP (note, 0)))
2322 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
2324 GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
2326 lose = 1;
2328 done:
2331 #endif
2333 if (lose || GET_CODE (p1) != GET_CODE (p2)
2334 || ! rtx_renumbered_equal_p (p1, p2))
2336 /* The following code helps take care of G++ cleanups. */
2337 rtx equiv1;
2338 rtx equiv2;
2340 if (!lose && GET_CODE (p1) == GET_CODE (p2)
2341 && ((equiv1 = find_reg_note (i1, REG_EQUAL, NULL_RTX)) != 0
2342 || (equiv1 = find_reg_note (i1, REG_EQUIV, NULL_RTX)) != 0)
2343 && ((equiv2 = find_reg_note (i2, REG_EQUAL, NULL_RTX)) != 0
2344 || (equiv2 = find_reg_note (i2, REG_EQUIV, NULL_RTX)) != 0)
2345 /* If the equivalences are not to a constant, they may
2346 reference pseudos that no longer exist, so we can't
2347 use them. */
2348 && CONSTANT_P (XEXP (equiv1, 0))
2349 && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
2351 rtx s1 = single_set (i1);
2352 rtx s2 = single_set (i2);
2353 if (s1 != 0 && s2 != 0
2354 && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
2356 validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
2357 validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
2358 if (! rtx_renumbered_equal_p (p1, p2))
2359 cancel_changes (0);
2360 else if (apply_change_group ())
2361 goto win;
2365 /* Insns fail to match; cross jumping is limited to the following
2366 insns. */
2368 #ifdef HAVE_cc0
2369 /* Don't allow the insn after a compare to be shared by
2370 cross-jumping unless the compare is also shared.
2371 Here, if either of these non-matching insns is a compare,
2372 exclude the following insn from possible cross-jumping. */
2373 if (sets_cc0_p (p1) || sets_cc0_p (p2))
2374 last1 = afterlast1, last2 = afterlast2, ++minimum;
2375 #endif
2377 /* If cross-jumping here will feed a jump-around-jump
2378 optimization, this jump won't cost extra, so reduce
2379 the minimum. */
2380 if (GET_CODE (i1) == JUMP_INSN
2381 && JUMP_LABEL (i1)
2382 && prev_real_insn (JUMP_LABEL (i1)) == e1)
2383 --minimum;
2384 break;
2387 win:
2388 if (GET_CODE (p1) != USE && GET_CODE (p1) != CLOBBER)
2390 /* Ok, this insn is potentially includable in a cross-jump here. */
2391 afterlast1 = last1, afterlast2 = last2;
2392 last1 = i1, last2 = i2, --minimum;
2396 if (minimum <= 0 && last1 != 0 && last1 != e1)
2397 *f1 = last1, *f2 = last2;
2400 static void
2401 do_cross_jump (insn, newjpos, newlpos)
2402 rtx insn, newjpos, newlpos;
2404 /* Find an existing label at this point
2405 or make a new one if there is none. */
2406 register rtx label = get_label_before (newlpos);
2408 /* Make the same jump insn jump to the new point. */
2409 if (GET_CODE (PATTERN (insn)) == RETURN)
2411 /* Remove from jump chain of returns. */
2412 delete_from_jump_chain (insn);
2413 /* Change the insn. */
2414 PATTERN (insn) = gen_jump (label);
2415 INSN_CODE (insn) = -1;
2416 JUMP_LABEL (insn) = label;
2417 LABEL_NUSES (label)++;
2418 /* Add to new the jump chain. */
2419 if (INSN_UID (label) < max_jump_chain
2420 && INSN_UID (insn) < max_jump_chain)
2422 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (label)];
2423 jump_chain[INSN_UID (label)] = insn;
2426 else
2427 redirect_jump (insn, label);
2429 /* Delete the matching insns before the jump. Also, remove any REG_EQUAL
2430 or REG_EQUIV note in the NEWLPOS stream that isn't also present in
2431 the NEWJPOS stream. */
2433 while (newjpos != insn)
2435 rtx lnote;
2437 for (lnote = REG_NOTES (newlpos); lnote; lnote = XEXP (lnote, 1))
2438 if ((REG_NOTE_KIND (lnote) == REG_EQUAL
2439 || REG_NOTE_KIND (lnote) == REG_EQUIV)
2440 && ! find_reg_note (newjpos, REG_EQUAL, XEXP (lnote, 0))
2441 && ! find_reg_note (newjpos, REG_EQUIV, XEXP (lnote, 0)))
2442 remove_note (newlpos, lnote);
2444 delete_insn (newjpos);
2445 newjpos = next_real_insn (newjpos);
2446 newlpos = next_real_insn (newlpos);
2450 /* Return the label before INSN, or put a new label there. */
2453 get_label_before (insn)
2454 rtx insn;
2456 rtx label;
2458 /* Find an existing label at this point
2459 or make a new one if there is none. */
2460 label = prev_nonnote_insn (insn);
2462 if (label == 0 || GET_CODE (label) != CODE_LABEL)
2464 rtx prev = PREV_INSN (insn);
2466 label = gen_label_rtx ();
2467 emit_label_after (label, prev);
2468 LABEL_NUSES (label) = 0;
2470 return label;
2473 /* Return the label after INSN, or put a new label there. */
2476 get_label_after (insn)
2477 rtx insn;
2479 rtx label;
2481 /* Find an existing label at this point
2482 or make a new one if there is none. */
2483 label = next_nonnote_insn (insn);
2485 if (label == 0 || GET_CODE (label) != CODE_LABEL)
2487 label = gen_label_rtx ();
2488 emit_label_after (label, insn);
2489 LABEL_NUSES (label) = 0;
2491 return label;
2494 /* Return 1 if INSN is a jump that jumps to right after TARGET
2495 only on the condition that TARGET itself would drop through.
2496 Assumes that TARGET is a conditional jump. */
2498 static int
2499 jump_back_p (insn, target)
2500 rtx insn, target;
2502 rtx cinsn, ctarget;
2503 enum rtx_code codei, codet;
2505 if (simplejump_p (insn) || ! condjump_p (insn)
2506 || simplejump_p (target)
2507 || target != prev_real_insn (JUMP_LABEL (insn)))
2508 return 0;
2510 cinsn = XEXP (SET_SRC (PATTERN (insn)), 0);
2511 ctarget = XEXP (SET_SRC (PATTERN (target)), 0);
2513 codei = GET_CODE (cinsn);
2514 codet = GET_CODE (ctarget);
2516 if (XEXP (SET_SRC (PATTERN (insn)), 1) == pc_rtx)
2518 if (! can_reverse_comparison_p (cinsn, insn))
2519 return 0;
2520 codei = reverse_condition (codei);
2523 if (XEXP (SET_SRC (PATTERN (target)), 2) == pc_rtx)
2525 if (! can_reverse_comparison_p (ctarget, target))
2526 return 0;
2527 codet = reverse_condition (codet);
2530 return (codei == codet
2531 && rtx_renumbered_equal_p (XEXP (cinsn, 0), XEXP (ctarget, 0))
2532 && rtx_renumbered_equal_p (XEXP (cinsn, 1), XEXP (ctarget, 1)));
2535 /* Given a comparison, COMPARISON, inside a conditional jump insn, INSN,
2536 return non-zero if it is safe to reverse this comparison. It is if our
2537 floating-point is not IEEE, if this is an NE or EQ comparison, or if
2538 this is known to be an integer comparison. */
2541 can_reverse_comparison_p (comparison, insn)
2542 rtx comparison;
2543 rtx insn;
2545 rtx arg0;
2547 /* If this is not actually a comparison, we can't reverse it. */
2548 if (GET_RTX_CLASS (GET_CODE (comparison)) != '<')
2549 return 0;
2551 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
2552 /* If this is an NE comparison, it is safe to reverse it to an EQ
2553 comparison and vice versa, even for floating point. If no operands
2554 are NaNs, the reversal is valid. If some operand is a NaN, EQ is
2555 always false and NE is always true, so the reversal is also valid. */
2556 || flag_fast_math
2557 || GET_CODE (comparison) == NE
2558 || GET_CODE (comparison) == EQ)
2559 return 1;
2561 arg0 = XEXP (comparison, 0);
2563 /* Make sure ARG0 is one of the actual objects being compared. If we
2564 can't do this, we can't be sure the comparison can be reversed.
2566 Handle cc0 and a MODE_CC register. */
2567 if ((GET_CODE (arg0) == REG && GET_MODE_CLASS (GET_MODE (arg0)) == MODE_CC)
2568 #ifdef HAVE_cc0
2569 || arg0 == cc0_rtx
2570 #endif
2573 rtx prev = prev_nonnote_insn (insn);
2574 rtx set = single_set (prev);
2576 if (set == 0 || SET_DEST (set) != arg0)
2577 return 0;
2579 arg0 = SET_SRC (set);
2581 if (GET_CODE (arg0) == COMPARE)
2582 arg0 = XEXP (arg0, 0);
2585 /* We can reverse this if ARG0 is a CONST_INT or if its mode is
2586 not VOIDmode and neither a MODE_CC nor MODE_FLOAT type. */
2587 return (GET_CODE (arg0) == CONST_INT
2588 || (GET_MODE (arg0) != VOIDmode
2589 && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_CC
2590 && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_FLOAT));
2593 /* Given an rtx-code for a comparison, return the code
2594 for the negated comparison.
2595 WATCH OUT! reverse_condition is not safe to use on a jump
2596 that might be acting on the results of an IEEE floating point comparison,
2597 because of the special treatment of non-signaling nans in comparisons.
2598 Use can_reverse_comparison_p to be sure. */
2600 enum rtx_code
2601 reverse_condition (code)
2602 enum rtx_code code;
2604 switch (code)
2606 case EQ:
2607 return NE;
2609 case NE:
2610 return EQ;
2612 case GT:
2613 return LE;
2615 case GE:
2616 return LT;
2618 case LT:
2619 return GE;
2621 case LE:
2622 return GT;
2624 case GTU:
2625 return LEU;
2627 case GEU:
2628 return LTU;
2630 case LTU:
2631 return GEU;
2633 case LEU:
2634 return GTU;
2636 default:
2637 abort ();
2638 return UNKNOWN;
2642 /* Similar, but return the code when two operands of a comparison are swapped.
2643 This IS safe for IEEE floating-point. */
2645 enum rtx_code
2646 swap_condition (code)
2647 enum rtx_code code;
2649 switch (code)
2651 case EQ:
2652 case NE:
2653 return code;
2655 case GT:
2656 return LT;
2658 case GE:
2659 return LE;
2661 case LT:
2662 return GT;
2664 case LE:
2665 return GE;
2667 case GTU:
2668 return LTU;
2670 case GEU:
2671 return LEU;
2673 case LTU:
2674 return GTU;
2676 case LEU:
2677 return GEU;
2679 default:
2680 abort ();
2681 return UNKNOWN;
2685 /* Given a comparison CODE, return the corresponding unsigned comparison.
2686 If CODE is an equality comparison or already an unsigned comparison,
2687 CODE is returned. */
2689 enum rtx_code
2690 unsigned_condition (code)
2691 enum rtx_code code;
2693 switch (code)
2695 case EQ:
2696 case NE:
2697 case GTU:
2698 case GEU:
2699 case LTU:
2700 case LEU:
2701 return code;
2703 case GT:
2704 return GTU;
2706 case GE:
2707 return GEU;
2709 case LT:
2710 return LTU;
2712 case LE:
2713 return LEU;
2715 default:
2716 abort ();
2720 /* Similarly, return the signed version of a comparison. */
2722 enum rtx_code
2723 signed_condition (code)
2724 enum rtx_code code;
2726 switch (code)
2728 case EQ:
2729 case NE:
2730 case GT:
2731 case GE:
2732 case LT:
2733 case LE:
2734 return code;
2736 case GTU:
2737 return GT;
2739 case GEU:
2740 return GE;
2742 case LTU:
2743 return LT;
2745 case LEU:
2746 return LE;
2748 default:
2749 abort ();
2753 /* Return non-zero if CODE1 is more strict than CODE2, i.e., if the
2754 truth of CODE1 implies the truth of CODE2. */
2757 comparison_dominates_p (code1, code2)
2758 enum rtx_code code1, code2;
2760 if (code1 == code2)
2761 return 1;
2763 switch (code1)
2765 case EQ:
2766 if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU)
2767 return 1;
2768 break;
2770 case LT:
2771 if (code2 == LE || code2 == NE)
2772 return 1;
2773 break;
2775 case GT:
2776 if (code2 == GE || code2 == NE)
2777 return 1;
2778 break;
2780 case LTU:
2781 if (code2 == LEU || code2 == NE)
2782 return 1;
2783 break;
2785 case GTU:
2786 if (code2 == GEU || code2 == NE)
2787 return 1;
2788 break;
2791 return 0;
2794 /* Return 1 if INSN is an unconditional jump and nothing else. */
2797 simplejump_p (insn)
2798 rtx insn;
2800 return (GET_CODE (insn) == JUMP_INSN
2801 && GET_CODE (PATTERN (insn)) == SET
2802 && GET_CODE (SET_DEST (PATTERN (insn))) == PC
2803 && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
2806 /* Return nonzero if INSN is a (possibly) conditional jump
2807 and nothing more. */
2810 condjump_p (insn)
2811 rtx insn;
2813 register rtx x = PATTERN (insn);
2814 if (GET_CODE (x) != SET)
2815 return 0;
2816 if (GET_CODE (SET_DEST (x)) != PC)
2817 return 0;
2818 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
2819 return 1;
2820 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
2821 return 0;
2822 if (XEXP (SET_SRC (x), 2) == pc_rtx
2823 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
2824 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
2825 return 1;
2826 if (XEXP (SET_SRC (x), 1) == pc_rtx
2827 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
2828 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
2829 return 1;
2830 return 0;
2833 /* Return 1 if X is an RTX that does nothing but set the condition codes
2834 and CLOBBER or USE registers.
2835 Return -1 if X does explicitly set the condition codes,
2836 but also does other things. */
2839 sets_cc0_p (x)
2840 rtx x;
2842 #ifdef HAVE_cc0
2843 if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
2844 return 1;
2845 if (GET_CODE (x) == PARALLEL)
2847 int i;
2848 int sets_cc0 = 0;
2849 int other_things = 0;
2850 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
2852 if (GET_CODE (XVECEXP (x, 0, i)) == SET
2853 && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
2854 sets_cc0 = 1;
2855 else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
2856 other_things = 1;
2858 return ! sets_cc0 ? 0 : other_things ? -1 : 1;
2860 return 0;
2861 #else
2862 abort ();
2863 #endif
2866 /* Follow any unconditional jump at LABEL;
2867 return the ultimate label reached by any such chain of jumps.
2868 If LABEL is not followed by a jump, return LABEL.
2869 If the chain loops or we can't find end, return LABEL,
2870 since that tells caller to avoid changing the insn.
2872 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
2873 a USE or CLOBBER. */
2876 follow_jumps (label)
2877 rtx label;
2879 register rtx insn;
2880 register rtx next;
2881 register rtx value = label;
2882 register int depth;
2884 for (depth = 0;
2885 (depth < 10
2886 && (insn = next_active_insn (value)) != 0
2887 && GET_CODE (insn) == JUMP_INSN
2888 && (JUMP_LABEL (insn) != 0 || GET_CODE (PATTERN (insn)) == RETURN)
2889 && (next = NEXT_INSN (insn))
2890 && GET_CODE (next) == BARRIER);
2891 depth++)
2893 /* Don't chain through the insn that jumps into a loop
2894 from outside the loop,
2895 since that would create multiple loop entry jumps
2896 and prevent loop optimization. */
2897 rtx tem;
2898 if (!reload_completed)
2899 for (tem = value; tem != insn; tem = NEXT_INSN (tem))
2900 if (GET_CODE (tem) == NOTE
2901 && NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG)
2902 return value;
2904 /* If we have found a cycle, make the insn jump to itself. */
2905 if (JUMP_LABEL (insn) == label)
2906 return label;
2908 tem = next_active_insn (JUMP_LABEL (insn));
2909 if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
2910 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
2911 break;
2913 value = JUMP_LABEL (insn);
2915 if (depth == 10)
2916 return label;
2917 return value;
2920 /* Assuming that field IDX of X is a vector of label_refs,
2921 replace each of them by the ultimate label reached by it.
2922 Return nonzero if a change is made.
2923 If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG. */
2925 static int
2926 tension_vector_labels (x, idx)
2927 register rtx x;
2928 register int idx;
2930 int changed = 0;
2931 register int i;
2932 for (i = XVECLEN (x, idx) - 1; i >= 0; i--)
2934 register rtx olabel = XEXP (XVECEXP (x, idx, i), 0);
2935 register rtx nlabel = follow_jumps (olabel);
2936 if (nlabel && nlabel != olabel)
2938 XEXP (XVECEXP (x, idx, i), 0) = nlabel;
2939 ++LABEL_NUSES (nlabel);
2940 if (--LABEL_NUSES (olabel) == 0)
2941 delete_insn (olabel);
2942 changed = 1;
2945 return changed;
2948 /* Find all CODE_LABELs referred to in X, and increment their use counts.
2949 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
2950 in INSN, then store one of them in JUMP_LABEL (INSN).
2951 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
2952 referenced in INSN, add a REG_LABEL note containing that label to INSN.
2953 Also, when there are consecutive labels, canonicalize on the last of them.
2955 Note that two labels separated by a loop-beginning note
2956 must be kept distinct if we have not yet done loop-optimization,
2957 because the gap between them is where loop-optimize
2958 will want to move invariant code to. CROSS_JUMP tells us
2959 that loop-optimization is done with.
2961 Once reload has completed (CROSS_JUMP non-zero), we need not consider
2962 two labels distinct if they are separated by only USE or CLOBBER insns. */
2964 static void
2965 mark_jump_label (x, insn, cross_jump)
2966 register rtx x;
2967 rtx insn;
2968 int cross_jump;
2970 register RTX_CODE code = GET_CODE (x);
2971 register int i;
2972 register char *fmt;
2974 switch (code)
2976 case PC:
2977 case CC0:
2978 case REG:
2979 case SUBREG:
2980 case CONST_INT:
2981 case SYMBOL_REF:
2982 case CONST_DOUBLE:
2983 case CLOBBER:
2984 case CALL:
2985 return;
2987 case MEM:
2988 /* If this is a constant-pool reference, see if it is a label. */
2989 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2990 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
2991 mark_jump_label (get_pool_constant (XEXP (x, 0)), insn, cross_jump);
2992 break;
2994 case LABEL_REF:
2996 rtx label = XEXP (x, 0);
2997 rtx olabel = label;
2998 rtx note;
2999 rtx next;
3001 if (GET_CODE (label) != CODE_LABEL)
3002 abort ();
3004 /* Ignore references to labels of containing functions. */
3005 if (LABEL_REF_NONLOCAL_P (x))
3006 break;
3008 /* If there are other labels following this one,
3009 replace it with the last of the consecutive labels. */
3010 for (next = NEXT_INSN (label); next; next = NEXT_INSN (next))
3012 if (GET_CODE (next) == CODE_LABEL)
3013 label = next;
3014 else if (cross_jump && GET_CODE (next) == INSN
3015 && (GET_CODE (PATTERN (next)) == USE
3016 || GET_CODE (PATTERN (next)) == CLOBBER))
3017 continue;
3018 else if (GET_CODE (next) != NOTE)
3019 break;
3020 else if (! cross_jump
3021 && (NOTE_LINE_NUMBER (next) == NOTE_INSN_LOOP_BEG
3022 || NOTE_LINE_NUMBER (next) == NOTE_INSN_FUNCTION_END))
3023 break;
3026 XEXP (x, 0) = label;
3027 ++LABEL_NUSES (label);
3029 if (insn)
3031 if (GET_CODE (insn) == JUMP_INSN)
3032 JUMP_LABEL (insn) = label;
3034 /* If we've changed OLABEL and we had a REG_LABEL note
3035 for it, update it as well. */
3036 else if (label != olabel
3037 && (note = find_reg_note (insn, REG_LABEL, olabel)) != 0)
3038 XEXP (note, 0) = label;
3040 /* Otherwise, add a REG_LABEL note for LABEL unless there already
3041 is one. */
3042 else if (! find_reg_note (insn, REG_LABEL, label))
3044 rtx next = next_real_insn (label);
3045 /* Don't record labels that refer to dispatch tables.
3046 This is not necessary, since the tablejump
3047 references the same label.
3048 And if we did record them, flow.c would make worse code. */
3049 if (next == 0
3050 || ! (GET_CODE (next) == JUMP_INSN
3051 && (GET_CODE (PATTERN (next)) == ADDR_VEC
3052 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC)))
3053 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, label,
3054 REG_NOTES (insn));
3057 return;
3060 /* Do walk the labels in a vector, but not the first operand of an
3061 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
3062 case ADDR_VEC:
3063 case ADDR_DIFF_VEC:
3065 int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
3067 for (i = 0; i < XVECLEN (x, eltnum); i++)
3068 mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, cross_jump);
3069 return;
3073 fmt = GET_RTX_FORMAT (code);
3074 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3076 if (fmt[i] == 'e')
3077 mark_jump_label (XEXP (x, i), insn, cross_jump);
3078 else if (fmt[i] == 'E')
3080 register int j;
3081 for (j = 0; j < XVECLEN (x, i); j++)
3082 mark_jump_label (XVECEXP (x, i, j), insn, cross_jump);
3087 /* If all INSN does is set the pc, delete it,
3088 and delete the insn that set the condition codes for it
3089 if that's what the previous thing was. */
3091 void
3092 delete_jump (insn)
3093 rtx insn;
3095 register rtx set = single_set (insn);
3097 if (set && GET_CODE (SET_DEST (set)) == PC)
3098 delete_computation (insn);
3101 /* Delete INSN and recursively delete insns that compute values used only
3102 by INSN. This uses the REG_DEAD notes computed during flow analysis.
3103 If we are running before flow.c, we need do nothing since flow.c will
3104 delete dead code. We also can't know if the registers being used are
3105 dead or not at this point.
3107 Otherwise, look at all our REG_DEAD notes. If a previous insn does
3108 nothing other than set a register that dies in this insn, we can delete
3109 that insn as well.
3111 On machines with CC0, if CC0 is used in this insn, we may be able to
3112 delete the insn that set it. */
3114 static void
3115 delete_computation (insn)
3116 rtx insn;
3118 rtx note, next;
3120 #ifdef HAVE_cc0
3121 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
3123 rtx prev = prev_nonnote_insn (insn);
3124 /* We assume that at this stage
3125 CC's are always set explicitly
3126 and always immediately before the jump that
3127 will use them. So if the previous insn
3128 exists to set the CC's, delete it
3129 (unless it performs auto-increments, etc.). */
3130 if (prev && GET_CODE (prev) == INSN
3131 && sets_cc0_p (PATTERN (prev)))
3133 if (sets_cc0_p (PATTERN (prev)) > 0
3134 && !FIND_REG_INC_NOTE (prev, NULL_RTX))
3135 delete_computation (prev);
3136 else
3137 /* Otherwise, show that cc0 won't be used. */
3138 REG_NOTES (prev) = gen_rtx (EXPR_LIST, REG_UNUSED,
3139 cc0_rtx, REG_NOTES (prev));
3142 #endif
3144 for (note = REG_NOTES (insn); note; note = next)
3146 rtx our_prev;
3148 next = XEXP (note, 1);
3150 if (REG_NOTE_KIND (note) != REG_DEAD
3151 /* Verify that the REG_NOTE is legitimate. */
3152 || GET_CODE (XEXP (note, 0)) != REG)
3153 continue;
3155 for (our_prev = prev_nonnote_insn (insn);
3156 our_prev && GET_CODE (our_prev) == INSN;
3157 our_prev = prev_nonnote_insn (our_prev))
3159 /* If we reach a SEQUENCE, it is too complex to try to
3160 do anything with it, so give up. */
3161 if (GET_CODE (PATTERN (our_prev)) == SEQUENCE)
3162 break;
3164 if (GET_CODE (PATTERN (our_prev)) == USE
3165 && GET_CODE (XEXP (PATTERN (our_prev), 0)) == INSN)
3166 /* reorg creates USEs that look like this. We leave them
3167 alone because reorg needs them for its own purposes. */
3168 break;
3170 if (reg_set_p (XEXP (note, 0), PATTERN (our_prev)))
3172 if (FIND_REG_INC_NOTE (our_prev, NULL_RTX))
3173 break;
3175 if (GET_CODE (PATTERN (our_prev)) == PARALLEL)
3177 /* If we find a SET of something else, we can't
3178 delete the insn. */
3180 int i;
3182 for (i = 0; i < XVECLEN (PATTERN (our_prev), 0); i++)
3184 rtx part = XVECEXP (PATTERN (our_prev), 0, i);
3186 if (GET_CODE (part) == SET
3187 && SET_DEST (part) != XEXP (note, 0))
3188 break;
3191 if (i == XVECLEN (PATTERN (our_prev), 0))
3192 delete_computation (our_prev);
3194 else if (GET_CODE (PATTERN (our_prev)) == SET
3195 && SET_DEST (PATTERN (our_prev)) == XEXP (note, 0))
3196 delete_computation (our_prev);
3198 break;
3201 /* If OUR_PREV references the register that dies here, it is an
3202 additional use. Hence any prior SET isn't dead. However, this
3203 insn becomes the new place for the REG_DEAD note. */
3204 if (reg_overlap_mentioned_p (XEXP (note, 0),
3205 PATTERN (our_prev)))
3207 XEXP (note, 1) = REG_NOTES (our_prev);
3208 REG_NOTES (our_prev) = note;
3209 break;
3214 delete_insn (insn);
3217 /* Delete insn INSN from the chain of insns and update label ref counts.
3218 May delete some following insns as a consequence; may even delete
3219 a label elsewhere and insns that follow it.
3221 Returns the first insn after INSN that was not deleted. */
3224 delete_insn (insn)
3225 register rtx insn;
3227 register rtx next = NEXT_INSN (insn);
3228 register rtx prev = PREV_INSN (insn);
3229 register int was_code_label = (GET_CODE (insn) == CODE_LABEL);
3230 register int dont_really_delete = 0;
3232 while (next && INSN_DELETED_P (next))
3233 next = NEXT_INSN (next);
3235 /* This insn is already deleted => return first following nondeleted. */
3236 if (INSN_DELETED_P (insn))
3237 return next;
3239 /* Don't delete user-declared labels. Convert them to special NOTEs
3240 instead. */
3241 if (was_code_label && LABEL_NAME (insn) != 0
3242 && optimize && ! dont_really_delete)
3244 PUT_CODE (insn, NOTE);
3245 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
3246 NOTE_SOURCE_FILE (insn) = 0;
3247 dont_really_delete = 1;
3249 else
3250 /* Mark this insn as deleted. */
3251 INSN_DELETED_P (insn) = 1;
3253 /* If this is an unconditional jump, delete it from the jump chain. */
3254 if (simplejump_p (insn))
3255 delete_from_jump_chain (insn);
3257 /* If instruction is followed by a barrier,
3258 delete the barrier too. */
3260 if (next != 0 && GET_CODE (next) == BARRIER)
3262 INSN_DELETED_P (next) = 1;
3263 next = NEXT_INSN (next);
3266 /* Patch out INSN (and the barrier if any) */
3268 if (optimize && ! dont_really_delete)
3270 if (prev)
3272 NEXT_INSN (prev) = next;
3273 if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
3274 NEXT_INSN (XVECEXP (PATTERN (prev), 0,
3275 XVECLEN (PATTERN (prev), 0) - 1)) = next;
3278 if (next)
3280 PREV_INSN (next) = prev;
3281 if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
3282 PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
3285 if (prev && NEXT_INSN (prev) == 0)
3286 set_last_insn (prev);
3289 /* If deleting a jump, decrement the count of the label,
3290 and delete the label if it is now unused. */
3292 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
3293 if (--LABEL_NUSES (JUMP_LABEL (insn)) == 0)
3295 /* This can delete NEXT or PREV,
3296 either directly if NEXT is JUMP_LABEL (INSN),
3297 or indirectly through more levels of jumps. */
3298 delete_insn (JUMP_LABEL (insn));
3299 /* I feel a little doubtful about this loop,
3300 but I see no clean and sure alternative way
3301 to find the first insn after INSN that is not now deleted.
3302 I hope this works. */
3303 while (next && INSN_DELETED_P (next))
3304 next = NEXT_INSN (next);
3305 return next;
3308 while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
3309 prev = PREV_INSN (prev);
3311 /* If INSN was a label and a dispatch table follows it,
3312 delete the dispatch table. The tablejump must have gone already.
3313 It isn't useful to fall through into a table. */
3315 if (was_code_label
3316 && NEXT_INSN (insn) != 0
3317 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
3318 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
3319 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
3320 next = delete_insn (NEXT_INSN (insn));
3322 /* If INSN was a label, delete insns following it if now unreachable. */
3324 if (was_code_label && prev && GET_CODE (prev) == BARRIER)
3326 register RTX_CODE code;
3327 while (next != 0
3328 && (GET_RTX_CLASS (code = GET_CODE (next)) == 'i'
3329 || code == NOTE
3330 || (code == CODE_LABEL && INSN_DELETED_P (next))))
3332 if (code == NOTE
3333 && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
3334 next = NEXT_INSN (next);
3335 /* Keep going past other deleted labels to delete what follows. */
3336 else if (code == CODE_LABEL && INSN_DELETED_P (next))
3337 next = NEXT_INSN (next);
3338 else
3339 /* Note: if this deletes a jump, it can cause more
3340 deletion of unreachable code, after a different label.
3341 As long as the value from this recursive call is correct,
3342 this invocation functions correctly. */
3343 next = delete_insn (next);
3347 return next;
3350 /* Advance from INSN till reaching something not deleted
3351 then return that. May return INSN itself. */
3354 next_nondeleted_insn (insn)
3355 rtx insn;
3357 while (INSN_DELETED_P (insn))
3358 insn = NEXT_INSN (insn);
3359 return insn;
3362 /* Delete a range of insns from FROM to TO, inclusive.
3363 This is for the sake of peephole optimization, so assume
3364 that whatever these insns do will still be done by a new
3365 peephole insn that will replace them. */
3367 void
3368 delete_for_peephole (from, to)
3369 register rtx from, to;
3371 register rtx insn = from;
3373 while (1)
3375 register rtx next = NEXT_INSN (insn);
3376 register rtx prev = PREV_INSN (insn);
3378 if (GET_CODE (insn) != NOTE)
3380 INSN_DELETED_P (insn) = 1;
3382 /* Patch this insn out of the chain. */
3383 /* We don't do this all at once, because we
3384 must preserve all NOTEs. */
3385 if (prev)
3386 NEXT_INSN (prev) = next;
3388 if (next)
3389 PREV_INSN (next) = prev;
3392 if (insn == to)
3393 break;
3394 insn = next;
3397 /* Note that if TO is an unconditional jump
3398 we *do not* delete the BARRIER that follows,
3399 since the peephole that replaces this sequence
3400 is also an unconditional jump in that case. */
3403 /* Invert the condition of the jump JUMP, and make it jump
3404 to label NLABEL instead of where it jumps now. */
3407 invert_jump (jump, nlabel)
3408 rtx jump, nlabel;
3410 /* We have to either invert the condition and change the label or
3411 do neither. Either operation could fail. We first try to invert
3412 the jump. If that succeeds, we try changing the label. If that fails,
3413 we invert the jump back to what it was. */
3415 if (! invert_exp (PATTERN (jump), jump))
3416 return 0;
3418 if (redirect_jump (jump, nlabel))
3419 return 1;
3421 if (! invert_exp (PATTERN (jump), jump))
3422 /* This should just be putting it back the way it was. */
3423 abort ();
3425 return 0;
3428 /* Invert the jump condition of rtx X contained in jump insn, INSN.
3430 Return 1 if we can do so, 0 if we cannot find a way to do so that
3431 matches a pattern. */
3434 invert_exp (x, insn)
3435 rtx x;
3436 rtx insn;
3438 register RTX_CODE code;
3439 register int i;
3440 register char *fmt;
3442 code = GET_CODE (x);
3444 if (code == IF_THEN_ELSE)
3446 register rtx comp = XEXP (x, 0);
3447 register rtx tem;
3449 /* We can do this in two ways: The preferable way, which can only
3450 be done if this is not an integer comparison, is to reverse
3451 the comparison code. Otherwise, swap the THEN-part and ELSE-part
3452 of the IF_THEN_ELSE. If we can't do either, fail. */
3454 if (can_reverse_comparison_p (comp, insn)
3455 && validate_change (insn, &XEXP (x, 0),
3456 gen_rtx (reverse_condition (GET_CODE (comp)),
3457 GET_MODE (comp), XEXP (comp, 0),
3458 XEXP (comp, 1)), 0))
3459 return 1;
3461 tem = XEXP (x, 1);
3462 validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
3463 validate_change (insn, &XEXP (x, 2), tem, 1);
3464 return apply_change_group ();
3467 fmt = GET_RTX_FORMAT (code);
3468 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3470 if (fmt[i] == 'e')
3471 if (! invert_exp (XEXP (x, i), insn))
3472 return 0;
3473 if (fmt[i] == 'E')
3475 register int j;
3476 for (j = 0; j < XVECLEN (x, i); j++)
3477 if (!invert_exp (XVECEXP (x, i, j), insn))
3478 return 0;
3482 return 1;
3485 /* Make jump JUMP jump to label NLABEL instead of where it jumps now.
3486 If the old jump target label is unused as a result,
3487 it and the code following it may be deleted.
3489 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
3490 RETURN insn.
3492 The return value will be 1 if the change was made, 0 if it wasn't (this
3493 can only occur for NLABEL == 0). */
3496 redirect_jump (jump, nlabel)
3497 rtx jump, nlabel;
3499 register rtx olabel = JUMP_LABEL (jump);
3501 if (nlabel == olabel)
3502 return 1;
3504 if (! redirect_exp (&PATTERN (jump), olabel, nlabel, jump))
3505 return 0;
3507 /* If this is an unconditional branch, delete it from the jump_chain of
3508 OLABEL and add it to the jump_chain of NLABEL (assuming both labels
3509 have UID's in range and JUMP_CHAIN is valid). */
3510 if (jump_chain && (simplejump_p (jump)
3511 || GET_CODE (PATTERN (jump)) == RETURN))
3513 int label_index = nlabel ? INSN_UID (nlabel) : 0;
3515 delete_from_jump_chain (jump);
3516 if (label_index < max_jump_chain
3517 && INSN_UID (jump) < max_jump_chain)
3519 jump_chain[INSN_UID (jump)] = jump_chain[label_index];
3520 jump_chain[label_index] = jump;
3524 JUMP_LABEL (jump) = nlabel;
3525 if (nlabel)
3526 ++LABEL_NUSES (nlabel);
3528 if (olabel && --LABEL_NUSES (olabel) == 0)
3529 delete_insn (olabel);
3531 return 1;
3534 /* Delete the instruction JUMP from any jump chain it might be on. */
3536 static void
3537 delete_from_jump_chain (jump)
3538 rtx jump;
3540 int index;
3541 rtx olabel = JUMP_LABEL (jump);
3543 /* Handle unconditional jumps. */
3544 if (jump_chain && olabel != 0
3545 && INSN_UID (olabel) < max_jump_chain
3546 && simplejump_p (jump))
3547 index = INSN_UID (olabel);
3548 /* Handle return insns. */
3549 else if (jump_chain && GET_CODE (PATTERN (jump)) == RETURN)
3550 index = 0;
3551 else return;
3553 if (jump_chain[index] == jump)
3554 jump_chain[index] = jump_chain[INSN_UID (jump)];
3555 else
3557 rtx insn;
3559 for (insn = jump_chain[index];
3560 insn != 0;
3561 insn = jump_chain[INSN_UID (insn)])
3562 if (jump_chain[INSN_UID (insn)] == jump)
3564 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (jump)];
3565 break;
3570 /* If NLABEL is nonzero, throughout the rtx at LOC,
3571 alter (LABEL_REF OLABEL) to (LABEL_REF NLABEL). If OLABEL is
3572 zero, alter (RETURN) to (LABEL_REF NLABEL).
3574 If NLABEL is zero, alter (LABEL_REF OLABEL) to (RETURN) and check
3575 validity with validate_change. Convert (set (pc) (label_ref olabel))
3576 to (return).
3578 Return 0 if we found a change we would like to make but it is invalid.
3579 Otherwise, return 1. */
3582 redirect_exp (loc, olabel, nlabel, insn)
3583 rtx *loc;
3584 rtx olabel, nlabel;
3585 rtx insn;
3587 register rtx x = *loc;
3588 register RTX_CODE code = GET_CODE (x);
3589 register int i;
3590 register char *fmt;
3592 if (code == LABEL_REF)
3594 if (XEXP (x, 0) == olabel)
3596 if (nlabel)
3597 XEXP (x, 0) = nlabel;
3598 else
3599 return validate_change (insn, loc, gen_rtx (RETURN, VOIDmode), 0);
3600 return 1;
3603 else if (code == RETURN && olabel == 0)
3605 x = gen_rtx (LABEL_REF, VOIDmode, nlabel);
3606 if (loc == &PATTERN (insn))
3607 x = gen_rtx (SET, VOIDmode, pc_rtx, x);
3608 return validate_change (insn, loc, x, 0);
3611 if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
3612 && GET_CODE (SET_SRC (x)) == LABEL_REF
3613 && XEXP (SET_SRC (x), 0) == olabel)
3614 return validate_change (insn, loc, gen_rtx (RETURN, VOIDmode), 0);
3616 fmt = GET_RTX_FORMAT (code);
3617 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3619 if (fmt[i] == 'e')
3620 if (! redirect_exp (&XEXP (x, i), olabel, nlabel, insn))
3621 return 0;
3622 if (fmt[i] == 'E')
3624 register int j;
3625 for (j = 0; j < XVECLEN (x, i); j++)
3626 if (! redirect_exp (&XVECEXP (x, i, j), olabel, nlabel, insn))
3627 return 0;
3631 return 1;
3634 /* Make jump JUMP jump to label NLABEL, assuming it used to be a tablejump.
3636 If the old jump target label (before the dispatch table) becomes unused,
3637 it and the dispatch table may be deleted. In that case, find the insn
3638 before the jump references that label and delete it and logical successors
3639 too. */
3641 static void
3642 redirect_tablejump (jump, nlabel)
3643 rtx jump, nlabel;
3645 register rtx olabel = JUMP_LABEL (jump);
3647 /* Add this jump to the jump_chain of NLABEL. */
3648 if (jump_chain && INSN_UID (nlabel) < max_jump_chain
3649 && INSN_UID (jump) < max_jump_chain)
3651 jump_chain[INSN_UID (jump)] = jump_chain[INSN_UID (nlabel)];
3652 jump_chain[INSN_UID (nlabel)] = jump;
3655 PATTERN (jump) = gen_jump (nlabel);
3656 JUMP_LABEL (jump) = nlabel;
3657 ++LABEL_NUSES (nlabel);
3658 INSN_CODE (jump) = -1;
3660 if (--LABEL_NUSES (olabel) == 0)
3662 delete_labelref_insn (jump, olabel, 0);
3663 delete_insn (olabel);
3667 /* Find the insn referencing LABEL that is a logical predecessor of INSN.
3668 If we found one, delete it and then delete this insn if DELETE_THIS is
3669 non-zero. Return non-zero if INSN or a predecessor references LABEL. */
3671 static int
3672 delete_labelref_insn (insn, label, delete_this)
3673 rtx insn, label;
3674 int delete_this;
3676 int deleted = 0;
3677 rtx link;
3679 if (GET_CODE (insn) != NOTE
3680 && reg_mentioned_p (label, PATTERN (insn)))
3682 if (delete_this)
3684 delete_insn (insn);
3685 deleted = 1;
3687 else
3688 return 1;
3691 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
3692 if (delete_labelref_insn (XEXP (link, 0), label, 1))
3694 if (delete_this)
3696 delete_insn (insn);
3697 deleted = 1;
3699 else
3700 return 1;
3703 return deleted;
3706 /* Like rtx_equal_p except that it considers two REGs as equal
3707 if they renumber to the same value and considers two commutative
3708 operations to be the same if the order of the operands has been
3709 reversed. */
3712 rtx_renumbered_equal_p (x, y)
3713 rtx x, y;
3715 register int i;
3716 register RTX_CODE code = GET_CODE (x);
3717 register char *fmt;
3719 if (x == y)
3720 return 1;
3722 if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
3723 && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
3724 && GET_CODE (SUBREG_REG (y)) == REG)))
3726 int reg_x = -1, reg_y = -1;
3727 int word_x = 0, word_y = 0;
3729 if (GET_MODE (x) != GET_MODE (y))
3730 return 0;
3732 /* If we haven't done any renumbering, don't
3733 make any assumptions. */
3734 if (reg_renumber == 0)
3735 return rtx_equal_p (x, y);
3737 if (code == SUBREG)
3739 reg_x = REGNO (SUBREG_REG (x));
3740 word_x = SUBREG_WORD (x);
3742 if (reg_renumber[reg_x] >= 0)
3744 reg_x = reg_renumber[reg_x] + word_x;
3745 word_x = 0;
3749 else
3751 reg_x = REGNO (x);
3752 if (reg_renumber[reg_x] >= 0)
3753 reg_x = reg_renumber[reg_x];
3756 if (GET_CODE (y) == SUBREG)
3758 reg_y = REGNO (SUBREG_REG (y));
3759 word_y = SUBREG_WORD (y);
3761 if (reg_renumber[reg_y] >= 0)
3763 reg_y = reg_renumber[reg_y];
3764 word_y = 0;
3768 else
3770 reg_y = REGNO (y);
3771 if (reg_renumber[reg_y] >= 0)
3772 reg_y = reg_renumber[reg_y];
3775 return reg_x >= 0 && reg_x == reg_y && word_x == word_y;
3778 /* Now we have disposed of all the cases
3779 in which different rtx codes can match. */
3780 if (code != GET_CODE (y))
3781 return 0;
3783 switch (code)
3785 case PC:
3786 case CC0:
3787 case ADDR_VEC:
3788 case ADDR_DIFF_VEC:
3789 return 0;
3791 case CONST_INT:
3792 return INTVAL (x) == INTVAL (y);
3794 case LABEL_REF:
3795 /* We can't assume nonlocal labels have their following insns yet. */
3796 if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
3797 return XEXP (x, 0) == XEXP (y, 0);
3799 /* Two label-refs are equivalent if they point at labels
3800 in the same position in the instruction stream. */
3801 return (next_real_insn (XEXP (x, 0))
3802 == next_real_insn (XEXP (y, 0)));
3804 case SYMBOL_REF:
3805 return XSTR (x, 0) == XSTR (y, 0);
3808 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
3810 if (GET_MODE (x) != GET_MODE (y))
3811 return 0;
3813 /* For commutative operations, the RTX match if the operand match in any
3814 order. Also handle the simple binary and unary cases without a loop. */
3815 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
3816 return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
3817 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
3818 || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
3819 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
3820 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
3821 return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
3822 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
3823 else if (GET_RTX_CLASS (code) == '1')
3824 return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
3826 /* Compare the elements. If any pair of corresponding elements
3827 fail to match, return 0 for the whole things. */
3829 fmt = GET_RTX_FORMAT (code);
3830 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3832 register int j;
3833 switch (fmt[i])
3835 case 'w':
3836 if (XWINT (x, i) != XWINT (y, i))
3837 return 0;
3838 break;
3840 case 'i':
3841 if (XINT (x, i) != XINT (y, i))
3842 return 0;
3843 break;
3845 case 's':
3846 if (strcmp (XSTR (x, i), XSTR (y, i)))
3847 return 0;
3848 break;
3850 case 'e':
3851 if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
3852 return 0;
3853 break;
3855 case 'u':
3856 if (XEXP (x, i) != XEXP (y, i))
3857 return 0;
3858 /* fall through. */
3859 case '0':
3860 break;
3862 case 'E':
3863 if (XVECLEN (x, i) != XVECLEN (y, i))
3864 return 0;
3865 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3866 if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
3867 return 0;
3868 break;
3870 default:
3871 abort ();
3874 return 1;
3877 /* If X is a hard register or equivalent to one or a subregister of one,
3878 return the hard register number. If X is a pseudo register that was not
3879 assigned a hard register, return the pseudo register number. Otherwise,
3880 return -1. Any rtx is valid for X. */
3883 true_regnum (x)
3884 rtx x;
3886 if (GET_CODE (x) == REG)
3888 if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
3889 return reg_renumber[REGNO (x)];
3890 return REGNO (x);
3892 if (GET_CODE (x) == SUBREG)
3894 int base = true_regnum (SUBREG_REG (x));
3895 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
3896 return SUBREG_WORD (x) + base;
3898 return -1;
3901 /* Optimize code of the form:
3903 for (x = a[i]; x; ...)
3905 for (x = a[i]; x; ...)
3907 foo:
3909 Loop optimize will change the above code into
3911 if (x = a[i])
3912 for (;;)
3913 { ...; if (! (x = ...)) break; }
3914 if (x = a[i])
3915 for (;;)
3916 { ...; if (! (x = ...)) break; }
3917 foo:
3919 In general, if the first test fails, the program can branch
3920 directly to `foo' and skip the second try which is doomed to fail.
3921 We run this after loop optimization and before flow analysis. */
3923 /* When comparing the insn patterns, we track the fact that different
3924 pseudo-register numbers may have been used in each computation.
3925 The following array stores an equivalence -- same_regs[I] == J means
3926 that pseudo register I was used in the first set of tests in a context
3927 where J was used in the second set. We also count the number of such
3928 pending equivalences. If nonzero, the expressions really aren't the
3929 same. */
3931 static int *same_regs;
3933 static int num_same_regs;
3935 /* Track any registers modified between the target of the first jump and
3936 the second jump. They never compare equal. */
3938 static char *modified_regs;
3940 /* Record if memory was modified. */
3942 static int modified_mem;
3944 /* Called via note_stores on each insn between the target of the first
3945 branch and the second branch. It marks any changed registers. */
3947 static void
3948 mark_modified_reg (dest, x)
3949 rtx dest;
3950 rtx x;
3952 int regno, i;
3954 if (GET_CODE (dest) == SUBREG)
3955 dest = SUBREG_REG (dest);
3957 if (GET_CODE (dest) == MEM)
3958 modified_mem = 1;
3960 if (GET_CODE (dest) != REG)
3961 return;
3963 regno = REGNO (dest);
3964 if (regno >= FIRST_PSEUDO_REGISTER)
3965 modified_regs[regno] = 1;
3966 else
3967 for (i = 0; i < HARD_REGNO_NREGS (regno, GET_MODE (dest)); i++)
3968 modified_regs[regno + i] = 1;
3971 /* F is the first insn in the chain of insns. */
3973 void
3974 thread_jumps (f, max_reg, flag_before_loop)
3975 rtx f;
3976 int max_reg;
3977 int flag_before_loop;
3979 /* Basic algorithm is to find a conditional branch,
3980 the label it may branch to, and the branch after
3981 that label. If the two branches test the same condition,
3982 walk back from both branch paths until the insn patterns
3983 differ, or code labels are hit. If we make it back to
3984 the target of the first branch, then we know that the first branch
3985 will either always succeed or always fail depending on the relative
3986 senses of the two branches. So adjust the first branch accordingly
3987 in this case. */
3989 rtx label, b1, b2, t1, t2;
3990 enum rtx_code code1, code2;
3991 rtx b1op0, b1op1, b2op0, b2op1;
3992 int changed = 1;
3993 int i;
3994 int *all_reset;
3996 /* Allocate register tables and quick-reset table. */
3997 modified_regs = (char *) alloca (max_reg * sizeof (char));
3998 same_regs = (int *) alloca (max_reg * sizeof (int));
3999 all_reset = (int *) alloca (max_reg * sizeof (int));
4000 for (i = 0; i < max_reg; i++)
4001 all_reset[i] = -1;
4003 while (changed)
4005 changed = 0;
4007 for (b1 = f; b1; b1 = NEXT_INSN (b1))
4009 /* Get to a candidate branch insn. */
4010 if (GET_CODE (b1) != JUMP_INSN
4011 || ! condjump_p (b1) || simplejump_p (b1)
4012 || JUMP_LABEL (b1) == 0)
4013 continue;
4015 bzero (modified_regs, max_reg * sizeof (char));
4016 modified_mem = 0;
4018 bcopy (all_reset, same_regs, max_reg * sizeof (int));
4019 num_same_regs = 0;
4021 label = JUMP_LABEL (b1);
4023 /* Look for a branch after the target. Record any registers and
4024 memory modified between the target and the branch. Stop when we
4025 get to a label since we can't know what was changed there. */
4026 for (b2 = NEXT_INSN (label); b2; b2 = NEXT_INSN (b2))
4028 if (GET_CODE (b2) == CODE_LABEL)
4029 break;
4031 else if (GET_CODE (b2) == JUMP_INSN)
4033 /* If this is an unconditional jump and is the only use of
4034 its target label, we can follow it. */
4035 if (simplejump_p (b2)
4036 && JUMP_LABEL (b2) != 0
4037 && LABEL_NUSES (JUMP_LABEL (b2)) == 1)
4039 b2 = JUMP_LABEL (b2);
4040 continue;
4042 else
4043 break;
4046 if (GET_CODE (b2) != CALL_INSN && GET_CODE (b2) != INSN)
4047 continue;
4049 if (GET_CODE (b2) == CALL_INSN)
4051 modified_mem = 1;
4052 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4053 if (call_used_regs[i] && ! fixed_regs[i]
4054 && i != STACK_POINTER_REGNUM
4055 && i != FRAME_POINTER_REGNUM
4056 && i != HARD_FRAME_POINTER_REGNUM
4057 && i != ARG_POINTER_REGNUM)
4058 modified_regs[i] = 1;
4061 note_stores (PATTERN (b2), mark_modified_reg);
4064 /* Check the next candidate branch insn from the label
4065 of the first. */
4066 if (b2 == 0
4067 || GET_CODE (b2) != JUMP_INSN
4068 || b2 == b1
4069 || ! condjump_p (b2)
4070 || simplejump_p (b2))
4071 continue;
4073 /* Get the comparison codes and operands, reversing the
4074 codes if appropriate. If we don't have comparison codes,
4075 we can't do anything. */
4076 b1op0 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 0);
4077 b1op1 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 1);
4078 code1 = GET_CODE (XEXP (SET_SRC (PATTERN (b1)), 0));
4079 if (XEXP (SET_SRC (PATTERN (b1)), 1) == pc_rtx)
4080 code1 = reverse_condition (code1);
4082 b2op0 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 0);
4083 b2op1 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 1);
4084 code2 = GET_CODE (XEXP (SET_SRC (PATTERN (b2)), 0));
4085 if (XEXP (SET_SRC (PATTERN (b2)), 1) == pc_rtx)
4086 code2 = reverse_condition (code2);
4088 /* If they test the same things and knowing that B1 branches
4089 tells us whether or not B2 branches, check if we
4090 can thread the branch. */
4091 if (rtx_equal_for_thread_p (b1op0, b2op0, b2)
4092 && rtx_equal_for_thread_p (b1op1, b2op1, b2)
4093 && (comparison_dominates_p (code1, code2)
4094 || comparison_dominates_p (code1, reverse_condition (code2))))
4096 t1 = prev_nonnote_insn (b1);
4097 t2 = prev_nonnote_insn (b2);
4099 while (t1 != 0 && t2 != 0)
4101 if (t2 == label)
4103 /* We have reached the target of the first branch.
4104 If there are no pending register equivalents,
4105 we know that this branch will either always
4106 succeed (if the senses of the two branches are
4107 the same) or always fail (if not). */
4108 rtx new_label;
4110 if (num_same_regs != 0)
4111 break;
4113 if (comparison_dominates_p (code1, code2))
4114 new_label = JUMP_LABEL (b2);
4115 else
4116 new_label = get_label_after (b2);
4118 if (JUMP_LABEL (b1) != new_label)
4120 rtx prev = PREV_INSN (new_label);
4122 if (flag_before_loop
4123 && NOTE_LINE_NUMBER (prev) == NOTE_INSN_LOOP_BEG)
4125 /* Don't thread to the loop label. If a loop
4126 label is reused, loop optimization will
4127 be disabled for that loop. */
4128 new_label = gen_label_rtx ();
4129 emit_label_after (new_label, PREV_INSN (prev));
4131 changed |= redirect_jump (b1, new_label);
4133 break;
4136 /* If either of these is not a normal insn (it might be
4137 a JUMP_INSN, CALL_INSN, or CODE_LABEL) we fail. (NOTEs
4138 have already been skipped above.) Similarly, fail
4139 if the insns are different. */
4140 if (GET_CODE (t1) != INSN || GET_CODE (t2) != INSN
4141 || recog_memoized (t1) != recog_memoized (t2)
4142 || ! rtx_equal_for_thread_p (PATTERN (t1),
4143 PATTERN (t2), t2))
4144 break;
4146 t1 = prev_nonnote_insn (t1);
4147 t2 = prev_nonnote_insn (t2);
4154 /* This is like RTX_EQUAL_P except that it knows about our handling of
4155 possibly equivalent registers and knows to consider volatile and
4156 modified objects as not equal.
4158 YINSN is the insn containing Y. */
4161 rtx_equal_for_thread_p (x, y, yinsn)
4162 rtx x, y;
4163 rtx yinsn;
4165 register int i;
4166 register int j;
4167 register enum rtx_code code;
4168 register char *fmt;
4170 code = GET_CODE (x);
4171 /* Rtx's of different codes cannot be equal. */
4172 if (code != GET_CODE (y))
4173 return 0;
4175 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
4176 (REG:SI x) and (REG:HI x) are NOT equivalent. */
4178 if (GET_MODE (x) != GET_MODE (y))
4179 return 0;
4181 /* For commutative operations, the RTX match if the operand match in any
4182 order. Also handle the simple binary and unary cases without a loop. */
4183 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
4184 return ((rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4185 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn))
4186 || (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 1), yinsn)
4187 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 0), yinsn)));
4188 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
4189 return (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4190 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn));
4191 else if (GET_RTX_CLASS (code) == '1')
4192 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4194 /* Handle special-cases first. */
4195 switch (code)
4197 case REG:
4198 if (REGNO (x) == REGNO (y) && ! modified_regs[REGNO (x)])
4199 return 1;
4201 /* If neither is user variable or hard register, check for possible
4202 equivalence. */
4203 if (REG_USERVAR_P (x) || REG_USERVAR_P (y)
4204 || REGNO (x) < FIRST_PSEUDO_REGISTER
4205 || REGNO (y) < FIRST_PSEUDO_REGISTER)
4206 return 0;
4208 if (same_regs[REGNO (x)] == -1)
4210 same_regs[REGNO (x)] = REGNO (y);
4211 num_same_regs++;
4213 /* If this is the first time we are seeing a register on the `Y'
4214 side, see if it is the last use. If not, we can't thread the
4215 jump, so mark it as not equivalent. */
4216 if (regno_last_uid[REGNO (y)] != INSN_UID (yinsn))
4217 return 0;
4219 return 1;
4221 else
4222 return (same_regs[REGNO (x)] == REGNO (y));
4224 break;
4226 case MEM:
4227 /* If memory modified or either volatile, not equivalent.
4228 Else, check address. */
4229 if (modified_mem || MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4230 return 0;
4232 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4234 case ASM_INPUT:
4235 if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4236 return 0;
4238 break;
4240 case SET:
4241 /* Cancel a pending `same_regs' if setting equivalenced registers.
4242 Then process source. */
4243 if (GET_CODE (SET_DEST (x)) == REG
4244 && GET_CODE (SET_DEST (y)) == REG)
4246 if (same_regs[REGNO (SET_DEST (x))] == REGNO (SET_DEST (y)))
4248 same_regs[REGNO (SET_DEST (x))] = -1;
4249 num_same_regs--;
4251 else if (REGNO (SET_DEST (x)) != REGNO (SET_DEST (y)))
4252 return 0;
4254 else
4255 if (rtx_equal_for_thread_p (SET_DEST (x), SET_DEST (y), yinsn) == 0)
4256 return 0;
4258 return rtx_equal_for_thread_p (SET_SRC (x), SET_SRC (y), yinsn);
4260 case LABEL_REF:
4261 return XEXP (x, 0) == XEXP (y, 0);
4263 case SYMBOL_REF:
4264 return XSTR (x, 0) == XSTR (y, 0);
4267 if (x == y)
4268 return 1;
4270 fmt = GET_RTX_FORMAT (code);
4271 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4273 switch (fmt[i])
4275 case 'w':
4276 if (XWINT (x, i) != XWINT (y, i))
4277 return 0;
4278 break;
4280 case 'n':
4281 case 'i':
4282 if (XINT (x, i) != XINT (y, i))
4283 return 0;
4284 break;
4286 case 'V':
4287 case 'E':
4288 /* Two vectors must have the same length. */
4289 if (XVECLEN (x, i) != XVECLEN (y, i))
4290 return 0;
4292 /* And the corresponding elements must match. */
4293 for (j = 0; j < XVECLEN (x, i); j++)
4294 if (rtx_equal_for_thread_p (XVECEXP (x, i, j),
4295 XVECEXP (y, i, j), yinsn) == 0)
4296 return 0;
4297 break;
4299 case 'e':
4300 if (rtx_equal_for_thread_p (XEXP (x, i), XEXP (y, i), yinsn) == 0)
4301 return 0;
4302 break;
4304 case 'S':
4305 case 's':
4306 if (strcmp (XSTR (x, i), XSTR (y, i)))
4307 return 0;
4308 break;
4310 case 'u':
4311 /* These are just backpointers, so they don't matter. */
4312 break;
4314 case '0':
4315 break;
4317 /* It is believed that rtx's at this level will never
4318 contain anything but integers and other rtx's,
4319 except for within LABEL_REFs and SYMBOL_REFs. */
4320 default:
4321 abort ();
4324 return 1;