Move *-*-gnu* pattern below *-*-linux*.
[official-gcc.git] / gcc / jump.c
blob098e2d4dd7a475d405f561ebdb5b1a6f0775b1d3
1 /* Optimize jump instructions, for GNU compiler.
2 Copyright (C) 1987, 88, 89, 91-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This is the jump-optimization pass of the compiler.
23 It is run two or three times: once before cse, sometimes once after cse,
24 and once after reload (before final).
26 jump_optimize deletes unreachable code and labels that are not used.
27 It also deletes jumps that jump to the following insn,
28 and simplifies jumps around unconditional jumps and jumps
29 to unconditional jumps.
31 Each CODE_LABEL has a count of the times it is used
32 stored in the LABEL_NUSES internal field, and each JUMP_INSN
33 has one label that it refers to stored in the
34 JUMP_LABEL internal field. With this we can detect labels that
35 become unused because of the deletion of all the jumps that
36 formerly used them. The JUMP_LABEL info is sometimes looked
37 at by later passes.
39 Optionally, cross-jumping can be done. Currently it is done
40 only the last time (when after reload and before final).
41 In fact, the code for cross-jumping now assumes that register
42 allocation has been done, since it uses `rtx_renumbered_equal_p'.
44 Jump optimization is done after cse when cse's constant-propagation
45 causes jumps to become unconditional or to be deleted.
47 Unreachable loops are not detected here, because the labels
48 have references and the insns appear reachable from the labels.
49 find_basic_blocks in flow.c finds and deletes such loops.
51 The subroutines delete_insn, redirect_jump, and invert_jump are used
52 from other passes as well. */
54 #include "config.h"
55 #include "system.h"
56 #include "rtl.h"
57 #include "flags.h"
58 #include "hard-reg-set.h"
59 #include "regs.h"
60 #include "insn-config.h"
61 #include "insn-flags.h"
62 #include "insn-attr.h"
63 #include "recog.h"
64 #include "function.h"
65 #include "expr.h"
66 #include "real.h"
67 #include "except.h"
68 #include "toplev.h"
70 /* ??? Eventually must record somehow the labels used by jumps
71 from nested functions. */
72 /* Pre-record the next or previous real insn for each label?
73 No, this pass is very fast anyway. */
74 /* Condense consecutive labels?
75 This would make life analysis faster, maybe. */
76 /* Optimize jump y; x: ... y: jumpif... x?
77 Don't know if it is worth bothering with. */
78 /* Optimize two cases of conditional jump to conditional jump?
79 This can never delete any instruction or make anything dead,
80 or even change what is live at any point.
81 So perhaps let combiner do it. */
83 /* Vector indexed by uid.
84 For each CODE_LABEL, index by its uid to get first unconditional jump
85 that jumps to the label.
86 For each JUMP_INSN, index by its uid to get the next unconditional jump
87 that jumps to the same label.
88 Element 0 is the start of a chain of all return insns.
89 (It is safe to use element 0 because insn uid 0 is not used. */
91 static rtx *jump_chain;
93 /* Maximum index in jump_chain. */
95 static int max_jump_chain;
97 /* Set nonzero by jump_optimize if control can fall through
98 to the end of the function. */
99 int can_reach_end;
101 /* Indicates whether death notes are significant in cross jump analysis.
102 Normally they are not significant, because of A and B jump to C,
103 and R dies in A, it must die in B. But this might not be true after
104 stack register conversion, and we must compare death notes in that
105 case. */
107 static int cross_jump_death_matters = 0;
109 static int init_label_info PROTO((rtx));
110 static void delete_barrier_successors PROTO((rtx));
111 static void mark_all_labels PROTO((rtx, int));
112 static rtx delete_unreferenced_labels PROTO((rtx));
113 static void delete_noop_moves PROTO((rtx));
114 static int calculate_can_reach_end PROTO((rtx, int, int));
115 static int duplicate_loop_exit_test PROTO((rtx));
116 static void find_cross_jump PROTO((rtx, rtx, int, rtx *, rtx *));
117 static void do_cross_jump PROTO((rtx, rtx, rtx));
118 static int jump_back_p PROTO((rtx, rtx));
119 static int tension_vector_labels PROTO((rtx, int));
120 static void mark_jump_label PROTO((rtx, rtx, int));
121 static void delete_computation PROTO((rtx));
122 static void delete_from_jump_chain PROTO((rtx));
123 static int delete_labelref_insn PROTO((rtx, rtx, int));
124 static void mark_modified_reg PROTO((rtx, rtx));
125 static void redirect_tablejump PROTO((rtx, rtx));
126 static void jump_optimize_1 PROTO ((rtx, int, int, int, int));
127 #ifndef HAVE_cc0
128 static rtx find_insert_position PROTO((rtx, rtx));
129 #endif
131 /* Main external entry point into the jump optimizer. See comments before
132 jump_optimize_1 for descriptions of the arguments. */
133 void
134 jump_optimize (f, cross_jump, noop_moves, after_regscan)
135 rtx f;
136 int cross_jump;
137 int noop_moves;
138 int after_regscan;
140 jump_optimize_1 (f, cross_jump, noop_moves, after_regscan, 0);
143 /* Alternate entry into the jump optimizer. This entry point only rebuilds
144 the JUMP_LABEL field in jumping insns and REG_LABEL notes in non-jumping
145 instructions. */
146 void
147 rebuild_jump_labels (f)
148 rtx f;
150 jump_optimize_1 (f, 0, 0, 0, 1);
154 /* Delete no-op jumps and optimize jumps to jumps
155 and jumps around jumps.
156 Delete unused labels and unreachable code.
158 If CROSS_JUMP is 1, detect matching code
159 before a jump and its destination and unify them.
160 If CROSS_JUMP is 2, do cross-jumping, but pay attention to death notes.
162 If NOOP_MOVES is nonzero, delete no-op move insns.
164 If AFTER_REGSCAN is nonzero, then this jump pass is being run immediately
165 after regscan, and it is safe to use regno_first_uid and regno_last_uid.
167 If MARK_LABELS_ONLY is nonzero, then we only rebuild the jump chain
168 and JUMP_LABEL field for jumping insns.
170 If `optimize' is zero, don't change any code,
171 just determine whether control drops off the end of the function.
172 This case occurs when we have -W and not -O.
173 It works because `delete_insn' checks the value of `optimize'
174 and refrains from actually deleting when that is 0. */
176 static void
177 jump_optimize_1 (f, cross_jump, noop_moves, after_regscan, mark_labels_only)
178 rtx f;
179 int cross_jump;
180 int noop_moves;
181 int after_regscan;
182 int mark_labels_only;
184 register rtx insn, next;
185 int changed;
186 int old_max_reg;
187 int first = 1;
188 int max_uid = 0;
189 rtx last_insn;
191 cross_jump_death_matters = (cross_jump == 2);
192 max_uid = init_label_info (f) + 1;
194 /* If we are performing cross jump optimizations, then initialize
195 tables mapping UIDs to EH regions to avoid incorrect movement
196 of insns from one EH region to another. */
197 if (flag_exceptions && cross_jump)
198 init_insn_eh_region (f, max_uid);
200 delete_barrier_successors (f);
202 /* Leave some extra room for labels and duplicate exit test insns
203 we make. */
204 max_jump_chain = max_uid * 14 / 10;
205 jump_chain = (rtx *) alloca (max_jump_chain * sizeof (rtx));
206 bzero ((char *) jump_chain, max_jump_chain * sizeof (rtx));
208 mark_all_labels (f, cross_jump);
210 /* Keep track of labels used from static data;
211 they cannot ever be deleted. */
213 for (insn = forced_labels; insn; insn = XEXP (insn, 1))
214 LABEL_NUSES (XEXP (insn, 0))++;
216 check_exception_handler_labels ();
218 /* Keep track of labels used for marking handlers for exception
219 regions; they cannot usually be deleted. */
221 for (insn = exception_handler_labels; insn; insn = XEXP (insn, 1))
222 LABEL_NUSES (XEXP (insn, 0))++;
224 /* Quit now if we just wanted to rebuild the JUMP_LABEL and REG_LABEL
225 notes and recompute LABEL_NUSES. */
226 if (mark_labels_only)
227 return;
229 exception_optimize ();
231 last_insn = delete_unreferenced_labels (f);
233 if (!optimize)
235 /* CAN_REACH_END is persistent for each function. Once set it should
236 not be cleared. This is especially true for the case where we
237 delete the NOTE_FUNCTION_END note. CAN_REACH_END is cleared by
238 the front-end before compiling each function. */
239 if (calculate_can_reach_end (last_insn, 1, 0))
240 can_reach_end = 1;
242 /* Zero the "deleted" flag of all the "deleted" insns. */
243 for (insn = f; insn; insn = NEXT_INSN (insn))
244 INSN_DELETED_P (insn) = 0;
246 /* Show that the jump chain is not valid. */
247 jump_chain = 0;
248 return;
251 #ifdef HAVE_return
252 if (HAVE_return)
254 /* If we fall through to the epilogue, see if we can insert a RETURN insn
255 in front of it. If the machine allows it at this point (we might be
256 after reload for a leaf routine), it will improve optimization for it
257 to be there. */
258 insn = get_last_insn ();
259 while (insn && GET_CODE (insn) == NOTE)
260 insn = PREV_INSN (insn);
262 if (insn && GET_CODE (insn) != BARRIER)
264 emit_jump_insn (gen_return ());
265 emit_barrier ();
268 #endif
270 if (noop_moves)
271 delete_noop_moves (f);
273 /* If we haven't yet gotten to reload and we have just run regscan,
274 delete any insn that sets a register that isn't used elsewhere.
275 This helps some of the optimizations below by having less insns
276 being jumped around. */
278 if (! reload_completed && after_regscan)
279 for (insn = f; insn; insn = next)
281 rtx set = single_set (insn);
283 next = NEXT_INSN (insn);
285 if (set && GET_CODE (SET_DEST (set)) == REG
286 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
287 && REGNO_FIRST_UID (REGNO (SET_DEST (set))) == INSN_UID (insn)
288 /* We use regno_last_note_uid so as not to delete the setting
289 of a reg that's used in notes. A subsequent optimization
290 might arrange to use that reg for real. */
291 && REGNO_LAST_NOTE_UID (REGNO (SET_DEST (set))) == INSN_UID (insn)
292 && ! side_effects_p (SET_SRC (set))
293 && ! find_reg_note (insn, REG_RETVAL, 0)
294 /* An ADDRESSOF expression can turn into a use of the internal arg
295 pointer, so do not delete the initialization of the internal
296 arg pointer yet. If it is truly dead, flow will delete the
297 initializing insn. */
298 && SET_DEST (set) != current_function_internal_arg_pointer)
299 delete_insn (insn);
302 /* Now iterate optimizing jumps until nothing changes over one pass. */
303 changed = 1;
304 old_max_reg = max_reg_num ();
305 while (changed)
307 changed = 0;
309 for (insn = f; insn; insn = next)
311 rtx reallabelprev;
312 rtx temp, temp1, temp2, temp3, temp4, temp5, temp6;
313 rtx nlabel;
314 int this_is_simplejump, this_is_condjump, reversep = 0;
315 int this_is_condjump_in_parallel;
317 #if 0
318 /* If NOT the first iteration, if this is the last jump pass
319 (just before final), do the special peephole optimizations.
320 Avoiding the first iteration gives ordinary jump opts
321 a chance to work before peephole opts. */
323 if (reload_completed && !first && !flag_no_peephole)
324 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
325 peephole (insn);
326 #endif
328 /* That could have deleted some insns after INSN, so check now
329 what the following insn is. */
331 next = NEXT_INSN (insn);
333 /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
334 jump. Try to optimize by duplicating the loop exit test if so.
335 This is only safe immediately after regscan, because it uses
336 the values of regno_first_uid and regno_last_uid. */
337 if (after_regscan && GET_CODE (insn) == NOTE
338 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
339 && (temp1 = next_nonnote_insn (insn)) != 0
340 && simplejump_p (temp1))
342 temp = PREV_INSN (insn);
343 if (duplicate_loop_exit_test (insn))
345 changed = 1;
346 next = NEXT_INSN (temp);
347 continue;
351 if (GET_CODE (insn) != JUMP_INSN)
352 continue;
354 this_is_simplejump = simplejump_p (insn);
355 this_is_condjump = condjump_p (insn);
356 this_is_condjump_in_parallel = condjump_in_parallel_p (insn);
358 /* Tension the labels in dispatch tables. */
360 if (GET_CODE (PATTERN (insn)) == ADDR_VEC)
361 changed |= tension_vector_labels (PATTERN (insn), 0);
362 if (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
363 changed |= tension_vector_labels (PATTERN (insn), 1);
365 /* If a dispatch table always goes to the same place,
366 get rid of it and replace the insn that uses it. */
368 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
369 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
371 int i;
372 rtx pat = PATTERN (insn);
373 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
374 int len = XVECLEN (pat, diff_vec_p);
375 rtx dispatch = prev_real_insn (insn);
377 for (i = 0; i < len; i++)
378 if (XEXP (XVECEXP (pat, diff_vec_p, i), 0)
379 != XEXP (XVECEXP (pat, diff_vec_p, 0), 0))
380 break;
381 if (i == len
382 && dispatch != 0
383 && GET_CODE (dispatch) == JUMP_INSN
384 && JUMP_LABEL (dispatch) != 0
385 /* Don't mess with a casesi insn. */
386 && !(GET_CODE (PATTERN (dispatch)) == SET
387 && (GET_CODE (SET_SRC (PATTERN (dispatch)))
388 == IF_THEN_ELSE))
389 && next_real_insn (JUMP_LABEL (dispatch)) == insn)
391 redirect_tablejump (dispatch,
392 XEXP (XVECEXP (pat, diff_vec_p, 0), 0));
393 changed = 1;
397 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
399 /* If a jump references the end of the function, try to turn
400 it into a RETURN insn, possibly a conditional one. */
401 if (JUMP_LABEL (insn)
402 && (next_active_insn (JUMP_LABEL (insn)) == 0
403 || GET_CODE (PATTERN (next_active_insn (JUMP_LABEL (insn))))
404 == RETURN))
405 changed |= redirect_jump (insn, NULL_RTX);
407 /* Detect jump to following insn. */
408 if (reallabelprev == insn && condjump_p (insn))
410 next = next_real_insn (JUMP_LABEL (insn));
411 delete_jump (insn);
412 changed = 1;
413 continue;
416 /* If we have an unconditional jump preceded by a USE, try to put
417 the USE before the target and jump there. This simplifies many
418 of the optimizations below since we don't have to worry about
419 dealing with these USE insns. We only do this if the label
420 being branch to already has the identical USE or if code
421 never falls through to that label. */
423 if (this_is_simplejump
424 && (temp = prev_nonnote_insn (insn)) != 0
425 && GET_CODE (temp) == INSN && GET_CODE (PATTERN (temp)) == USE
426 && (temp1 = prev_nonnote_insn (JUMP_LABEL (insn))) != 0
427 && (GET_CODE (temp1) == BARRIER
428 || (GET_CODE (temp1) == INSN
429 && rtx_equal_p (PATTERN (temp), PATTERN (temp1))))
430 /* Don't do this optimization if we have a loop containing only
431 the USE instruction, and the loop start label has a usage
432 count of 1. This is because we will redo this optimization
433 everytime through the outer loop, and jump opt will never
434 exit. */
435 && ! ((temp2 = prev_nonnote_insn (temp)) != 0
436 && temp2 == JUMP_LABEL (insn)
437 && LABEL_NUSES (temp2) == 1))
439 if (GET_CODE (temp1) == BARRIER)
441 emit_insn_after (PATTERN (temp), temp1);
442 temp1 = NEXT_INSN (temp1);
445 delete_insn (temp);
446 redirect_jump (insn, get_label_before (temp1));
447 reallabelprev = prev_real_insn (temp1);
448 changed = 1;
451 /* Simplify if (...) x = a; else x = b; by converting it
452 to x = b; if (...) x = a;
453 if B is sufficiently simple, the test doesn't involve X,
454 and nothing in the test modifies B or X.
456 If we have small register classes, we also can't do this if X
457 is a hard register.
459 If the "x = b;" insn has any REG_NOTES, we don't do this because
460 of the possibility that we are running after CSE and there is a
461 REG_EQUAL note that is only valid if the branch has already been
462 taken. If we move the insn with the REG_EQUAL note, we may
463 fold the comparison to always be false in a later CSE pass.
464 (We could also delete the REG_NOTES when moving the insn, but it
465 seems simpler to not move it.) An exception is that we can move
466 the insn if the only note is a REG_EQUAL or REG_EQUIV whose
467 value is the same as "b".
469 INSN is the branch over the `else' part.
471 We set:
473 TEMP to the jump insn preceding "x = a;"
474 TEMP1 to X
475 TEMP2 to the insn that sets "x = b;"
476 TEMP3 to the insn that sets "x = a;"
477 TEMP4 to the set of "x = b"; */
479 if (this_is_simplejump
480 && (temp3 = prev_active_insn (insn)) != 0
481 && GET_CODE (temp3) == INSN
482 && (temp4 = single_set (temp3)) != 0
483 && GET_CODE (temp1 = SET_DEST (temp4)) == REG
484 && (! SMALL_REGISTER_CLASSES
485 || REGNO (temp1) >= FIRST_PSEUDO_REGISTER)
486 && (temp2 = next_active_insn (insn)) != 0
487 && GET_CODE (temp2) == INSN
488 && (temp4 = single_set (temp2)) != 0
489 && rtx_equal_p (SET_DEST (temp4), temp1)
490 && ! side_effects_p (SET_SRC (temp4))
491 && ! may_trap_p (SET_SRC (temp4))
492 && (REG_NOTES (temp2) == 0
493 || ((REG_NOTE_KIND (REG_NOTES (temp2)) == REG_EQUAL
494 || REG_NOTE_KIND (REG_NOTES (temp2)) == REG_EQUIV)
495 && XEXP (REG_NOTES (temp2), 1) == 0
496 && rtx_equal_p (XEXP (REG_NOTES (temp2), 0),
497 SET_SRC (temp4))))
498 && (temp = prev_active_insn (temp3)) != 0
499 && condjump_p (temp) && ! simplejump_p (temp)
500 /* TEMP must skip over the "x = a;" insn */
501 && prev_real_insn (JUMP_LABEL (temp)) == insn
502 && no_labels_between_p (insn, JUMP_LABEL (temp))
503 /* There must be no other entries to the "x = b;" insn. */
504 && no_labels_between_p (JUMP_LABEL (temp), temp2)
505 /* INSN must either branch to the insn after TEMP2 or the insn
506 after TEMP2 must branch to the same place as INSN. */
507 && (reallabelprev == temp2
508 || ((temp5 = next_active_insn (temp2)) != 0
509 && simplejump_p (temp5)
510 && JUMP_LABEL (temp5) == JUMP_LABEL (insn))))
512 /* The test expression, X, may be a complicated test with
513 multiple branches. See if we can find all the uses of
514 the label that TEMP branches to without hitting a CALL_INSN
515 or a jump to somewhere else. */
516 rtx target = JUMP_LABEL (temp);
517 int nuses = LABEL_NUSES (target);
518 rtx p;
519 #ifdef HAVE_cc0
520 rtx q;
521 #endif
523 /* Set P to the first jump insn that goes around "x = a;". */
524 for (p = temp; nuses && p; p = prev_nonnote_insn (p))
526 if (GET_CODE (p) == JUMP_INSN)
528 if (condjump_p (p) && ! simplejump_p (p)
529 && JUMP_LABEL (p) == target)
531 nuses--;
532 if (nuses == 0)
533 break;
535 else
536 break;
538 else if (GET_CODE (p) == CALL_INSN)
539 break;
542 #ifdef HAVE_cc0
543 /* We cannot insert anything between a set of cc and its use
544 so if P uses cc0, we must back up to the previous insn. */
545 q = prev_nonnote_insn (p);
546 if (q && GET_RTX_CLASS (GET_CODE (q)) == 'i'
547 && sets_cc0_p (PATTERN (q)))
548 p = q;
549 #endif
551 if (p)
552 p = PREV_INSN (p);
554 /* If we found all the uses and there was no data conflict, we
555 can move the assignment unless we can branch into the middle
556 from somewhere. */
557 if (nuses == 0 && p
558 && no_labels_between_p (p, insn)
559 && ! reg_referenced_between_p (temp1, p, NEXT_INSN (temp3))
560 && ! reg_set_between_p (temp1, p, temp3)
561 && (GET_CODE (SET_SRC (temp4)) == CONST_INT
562 || ! modified_between_p (SET_SRC (temp4), p, temp2))
563 /* Verify that registers used by the jump are not clobbered
564 by the instruction being moved. */
565 && ! regs_set_between_p (PATTERN (temp),
566 PREV_INSN (temp2),
567 NEXT_INSN (temp2)))
569 emit_insn_after_with_line_notes (PATTERN (temp2), p, temp2);
570 delete_insn (temp2);
572 /* Set NEXT to an insn that we know won't go away. */
573 next = next_active_insn (insn);
575 /* Delete the jump around the set. Note that we must do
576 this before we redirect the test jumps so that it won't
577 delete the code immediately following the assignment
578 we moved (which might be a jump). */
580 delete_insn (insn);
582 /* We either have two consecutive labels or a jump to
583 a jump, so adjust all the JUMP_INSNs to branch to where
584 INSN branches to. */
585 for (p = NEXT_INSN (p); p != next; p = NEXT_INSN (p))
586 if (GET_CODE (p) == JUMP_INSN)
587 redirect_jump (p, target);
589 changed = 1;
590 continue;
594 /* Simplify if (...) { x = a; goto l; } x = b; by converting it
595 to x = a; if (...) goto l; x = b;
596 if A is sufficiently simple, the test doesn't involve X,
597 and nothing in the test modifies A or X.
599 If we have small register classes, we also can't do this if X
600 is a hard register.
602 If the "x = a;" insn has any REG_NOTES, we don't do this because
603 of the possibility that we are running after CSE and there is a
604 REG_EQUAL note that is only valid if the branch has already been
605 taken. If we move the insn with the REG_EQUAL note, we may
606 fold the comparison to always be false in a later CSE pass.
607 (We could also delete the REG_NOTES when moving the insn, but it
608 seems simpler to not move it.) An exception is that we can move
609 the insn if the only note is a REG_EQUAL or REG_EQUIV whose
610 value is the same as "a".
612 INSN is the goto.
614 We set:
616 TEMP to the jump insn preceding "x = a;"
617 TEMP1 to X
618 TEMP2 to the insn that sets "x = b;"
619 TEMP3 to the insn that sets "x = a;"
620 TEMP4 to the set of "x = a"; */
622 if (this_is_simplejump
623 && (temp2 = next_active_insn (insn)) != 0
624 && GET_CODE (temp2) == INSN
625 && (temp4 = single_set (temp2)) != 0
626 && GET_CODE (temp1 = SET_DEST (temp4)) == REG
627 && (! SMALL_REGISTER_CLASSES
628 || REGNO (temp1) >= FIRST_PSEUDO_REGISTER)
629 && (temp3 = prev_active_insn (insn)) != 0
630 && GET_CODE (temp3) == INSN
631 && (temp4 = single_set (temp3)) != 0
632 && rtx_equal_p (SET_DEST (temp4), temp1)
633 && ! side_effects_p (SET_SRC (temp4))
634 && ! may_trap_p (SET_SRC (temp4))
635 && (REG_NOTES (temp3) == 0
636 || ((REG_NOTE_KIND (REG_NOTES (temp3)) == REG_EQUAL
637 || REG_NOTE_KIND (REG_NOTES (temp3)) == REG_EQUIV)
638 && XEXP (REG_NOTES (temp3), 1) == 0
639 && rtx_equal_p (XEXP (REG_NOTES (temp3), 0),
640 SET_SRC (temp4))))
641 && (temp = prev_active_insn (temp3)) != 0
642 && condjump_p (temp) && ! simplejump_p (temp)
643 /* TEMP must skip over the "x = a;" insn */
644 && prev_real_insn (JUMP_LABEL (temp)) == insn
645 && no_labels_between_p (temp, insn))
647 rtx prev_label = JUMP_LABEL (temp);
648 rtx insert_after = prev_nonnote_insn (temp);
650 #ifdef HAVE_cc0
651 /* We cannot insert anything between a set of cc and its use. */
652 if (insert_after && GET_RTX_CLASS (GET_CODE (insert_after)) == 'i'
653 && sets_cc0_p (PATTERN (insert_after)))
654 insert_after = prev_nonnote_insn (insert_after);
655 #endif
656 ++LABEL_NUSES (prev_label);
658 if (insert_after
659 && no_labels_between_p (insert_after, temp)
660 && ! reg_referenced_between_p (temp1, insert_after, temp3)
661 && ! reg_referenced_between_p (temp1, temp3,
662 NEXT_INSN (temp2))
663 && ! reg_set_between_p (temp1, insert_after, temp)
664 && ! modified_between_p (SET_SRC (temp4), insert_after, temp)
665 /* Verify that registers used by the jump are not clobbered
666 by the instruction being moved. */
667 && ! regs_set_between_p (PATTERN (temp),
668 PREV_INSN (temp3),
669 NEXT_INSN (temp3))
670 && invert_jump (temp, JUMP_LABEL (insn)))
672 emit_insn_after_with_line_notes (PATTERN (temp3),
673 insert_after, temp3);
674 delete_insn (temp3);
675 delete_insn (insn);
676 /* Set NEXT to an insn that we know won't go away. */
677 next = temp2;
678 changed = 1;
680 if (prev_label && --LABEL_NUSES (prev_label) == 0)
681 delete_insn (prev_label);
682 if (changed)
683 continue;
686 #ifndef HAVE_cc0
687 /* If we have if (...) x = exp; and branches are expensive,
688 EXP is a single insn, does not have any side effects, cannot
689 trap, and is not too costly, convert this to
690 t = exp; if (...) x = t;
692 Don't do this when we have CC0 because it is unlikely to help
693 and we'd need to worry about where to place the new insn and
694 the potential for conflicts. We also can't do this when we have
695 notes on the insn for the same reason as above.
697 We set:
699 TEMP to the "x = exp;" insn.
700 TEMP1 to the single set in the "x = exp;" insn.
701 TEMP2 to "x". */
703 if (! reload_completed
704 && this_is_condjump && ! this_is_simplejump
705 && BRANCH_COST >= 3
706 && (temp = next_nonnote_insn (insn)) != 0
707 && GET_CODE (temp) == INSN
708 && REG_NOTES (temp) == 0
709 && (reallabelprev == temp
710 || ((temp2 = next_active_insn (temp)) != 0
711 && simplejump_p (temp2)
712 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
713 && (temp1 = single_set (temp)) != 0
714 && (temp2 = SET_DEST (temp1), GET_CODE (temp2) == REG)
715 && (! SMALL_REGISTER_CLASSES
716 || REGNO (temp2) >= FIRST_PSEUDO_REGISTER)
717 && GET_CODE (SET_SRC (temp1)) != REG
718 && GET_CODE (SET_SRC (temp1)) != SUBREG
719 && GET_CODE (SET_SRC (temp1)) != CONST_INT
720 && ! side_effects_p (SET_SRC (temp1))
721 && ! may_trap_p (SET_SRC (temp1))
722 && rtx_cost (SET_SRC (temp1), SET) < 10)
724 rtx new = gen_reg_rtx (GET_MODE (temp2));
726 if ((temp3 = find_insert_position (insn, temp))
727 && validate_change (temp, &SET_DEST (temp1), new, 0))
729 next = emit_insn_after (gen_move_insn (temp2, new), insn);
730 emit_insn_after_with_line_notes (PATTERN (temp),
731 PREV_INSN (temp3), temp);
732 delete_insn (temp);
733 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
735 if (after_regscan)
737 reg_scan_update (temp3, NEXT_INSN (next), old_max_reg);
738 old_max_reg = max_reg_num ();
743 /* Similarly, if it takes two insns to compute EXP but they
744 have the same destination. Here TEMP3 will be the second
745 insn and TEMP4 the SET from that insn. */
747 if (! reload_completed
748 && this_is_condjump && ! this_is_simplejump
749 && BRANCH_COST >= 4
750 && (temp = next_nonnote_insn (insn)) != 0
751 && GET_CODE (temp) == INSN
752 && REG_NOTES (temp) == 0
753 && (temp3 = next_nonnote_insn (temp)) != 0
754 && GET_CODE (temp3) == INSN
755 && REG_NOTES (temp3) == 0
756 && (reallabelprev == temp3
757 || ((temp2 = next_active_insn (temp3)) != 0
758 && simplejump_p (temp2)
759 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
760 && (temp1 = single_set (temp)) != 0
761 && (temp2 = SET_DEST (temp1), GET_CODE (temp2) == REG)
762 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
763 && (! SMALL_REGISTER_CLASSES
764 || REGNO (temp2) >= FIRST_PSEUDO_REGISTER)
765 && ! side_effects_p (SET_SRC (temp1))
766 && ! may_trap_p (SET_SRC (temp1))
767 && rtx_cost (SET_SRC (temp1), SET) < 10
768 && (temp4 = single_set (temp3)) != 0
769 && rtx_equal_p (SET_DEST (temp4), temp2)
770 && ! side_effects_p (SET_SRC (temp4))
771 && ! may_trap_p (SET_SRC (temp4))
772 && rtx_cost (SET_SRC (temp4), SET) < 10)
774 rtx new = gen_reg_rtx (GET_MODE (temp2));
776 if ((temp5 = find_insert_position (insn, temp))
777 && (temp6 = find_insert_position (insn, temp3))
778 && validate_change (temp, &SET_DEST (temp1), new, 0))
780 /* Use the earliest of temp5 and temp6. */
781 if (temp5 != insn)
782 temp6 = temp5;
783 next = emit_insn_after (gen_move_insn (temp2, new), insn);
784 emit_insn_after_with_line_notes (PATTERN (temp),
785 PREV_INSN (temp6), temp);
786 emit_insn_after_with_line_notes
787 (replace_rtx (PATTERN (temp3), temp2, new),
788 PREV_INSN (temp6), temp3);
789 delete_insn (temp);
790 delete_insn (temp3);
791 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
793 if (after_regscan)
795 reg_scan_update (temp6, NEXT_INSN (next), old_max_reg);
796 old_max_reg = max_reg_num ();
801 /* Finally, handle the case where two insns are used to
802 compute EXP but a temporary register is used. Here we must
803 ensure that the temporary register is not used anywhere else. */
805 if (! reload_completed
806 && after_regscan
807 && this_is_condjump && ! this_is_simplejump
808 && BRANCH_COST >= 4
809 && (temp = next_nonnote_insn (insn)) != 0
810 && GET_CODE (temp) == INSN
811 && REG_NOTES (temp) == 0
812 && (temp3 = next_nonnote_insn (temp)) != 0
813 && GET_CODE (temp3) == INSN
814 && REG_NOTES (temp3) == 0
815 && (reallabelprev == temp3
816 || ((temp2 = next_active_insn (temp3)) != 0
817 && simplejump_p (temp2)
818 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
819 && (temp1 = single_set (temp)) != 0
820 && (temp5 = SET_DEST (temp1),
821 (GET_CODE (temp5) == REG
822 || (GET_CODE (temp5) == SUBREG
823 && (temp5 = SUBREG_REG (temp5),
824 GET_CODE (temp5) == REG))))
825 && REGNO (temp5) >= FIRST_PSEUDO_REGISTER
826 && REGNO_FIRST_UID (REGNO (temp5)) == INSN_UID (temp)
827 && REGNO_LAST_UID (REGNO (temp5)) == INSN_UID (temp3)
828 && ! side_effects_p (SET_SRC (temp1))
829 && ! may_trap_p (SET_SRC (temp1))
830 && rtx_cost (SET_SRC (temp1), SET) < 10
831 && (temp4 = single_set (temp3)) != 0
832 && (temp2 = SET_DEST (temp4), GET_CODE (temp2) == REG)
833 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
834 && (! SMALL_REGISTER_CLASSES
835 || REGNO (temp2) >= FIRST_PSEUDO_REGISTER)
836 && rtx_equal_p (SET_DEST (temp4), temp2)
837 && ! side_effects_p (SET_SRC (temp4))
838 && ! may_trap_p (SET_SRC (temp4))
839 && rtx_cost (SET_SRC (temp4), SET) < 10)
841 rtx new = gen_reg_rtx (GET_MODE (temp2));
843 if ((temp5 = find_insert_position (insn, temp))
844 && (temp6 = find_insert_position (insn, temp3))
845 && validate_change (temp3, &SET_DEST (temp4), new, 0))
847 /* Use the earliest of temp5 and temp6. */
848 if (temp5 != insn)
849 temp6 = temp5;
850 next = emit_insn_after (gen_move_insn (temp2, new), insn);
851 emit_insn_after_with_line_notes (PATTERN (temp),
852 PREV_INSN (temp6), temp);
853 emit_insn_after_with_line_notes (PATTERN (temp3),
854 PREV_INSN (temp6), temp3);
855 delete_insn (temp);
856 delete_insn (temp3);
857 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
859 if (after_regscan)
861 reg_scan_update (temp6, NEXT_INSN (next), old_max_reg);
862 old_max_reg = max_reg_num ();
866 #endif /* HAVE_cc0 */
868 /* Try to use a conditional move (if the target has them), or a
869 store-flag insn. The general case is:
871 1) x = a; if (...) x = b; and
872 2) if (...) x = b;
874 If the jump would be faster, the machine should not have defined
875 the movcc or scc insns!. These cases are often made by the
876 previous optimization.
878 The second case is treated as x = x; if (...) x = b;.
880 INSN here is the jump around the store. We set:
882 TEMP to the "x op= b;" insn.
883 TEMP1 to X.
884 TEMP2 to B.
885 TEMP3 to A (X in the second case).
886 TEMP4 to the condition being tested.
887 TEMP5 to the earliest insn used to find the condition.
888 TEMP6 to the SET of TEMP. */
890 if (/* We can't do this after reload has completed. */
891 ! reload_completed
892 && this_is_condjump && ! this_is_simplejump
893 /* Set TEMP to the "x = b;" insn. */
894 && (temp = next_nonnote_insn (insn)) != 0
895 && GET_CODE (temp) == INSN
896 && (temp6 = single_set (temp)) != NULL_RTX
897 && GET_CODE (temp1 = SET_DEST (temp6)) == REG
898 && (! SMALL_REGISTER_CLASSES
899 || REGNO (temp1) >= FIRST_PSEUDO_REGISTER)
900 && ! side_effects_p (temp2 = SET_SRC (temp6))
901 && ! may_trap_p (temp2)
902 /* Allow either form, but prefer the former if both apply.
903 There is no point in using the old value of TEMP1 if
904 it is a register, since cse will alias them. It can
905 lose if the old value were a hard register since CSE
906 won't replace hard registers. Avoid using TEMP3 if
907 small register classes and it is a hard register. */
908 && (((temp3 = reg_set_last (temp1, insn)) != 0
909 && ! (SMALL_REGISTER_CLASSES && GET_CODE (temp3) == REG
910 && REGNO (temp3) < FIRST_PSEUDO_REGISTER))
911 /* Make the latter case look like x = x; if (...) x = b; */
912 || (temp3 = temp1, 1))
913 /* INSN must either branch to the insn after TEMP or the insn
914 after TEMP must branch to the same place as INSN. */
915 && (reallabelprev == temp
916 || ((temp4 = next_active_insn (temp)) != 0
917 && simplejump_p (temp4)
918 && JUMP_LABEL (temp4) == JUMP_LABEL (insn)))
919 && (temp4 = get_condition (insn, &temp5)) != 0
920 /* We must be comparing objects whose modes imply the size.
921 We could handle BLKmode if (1) emit_store_flag could
922 and (2) we could find the size reliably. */
923 && GET_MODE (XEXP (temp4, 0)) != BLKmode
924 /* Even if branches are cheap, the store_flag optimization
925 can win when the operation to be performed can be
926 expressed directly. */
927 #ifdef HAVE_cc0
928 /* If the previous insn sets CC0 and something else, we can't
929 do this since we are going to delete that insn. */
931 && ! ((temp6 = prev_nonnote_insn (insn)) != 0
932 && GET_CODE (temp6) == INSN
933 && (sets_cc0_p (PATTERN (temp6)) == -1
934 || (sets_cc0_p (PATTERN (temp6)) == 1
935 && FIND_REG_INC_NOTE (temp6, NULL_RTX))))
936 #endif
939 #ifdef HAVE_conditional_move
940 /* First try a conditional move. */
942 enum rtx_code code = GET_CODE (temp4);
943 rtx var = temp1;
944 rtx cond0, cond1, aval, bval;
945 rtx target, new_insn;
947 /* Copy the compared variables into cond0 and cond1, so that
948 any side effects performed in or after the old comparison,
949 will not affect our compare which will come later. */
950 /* ??? Is it possible to just use the comparison in the jump
951 insn? After all, we're going to delete it. We'd have
952 to modify emit_conditional_move to take a comparison rtx
953 instead or write a new function. */
954 cond0 = gen_reg_rtx (GET_MODE (XEXP (temp4, 0)));
955 /* We want the target to be able to simplify comparisons with
956 zero (and maybe other constants as well), so don't create
957 pseudos for them. There's no need to either. */
958 if (GET_CODE (XEXP (temp4, 1)) == CONST_INT
959 || GET_CODE (XEXP (temp4, 1)) == CONST_DOUBLE)
960 cond1 = XEXP (temp4, 1);
961 else
962 cond1 = gen_reg_rtx (GET_MODE (XEXP (temp4, 1)));
964 /* Careful about copying these values -- an IOR or what may
965 need to do other things, like clobber flags. */
966 /* ??? Assume for the moment that AVAL is ok. */
967 aval = temp3;
969 start_sequence ();
971 /* If we're not dealing with a register or the insn is more
972 complex than a simple SET, duplicate the computation and
973 replace the destination with a new temporary. */
974 if (register_operand (temp2, GET_MODE (var))
975 && GET_CODE (PATTERN (temp)) == SET)
976 bval = temp2;
977 else
979 bval = gen_reg_rtx (GET_MODE (var));
980 new_insn = copy_rtx (temp);
981 temp6 = single_set (new_insn);
982 SET_DEST (temp6) = bval;
983 emit_insn (PATTERN (new_insn));
986 target = emit_conditional_move (var, code,
987 cond0, cond1, VOIDmode,
988 aval, bval, GET_MODE (var),
989 (code == LTU || code == GEU
990 || code == LEU || code == GTU));
992 if (target)
994 rtx seq1, seq2, last;
995 int copy_ok;
997 /* Save the conditional move sequence but don't emit it
998 yet. On some machines, like the alpha, it is possible
999 that temp5 == insn, so next generate the sequence that
1000 saves the compared values and then emit both
1001 sequences ensuring seq1 occurs before seq2. */
1002 seq2 = get_insns ();
1003 end_sequence ();
1005 /* "Now that we can't fail..." Famous last words.
1006 Generate the copy insns that preserve the compared
1007 values. */
1008 start_sequence ();
1009 emit_move_insn (cond0, XEXP (temp4, 0));
1010 if (cond1 != XEXP (temp4, 1))
1011 emit_move_insn (cond1, XEXP (temp4, 1));
1012 seq1 = get_insns ();
1013 end_sequence ();
1015 /* Validate the sequence -- this may be some weird
1016 bit-extract-and-test instruction for which there
1017 exists no complimentary bit-extract insn. */
1018 copy_ok = 1;
1019 for (last = seq1; last ; last = NEXT_INSN (last))
1020 if (recog_memoized (last) < 0)
1022 copy_ok = 0;
1023 break;
1026 if (copy_ok)
1028 emit_insns_before (seq1, temp5);
1030 /* Insert conditional move after insn, to be sure
1031 that the jump and a possible compare won't be
1032 separated. */
1033 last = emit_insns_after (seq2, insn);
1035 /* ??? We can also delete the insn that sets X to A.
1036 Flow will do it too though. */
1037 delete_insn (temp);
1038 next = NEXT_INSN (insn);
1039 delete_jump (insn);
1041 if (after_regscan)
1043 reg_scan_update (seq1, NEXT_INSN (last),
1044 old_max_reg);
1045 old_max_reg = max_reg_num ();
1048 changed = 1;
1049 continue;
1052 else
1053 end_sequence ();
1055 #endif
1057 /* That didn't work, try a store-flag insn.
1059 We further divide the cases into:
1061 1) x = a; if (...) x = b; and either A or B is zero,
1062 2) if (...) x = 0; and jumps are expensive,
1063 3) x = a; if (...) x = b; and A and B are constants where all
1064 the set bits in A are also set in B and jumps are expensive,
1065 4) x = a; if (...) x = b; and A and B non-zero, and jumps are
1066 more expensive, and
1067 5) if (...) x = b; if jumps are even more expensive. */
1069 if (GET_MODE_CLASS (GET_MODE (temp1)) == MODE_INT
1070 && ((GET_CODE (temp3) == CONST_INT)
1071 /* Make the latter case look like
1072 x = x; if (...) x = 0; */
1073 || (temp3 = temp1,
1074 ((BRANCH_COST >= 2
1075 && temp2 == const0_rtx)
1076 || BRANCH_COST >= 3)))
1077 /* If B is zero, OK; if A is zero, can only do (1) if we
1078 can reverse the condition. See if (3) applies possibly
1079 by reversing the condition. Prefer reversing to (4) when
1080 branches are very expensive. */
1081 && (((BRANCH_COST >= 2
1082 || STORE_FLAG_VALUE == -1
1083 || (STORE_FLAG_VALUE == 1
1084 /* Check that the mask is a power of two,
1085 so that it can probably be generated
1086 with a shift. */
1087 && GET_CODE (temp3) == CONST_INT
1088 && exact_log2 (INTVAL (temp3)) >= 0))
1089 && (reversep = 0, temp2 == const0_rtx))
1090 || ((BRANCH_COST >= 2
1091 || STORE_FLAG_VALUE == -1
1092 || (STORE_FLAG_VALUE == 1
1093 && GET_CODE (temp2) == CONST_INT
1094 && exact_log2 (INTVAL (temp2)) >= 0))
1095 && temp3 == const0_rtx
1096 && (reversep = can_reverse_comparison_p (temp4, insn)))
1097 || (BRANCH_COST >= 2
1098 && GET_CODE (temp2) == CONST_INT
1099 && GET_CODE (temp3) == CONST_INT
1100 && ((INTVAL (temp2) & INTVAL (temp3)) == INTVAL (temp2)
1101 || ((INTVAL (temp2) & INTVAL (temp3)) == INTVAL (temp3)
1102 && (reversep = can_reverse_comparison_p (temp4,
1103 insn)))))
1104 || BRANCH_COST >= 3)
1107 enum rtx_code code = GET_CODE (temp4);
1108 rtx uval, cval, var = temp1;
1109 int normalizep;
1110 rtx target;
1112 /* If necessary, reverse the condition. */
1113 if (reversep)
1114 code = reverse_condition (code), uval = temp2, cval = temp3;
1115 else
1116 uval = temp3, cval = temp2;
1118 /* If CVAL is non-zero, normalize to -1. Otherwise, if UVAL
1119 is the constant 1, it is best to just compute the result
1120 directly. If UVAL is constant and STORE_FLAG_VALUE
1121 includes all of its bits, it is best to compute the flag
1122 value unnormalized and `and' it with UVAL. Otherwise,
1123 normalize to -1 and `and' with UVAL. */
1124 normalizep = (cval != const0_rtx ? -1
1125 : (uval == const1_rtx ? 1
1126 : (GET_CODE (uval) == CONST_INT
1127 && (INTVAL (uval) & ~STORE_FLAG_VALUE) == 0)
1128 ? 0 : -1));
1130 /* We will be putting the store-flag insn immediately in
1131 front of the comparison that was originally being done,
1132 so we know all the variables in TEMP4 will be valid.
1133 However, this might be in front of the assignment of
1134 A to VAR. If it is, it would clobber the store-flag
1135 we will be emitting.
1137 Therefore, emit into a temporary which will be copied to
1138 VAR immediately after TEMP. */
1140 start_sequence ();
1141 target = emit_store_flag (gen_reg_rtx (GET_MODE (var)), code,
1142 XEXP (temp4, 0), XEXP (temp4, 1),
1143 VOIDmode,
1144 (code == LTU || code == LEU
1145 || code == GEU || code == GTU),
1146 normalizep);
1147 if (target)
1149 rtx seq;
1150 rtx before = insn;
1152 seq = get_insns ();
1153 end_sequence ();
1155 /* Put the store-flag insns in front of the first insn
1156 used to compute the condition to ensure that we
1157 use the same values of them as the current
1158 comparison. However, the remainder of the insns we
1159 generate will be placed directly in front of the
1160 jump insn, in case any of the pseudos we use
1161 are modified earlier. */
1163 emit_insns_before (seq, temp5);
1165 start_sequence ();
1167 /* Both CVAL and UVAL are non-zero. */
1168 if (cval != const0_rtx && uval != const0_rtx)
1170 rtx tem1, tem2;
1172 tem1 = expand_and (uval, target, NULL_RTX);
1173 if (GET_CODE (cval) == CONST_INT
1174 && GET_CODE (uval) == CONST_INT
1175 && (INTVAL (cval) & INTVAL (uval)) == INTVAL (cval))
1176 tem2 = cval;
1177 else
1179 tem2 = expand_unop (GET_MODE (var), one_cmpl_optab,
1180 target, NULL_RTX, 0);
1181 tem2 = expand_and (cval, tem2,
1182 (GET_CODE (tem2) == REG
1183 ? tem2 : 0));
1186 /* If we usually make new pseudos, do so here. This
1187 turns out to help machines that have conditional
1188 move insns. */
1189 /* ??? Conditional moves have already been handled.
1190 This may be obsolete. */
1192 if (flag_expensive_optimizations)
1193 target = 0;
1195 target = expand_binop (GET_MODE (var), ior_optab,
1196 tem1, tem2, target,
1197 1, OPTAB_WIDEN);
1199 else if (normalizep != 1)
1201 /* We know that either CVAL or UVAL is zero. If
1202 UVAL is zero, negate TARGET and `and' with CVAL.
1203 Otherwise, `and' with UVAL. */
1204 if (uval == const0_rtx)
1206 target = expand_unop (GET_MODE (var), one_cmpl_optab,
1207 target, NULL_RTX, 0);
1208 uval = cval;
1211 target = expand_and (uval, target,
1212 (GET_CODE (target) == REG
1213 && ! preserve_subexpressions_p ()
1214 ? target : NULL_RTX));
1217 emit_move_insn (var, target);
1218 seq = get_insns ();
1219 end_sequence ();
1220 #ifdef HAVE_cc0
1221 /* If INSN uses CC0, we must not separate it from the
1222 insn that sets cc0. */
1223 if (reg_mentioned_p (cc0_rtx, PATTERN (before)))
1224 before = prev_nonnote_insn (before);
1225 #endif
1226 emit_insns_before (seq, before);
1228 delete_insn (temp);
1229 next = NEXT_INSN (insn);
1230 delete_jump (insn);
1232 if (after_regscan)
1234 reg_scan_update (seq, NEXT_INSN (next), old_max_reg);
1235 old_max_reg = max_reg_num ();
1238 changed = 1;
1239 continue;
1241 else
1242 end_sequence ();
1246 /* If branches are expensive, convert
1247 if (foo) bar++; to bar += (foo != 0);
1248 and similarly for "bar--;"
1250 INSN is the conditional branch around the arithmetic. We set:
1252 TEMP is the arithmetic insn.
1253 TEMP1 is the SET doing the arithmetic.
1254 TEMP2 is the operand being incremented or decremented.
1255 TEMP3 to the condition being tested.
1256 TEMP4 to the earliest insn used to find the condition. */
1258 if ((BRANCH_COST >= 2
1259 #ifdef HAVE_incscc
1260 || HAVE_incscc
1261 #endif
1262 #ifdef HAVE_decscc
1263 || HAVE_decscc
1264 #endif
1266 && ! reload_completed
1267 && this_is_condjump && ! this_is_simplejump
1268 && (temp = next_nonnote_insn (insn)) != 0
1269 && (temp1 = single_set (temp)) != 0
1270 && (temp2 = SET_DEST (temp1),
1271 GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT)
1272 && GET_CODE (SET_SRC (temp1)) == PLUS
1273 && (XEXP (SET_SRC (temp1), 1) == const1_rtx
1274 || XEXP (SET_SRC (temp1), 1) == constm1_rtx)
1275 && rtx_equal_p (temp2, XEXP (SET_SRC (temp1), 0))
1276 && ! side_effects_p (temp2)
1277 && ! may_trap_p (temp2)
1278 /* INSN must either branch to the insn after TEMP or the insn
1279 after TEMP must branch to the same place as INSN. */
1280 && (reallabelprev == temp
1281 || ((temp3 = next_active_insn (temp)) != 0
1282 && simplejump_p (temp3)
1283 && JUMP_LABEL (temp3) == JUMP_LABEL (insn)))
1284 && (temp3 = get_condition (insn, &temp4)) != 0
1285 /* We must be comparing objects whose modes imply the size.
1286 We could handle BLKmode if (1) emit_store_flag could
1287 and (2) we could find the size reliably. */
1288 && GET_MODE (XEXP (temp3, 0)) != BLKmode
1289 && can_reverse_comparison_p (temp3, insn))
1291 rtx temp6, target = 0, seq, init_insn = 0, init = temp2;
1292 enum rtx_code code = reverse_condition (GET_CODE (temp3));
1294 start_sequence ();
1296 /* It must be the case that TEMP2 is not modified in the range
1297 [TEMP4, INSN). The one exception we make is if the insn
1298 before INSN sets TEMP2 to something which is also unchanged
1299 in that range. In that case, we can move the initialization
1300 into our sequence. */
1302 if ((temp5 = prev_active_insn (insn)) != 0
1303 && no_labels_between_p (temp5, insn)
1304 && GET_CODE (temp5) == INSN
1305 && (temp6 = single_set (temp5)) != 0
1306 && rtx_equal_p (temp2, SET_DEST (temp6))
1307 && (CONSTANT_P (SET_SRC (temp6))
1308 || GET_CODE (SET_SRC (temp6)) == REG
1309 || GET_CODE (SET_SRC (temp6)) == SUBREG))
1311 emit_insn (PATTERN (temp5));
1312 init_insn = temp5;
1313 init = SET_SRC (temp6);
1316 if (CONSTANT_P (init)
1317 || ! reg_set_between_p (init, PREV_INSN (temp4), insn))
1318 target = emit_store_flag (gen_reg_rtx (GET_MODE (temp2)), code,
1319 XEXP (temp3, 0), XEXP (temp3, 1),
1320 VOIDmode,
1321 (code == LTU || code == LEU
1322 || code == GTU || code == GEU), 1);
1324 /* If we can do the store-flag, do the addition or
1325 subtraction. */
1327 if (target)
1328 target = expand_binop (GET_MODE (temp2),
1329 (XEXP (SET_SRC (temp1), 1) == const1_rtx
1330 ? add_optab : sub_optab),
1331 temp2, target, temp2, 0, OPTAB_WIDEN);
1333 if (target != 0)
1335 /* Put the result back in temp2 in case it isn't already.
1336 Then replace the jump, possible a CC0-setting insn in
1337 front of the jump, and TEMP, with the sequence we have
1338 made. */
1340 if (target != temp2)
1341 emit_move_insn (temp2, target);
1343 seq = get_insns ();
1344 end_sequence ();
1346 emit_insns_before (seq, temp4);
1347 delete_insn (temp);
1349 if (init_insn)
1350 delete_insn (init_insn);
1352 next = NEXT_INSN (insn);
1353 #ifdef HAVE_cc0
1354 delete_insn (prev_nonnote_insn (insn));
1355 #endif
1356 delete_insn (insn);
1358 if (after_regscan)
1360 reg_scan_update (seq, NEXT_INSN (next), old_max_reg);
1361 old_max_reg = max_reg_num ();
1364 changed = 1;
1365 continue;
1367 else
1368 end_sequence ();
1371 /* Simplify if (...) x = 1; else {...} if (x) ...
1372 We recognize this case scanning backwards as well.
1374 TEMP is the assignment to x;
1375 TEMP1 is the label at the head of the second if. */
1376 /* ?? This should call get_condition to find the values being
1377 compared, instead of looking for a COMPARE insn when HAVE_cc0
1378 is not defined. This would allow it to work on the m88k. */
1379 /* ?? This optimization is only safe before cse is run if HAVE_cc0
1380 is not defined and the condition is tested by a separate compare
1381 insn. This is because the code below assumes that the result
1382 of the compare dies in the following branch.
1384 Not only that, but there might be other insns between the
1385 compare and branch whose results are live. Those insns need
1386 to be executed.
1388 A way to fix this is to move the insns at JUMP_LABEL (insn)
1389 to before INSN. If we are running before flow, they will
1390 be deleted if they aren't needed. But this doesn't work
1391 well after flow.
1393 This is really a special-case of jump threading, anyway. The
1394 right thing to do is to replace this and jump threading with
1395 much simpler code in cse.
1397 This code has been turned off in the non-cc0 case in the
1398 meantime. */
1400 #ifdef HAVE_cc0
1401 else if (this_is_simplejump
1402 /* Safe to skip USE and CLOBBER insns here
1403 since they will not be deleted. */
1404 && (temp = prev_active_insn (insn))
1405 && no_labels_between_p (temp, insn)
1406 && GET_CODE (temp) == INSN
1407 && GET_CODE (PATTERN (temp)) == SET
1408 && GET_CODE (SET_DEST (PATTERN (temp))) == REG
1409 && CONSTANT_P (SET_SRC (PATTERN (temp)))
1410 && (temp1 = next_active_insn (JUMP_LABEL (insn)))
1411 /* If we find that the next value tested is `x'
1412 (TEMP1 is the insn where this happens), win. */
1413 && GET_CODE (temp1) == INSN
1414 && GET_CODE (PATTERN (temp1)) == SET
1415 #ifdef HAVE_cc0
1416 /* Does temp1 `tst' the value of x? */
1417 && SET_SRC (PATTERN (temp1)) == SET_DEST (PATTERN (temp))
1418 && SET_DEST (PATTERN (temp1)) == cc0_rtx
1419 && (temp1 = next_nonnote_insn (temp1))
1420 #else
1421 /* Does temp1 compare the value of x against zero? */
1422 && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
1423 && XEXP (SET_SRC (PATTERN (temp1)), 1) == const0_rtx
1424 && (XEXP (SET_SRC (PATTERN (temp1)), 0)
1425 == SET_DEST (PATTERN (temp)))
1426 && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
1427 && (temp1 = find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
1428 #endif
1429 && condjump_p (temp1))
1431 /* Get the if_then_else from the condjump. */
1432 rtx choice = SET_SRC (PATTERN (temp1));
1433 if (GET_CODE (choice) == IF_THEN_ELSE)
1435 enum rtx_code code = GET_CODE (XEXP (choice, 0));
1436 rtx val = SET_SRC (PATTERN (temp));
1437 rtx cond
1438 = simplify_relational_operation (code, GET_MODE (SET_DEST (PATTERN (temp))),
1439 val, const0_rtx);
1440 rtx ultimate;
1442 if (cond == const_true_rtx)
1443 ultimate = XEXP (choice, 1);
1444 else if (cond == const0_rtx)
1445 ultimate = XEXP (choice, 2);
1446 else
1447 ultimate = 0;
1449 if (ultimate == pc_rtx)
1450 ultimate = get_label_after (temp1);
1451 else if (ultimate && GET_CODE (ultimate) != RETURN)
1452 ultimate = XEXP (ultimate, 0);
1454 if (ultimate && JUMP_LABEL(insn) != ultimate)
1455 changed |= redirect_jump (insn, ultimate);
1458 #endif
1460 #if 0
1461 /* @@ This needs a bit of work before it will be right.
1463 Any type of comparison can be accepted for the first and
1464 second compare. When rewriting the first jump, we must
1465 compute the what conditions can reach label3, and use the
1466 appropriate code. We can not simply reverse/swap the code
1467 of the first jump. In some cases, the second jump must be
1468 rewritten also.
1470 For example,
1471 < == converts to > ==
1472 < != converts to == >
1473 etc.
1475 If the code is written to only accept an '==' test for the second
1476 compare, then all that needs to be done is to swap the condition
1477 of the first branch.
1479 It is questionable whether we want this optimization anyways,
1480 since if the user wrote code like this because he/she knew that
1481 the jump to label1 is taken most of the time, then rewriting
1482 this gives slower code. */
1483 /* @@ This should call get_condition to find the values being
1484 compared, instead of looking for a COMPARE insn when HAVE_cc0
1485 is not defined. This would allow it to work on the m88k. */
1486 /* @@ This optimization is only safe before cse is run if HAVE_cc0
1487 is not defined and the condition is tested by a separate compare
1488 insn. This is because the code below assumes that the result
1489 of the compare dies in the following branch. */
1491 /* Simplify test a ~= b
1492 condjump label1;
1493 test a == b
1494 condjump label2;
1495 jump label3;
1496 label1:
1498 rewriting as
1499 test a ~~= b
1500 condjump label3
1501 test a == b
1502 condjump label2
1503 label1:
1505 where ~= is an inequality, e.g. >, and ~~= is the swapped
1506 inequality, e.g. <.
1508 We recognize this case scanning backwards.
1510 TEMP is the conditional jump to `label2';
1511 TEMP1 is the test for `a == b';
1512 TEMP2 is the conditional jump to `label1';
1513 TEMP3 is the test for `a ~= b'. */
1514 else if (this_is_simplejump
1515 && (temp = prev_active_insn (insn))
1516 && no_labels_between_p (temp, insn)
1517 && condjump_p (temp)
1518 && (temp1 = prev_active_insn (temp))
1519 && no_labels_between_p (temp1, temp)
1520 && GET_CODE (temp1) == INSN
1521 && GET_CODE (PATTERN (temp1)) == SET
1522 #ifdef HAVE_cc0
1523 && sets_cc0_p (PATTERN (temp1)) == 1
1524 #else
1525 && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
1526 && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
1527 && (temp == find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
1528 #endif
1529 && (temp2 = prev_active_insn (temp1))
1530 && no_labels_between_p (temp2, temp1)
1531 && condjump_p (temp2)
1532 && JUMP_LABEL (temp2) == next_nonnote_insn (NEXT_INSN (insn))
1533 && (temp3 = prev_active_insn (temp2))
1534 && no_labels_between_p (temp3, temp2)
1535 && GET_CODE (PATTERN (temp3)) == SET
1536 && rtx_equal_p (SET_DEST (PATTERN (temp3)),
1537 SET_DEST (PATTERN (temp1)))
1538 && rtx_equal_p (SET_SRC (PATTERN (temp1)),
1539 SET_SRC (PATTERN (temp3)))
1540 && ! inequality_comparisons_p (PATTERN (temp))
1541 && inequality_comparisons_p (PATTERN (temp2)))
1543 rtx fallthrough_label = JUMP_LABEL (temp2);
1545 ++LABEL_NUSES (fallthrough_label);
1546 if (swap_jump (temp2, JUMP_LABEL (insn)))
1548 delete_insn (insn);
1549 changed = 1;
1552 if (--LABEL_NUSES (fallthrough_label) == 0)
1553 delete_insn (fallthrough_label);
1555 #endif
1556 /* Simplify if (...) {... x = 1;} if (x) ...
1558 We recognize this case backwards.
1560 TEMP is the test of `x';
1561 TEMP1 is the assignment to `x' at the end of the
1562 previous statement. */
1563 /* @@ This should call get_condition to find the values being
1564 compared, instead of looking for a COMPARE insn when HAVE_cc0
1565 is not defined. This would allow it to work on the m88k. */
1566 /* @@ This optimization is only safe before cse is run if HAVE_cc0
1567 is not defined and the condition is tested by a separate compare
1568 insn. This is because the code below assumes that the result
1569 of the compare dies in the following branch. */
1571 /* ??? This has to be turned off. The problem is that the
1572 unconditional jump might indirectly end up branching to the
1573 label between TEMP1 and TEMP. We can't detect this, in general,
1574 since it may become a jump to there after further optimizations.
1575 If that jump is done, it will be deleted, so we will retry
1576 this optimization in the next pass, thus an infinite loop.
1578 The present code prevents this by putting the jump after the
1579 label, but this is not logically correct. */
1580 #if 0
1581 else if (this_is_condjump
1582 /* Safe to skip USE and CLOBBER insns here
1583 since they will not be deleted. */
1584 && (temp = prev_active_insn (insn))
1585 && no_labels_between_p (temp, insn)
1586 && GET_CODE (temp) == INSN
1587 && GET_CODE (PATTERN (temp)) == SET
1588 #ifdef HAVE_cc0
1589 && sets_cc0_p (PATTERN (temp)) == 1
1590 && GET_CODE (SET_SRC (PATTERN (temp))) == REG
1591 #else
1592 /* Temp must be a compare insn, we can not accept a register
1593 to register move here, since it may not be simply a
1594 tst insn. */
1595 && GET_CODE (SET_SRC (PATTERN (temp))) == COMPARE
1596 && XEXP (SET_SRC (PATTERN (temp)), 1) == const0_rtx
1597 && GET_CODE (XEXP (SET_SRC (PATTERN (temp)), 0)) == REG
1598 && GET_CODE (SET_DEST (PATTERN (temp))) == REG
1599 && insn == find_next_ref (SET_DEST (PATTERN (temp)), temp)
1600 #endif
1601 /* May skip USE or CLOBBER insns here
1602 for checking for opportunity, since we
1603 take care of them later. */
1604 && (temp1 = prev_active_insn (temp))
1605 && GET_CODE (temp1) == INSN
1606 && GET_CODE (PATTERN (temp1)) == SET
1607 #ifdef HAVE_cc0
1608 && SET_SRC (PATTERN (temp)) == SET_DEST (PATTERN (temp1))
1609 #else
1610 && (XEXP (SET_SRC (PATTERN (temp)), 0)
1611 == SET_DEST (PATTERN (temp1)))
1612 #endif
1613 && CONSTANT_P (SET_SRC (PATTERN (temp1)))
1614 /* If this isn't true, cse will do the job. */
1615 && ! no_labels_between_p (temp1, temp))
1617 /* Get the if_then_else from the condjump. */
1618 rtx choice = SET_SRC (PATTERN (insn));
1619 if (GET_CODE (choice) == IF_THEN_ELSE
1620 && (GET_CODE (XEXP (choice, 0)) == EQ
1621 || GET_CODE (XEXP (choice, 0)) == NE))
1623 int want_nonzero = (GET_CODE (XEXP (choice, 0)) == NE);
1624 rtx last_insn;
1625 rtx ultimate;
1626 rtx p;
1628 /* Get the place that condjump will jump to
1629 if it is reached from here. */
1630 if ((SET_SRC (PATTERN (temp1)) != const0_rtx)
1631 == want_nonzero)
1632 ultimate = XEXP (choice, 1);
1633 else
1634 ultimate = XEXP (choice, 2);
1635 /* Get it as a CODE_LABEL. */
1636 if (ultimate == pc_rtx)
1637 ultimate = get_label_after (insn);
1638 else
1639 /* Get the label out of the LABEL_REF. */
1640 ultimate = XEXP (ultimate, 0);
1642 /* Insert the jump immediately before TEMP, specifically
1643 after the label that is between TEMP1 and TEMP. */
1644 last_insn = PREV_INSN (temp);
1646 /* If we would be branching to the next insn, the jump
1647 would immediately be deleted and the re-inserted in
1648 a subsequent pass over the code. So don't do anything
1649 in that case. */
1650 if (next_active_insn (last_insn)
1651 != next_active_insn (ultimate))
1653 emit_barrier_after (last_insn);
1654 p = emit_jump_insn_after (gen_jump (ultimate),
1655 last_insn);
1656 JUMP_LABEL (p) = ultimate;
1657 ++LABEL_NUSES (ultimate);
1658 if (INSN_UID (ultimate) < max_jump_chain
1659 && INSN_CODE (p) < max_jump_chain)
1661 jump_chain[INSN_UID (p)]
1662 = jump_chain[INSN_UID (ultimate)];
1663 jump_chain[INSN_UID (ultimate)] = p;
1665 changed = 1;
1666 continue;
1670 #endif
1671 /* Detect a conditional jump going to the same place
1672 as an immediately following unconditional jump. */
1673 else if (this_is_condjump
1674 && (temp = next_active_insn (insn)) != 0
1675 && simplejump_p (temp)
1676 && (next_active_insn (JUMP_LABEL (insn))
1677 == next_active_insn (JUMP_LABEL (temp))))
1679 rtx tem = temp;
1681 /* ??? Optional. Disables some optimizations, but makes
1682 gcov output more accurate with -O. */
1683 if (flag_test_coverage && !reload_completed)
1684 for (tem = insn; tem != temp; tem = NEXT_INSN (tem))
1685 if (GET_CODE (tem) == NOTE && NOTE_LINE_NUMBER (tem) > 0)
1686 break;
1688 if (tem == temp)
1690 delete_jump (insn);
1691 changed = 1;
1692 continue;
1695 #ifdef HAVE_trap
1696 /* Detect a conditional jump jumping over an unconditional trap. */
1697 else if (HAVE_trap
1698 && this_is_condjump && ! this_is_simplejump
1699 && reallabelprev != 0
1700 && GET_CODE (reallabelprev) == INSN
1701 && GET_CODE (PATTERN (reallabelprev)) == TRAP_IF
1702 && TRAP_CONDITION (PATTERN (reallabelprev)) == const_true_rtx
1703 && prev_active_insn (reallabelprev) == insn
1704 && no_labels_between_p (insn, reallabelprev)
1705 && (temp2 = get_condition (insn, &temp4))
1706 && can_reverse_comparison_p (temp2, insn))
1708 rtx new = gen_cond_trap (reverse_condition (GET_CODE (temp2)),
1709 XEXP (temp2, 0), XEXP (temp2, 1),
1710 TRAP_CODE (PATTERN (reallabelprev)));
1712 if (new)
1714 emit_insn_before (new, temp4);
1715 delete_insn (reallabelprev);
1716 delete_jump (insn);
1717 changed = 1;
1718 continue;
1721 /* Detect a jump jumping to an unconditional trap. */
1722 else if (HAVE_trap && this_is_condjump
1723 && (temp = next_active_insn (JUMP_LABEL (insn)))
1724 && GET_CODE (temp) == INSN
1725 && GET_CODE (PATTERN (temp)) == TRAP_IF
1726 && (this_is_simplejump
1727 || (temp2 = get_condition (insn, &temp4))))
1729 rtx tc = TRAP_CONDITION (PATTERN (temp));
1731 if (tc == const_true_rtx
1732 || (! this_is_simplejump && rtx_equal_p (temp2, tc)))
1734 rtx new;
1735 /* Replace an unconditional jump to a trap with a trap. */
1736 if (this_is_simplejump)
1738 emit_barrier_after (emit_insn_before (gen_trap (), insn));
1739 delete_jump (insn);
1740 changed = 1;
1741 continue;
1743 new = gen_cond_trap (GET_CODE (temp2), XEXP (temp2, 0),
1744 XEXP (temp2, 1),
1745 TRAP_CODE (PATTERN (temp)));
1746 if (new)
1748 emit_insn_before (new, temp4);
1749 delete_jump (insn);
1750 changed = 1;
1751 continue;
1754 /* If the trap condition and jump condition are mutually
1755 exclusive, redirect the jump to the following insn. */
1756 else if (GET_RTX_CLASS (GET_CODE (tc)) == '<'
1757 && ! this_is_simplejump
1758 && swap_condition (GET_CODE (temp2)) == GET_CODE (tc)
1759 && rtx_equal_p (XEXP (tc, 0), XEXP (temp2, 0))
1760 && rtx_equal_p (XEXP (tc, 1), XEXP (temp2, 1))
1761 && redirect_jump (insn, get_label_after (temp)))
1763 changed = 1;
1764 continue;
1767 #endif
1769 /* Detect a conditional jump jumping over an unconditional jump. */
1771 else if ((this_is_condjump || this_is_condjump_in_parallel)
1772 && ! this_is_simplejump
1773 && reallabelprev != 0
1774 && GET_CODE (reallabelprev) == JUMP_INSN
1775 && prev_active_insn (reallabelprev) == insn
1776 && no_labels_between_p (insn, reallabelprev)
1777 && simplejump_p (reallabelprev))
1779 /* When we invert the unconditional jump, we will be
1780 decrementing the usage count of its old label.
1781 Make sure that we don't delete it now because that
1782 might cause the following code to be deleted. */
1783 rtx prev_uses = prev_nonnote_insn (reallabelprev);
1784 rtx prev_label = JUMP_LABEL (insn);
1786 if (prev_label)
1787 ++LABEL_NUSES (prev_label);
1789 if (invert_jump (insn, JUMP_LABEL (reallabelprev)))
1791 /* It is very likely that if there are USE insns before
1792 this jump, they hold REG_DEAD notes. These REG_DEAD
1793 notes are no longer valid due to this optimization,
1794 and will cause the life-analysis that following passes
1795 (notably delayed-branch scheduling) to think that
1796 these registers are dead when they are not.
1798 To prevent this trouble, we just remove the USE insns
1799 from the insn chain. */
1801 while (prev_uses && GET_CODE (prev_uses) == INSN
1802 && GET_CODE (PATTERN (prev_uses)) == USE)
1804 rtx useless = prev_uses;
1805 prev_uses = prev_nonnote_insn (prev_uses);
1806 delete_insn (useless);
1809 delete_insn (reallabelprev);
1810 next = insn;
1811 changed = 1;
1814 /* We can now safely delete the label if it is unreferenced
1815 since the delete_insn above has deleted the BARRIER. */
1816 if (prev_label && --LABEL_NUSES (prev_label) == 0)
1817 delete_insn (prev_label);
1818 continue;
1820 else
1822 /* Detect a jump to a jump. */
1824 nlabel = follow_jumps (JUMP_LABEL (insn));
1825 if (nlabel != JUMP_LABEL (insn)
1826 && redirect_jump (insn, nlabel))
1828 changed = 1;
1829 next = insn;
1832 /* Look for if (foo) bar; else break; */
1833 /* The insns look like this:
1834 insn = condjump label1;
1835 ...range1 (some insns)...
1836 jump label2;
1837 label1:
1838 ...range2 (some insns)...
1839 jump somewhere unconditionally
1840 label2: */
1842 rtx label1 = next_label (insn);
1843 rtx range1end = label1 ? prev_active_insn (label1) : 0;
1844 /* Don't do this optimization on the first round, so that
1845 jump-around-a-jump gets simplified before we ask here
1846 whether a jump is unconditional.
1848 Also don't do it when we are called after reload since
1849 it will confuse reorg. */
1850 if (! first
1851 && (reload_completed ? ! flag_delayed_branch : 1)
1852 /* Make sure INSN is something we can invert. */
1853 && condjump_p (insn)
1854 && label1 != 0
1855 && JUMP_LABEL (insn) == label1
1856 && LABEL_NUSES (label1) == 1
1857 && GET_CODE (range1end) == JUMP_INSN
1858 && simplejump_p (range1end))
1860 rtx label2 = next_label (label1);
1861 rtx range2end = label2 ? prev_active_insn (label2) : 0;
1862 if (range1end != range2end
1863 && JUMP_LABEL (range1end) == label2
1864 && GET_CODE (range2end) == JUMP_INSN
1865 && GET_CODE (NEXT_INSN (range2end)) == BARRIER
1866 /* Invert the jump condition, so we
1867 still execute the same insns in each case. */
1868 && invert_jump (insn, label1))
1870 rtx range1beg = next_active_insn (insn);
1871 rtx range2beg = next_active_insn (label1);
1872 rtx range1after, range2after;
1873 rtx range1before, range2before;
1874 rtx rangenext;
1876 /* Include in each range any notes before it, to be
1877 sure that we get the line number note if any, even
1878 if there are other notes here. */
1879 while (PREV_INSN (range1beg)
1880 && GET_CODE (PREV_INSN (range1beg)) == NOTE)
1881 range1beg = PREV_INSN (range1beg);
1883 while (PREV_INSN (range2beg)
1884 && GET_CODE (PREV_INSN (range2beg)) == NOTE)
1885 range2beg = PREV_INSN (range2beg);
1887 /* Don't move NOTEs for blocks or loops; shift them
1888 outside the ranges, where they'll stay put. */
1889 range1beg = squeeze_notes (range1beg, range1end);
1890 range2beg = squeeze_notes (range2beg, range2end);
1892 /* Get current surrounds of the 2 ranges. */
1893 range1before = PREV_INSN (range1beg);
1894 range2before = PREV_INSN (range2beg);
1895 range1after = NEXT_INSN (range1end);
1896 range2after = NEXT_INSN (range2end);
1898 /* Splice range2 where range1 was. */
1899 NEXT_INSN (range1before) = range2beg;
1900 PREV_INSN (range2beg) = range1before;
1901 NEXT_INSN (range2end) = range1after;
1902 PREV_INSN (range1after) = range2end;
1903 /* Splice range1 where range2 was. */
1904 NEXT_INSN (range2before) = range1beg;
1905 PREV_INSN (range1beg) = range2before;
1906 NEXT_INSN (range1end) = range2after;
1907 PREV_INSN (range2after) = range1end;
1909 /* Check for a loop end note between the end of
1910 range2, and the next code label. If there is one,
1911 then what we have really seen is
1912 if (foo) break; end_of_loop;
1913 and moved the break sequence outside the loop.
1914 We must move the LOOP_END note to where the
1915 loop really ends now, or we will confuse loop
1916 optimization. Stop if we find a LOOP_BEG note
1917 first, since we don't want to move the LOOP_END
1918 note in that case. */
1919 for (;range2after != label2; range2after = rangenext)
1921 rangenext = NEXT_INSN (range2after);
1922 if (GET_CODE (range2after) == NOTE)
1924 if (NOTE_LINE_NUMBER (range2after)
1925 == NOTE_INSN_LOOP_END)
1927 NEXT_INSN (PREV_INSN (range2after))
1928 = rangenext;
1929 PREV_INSN (rangenext)
1930 = PREV_INSN (range2after);
1931 PREV_INSN (range2after)
1932 = PREV_INSN (range1beg);
1933 NEXT_INSN (range2after) = range1beg;
1934 NEXT_INSN (PREV_INSN (range1beg))
1935 = range2after;
1936 PREV_INSN (range1beg) = range2after;
1938 else if (NOTE_LINE_NUMBER (range2after)
1939 == NOTE_INSN_LOOP_BEG)
1940 break;
1943 changed = 1;
1944 continue;
1949 /* Now that the jump has been tensioned,
1950 try cross jumping: check for identical code
1951 before the jump and before its target label. */
1953 /* First, cross jumping of conditional jumps: */
1955 if (cross_jump && condjump_p (insn))
1957 rtx newjpos, newlpos;
1958 rtx x = prev_real_insn (JUMP_LABEL (insn));
1960 /* A conditional jump may be crossjumped
1961 only if the place it jumps to follows
1962 an opposing jump that comes back here. */
1964 if (x != 0 && ! jump_back_p (x, insn))
1965 /* We have no opposing jump;
1966 cannot cross jump this insn. */
1967 x = 0;
1969 newjpos = 0;
1970 /* TARGET is nonzero if it is ok to cross jump
1971 to code before TARGET. If so, see if matches. */
1972 if (x != 0)
1973 find_cross_jump (insn, x, 2,
1974 &newjpos, &newlpos);
1976 if (newjpos != 0)
1978 do_cross_jump (insn, newjpos, newlpos);
1979 /* Make the old conditional jump
1980 into an unconditional one. */
1981 SET_SRC (PATTERN (insn))
1982 = gen_rtx_LABEL_REF (VOIDmode, JUMP_LABEL (insn));
1983 INSN_CODE (insn) = -1;
1984 emit_barrier_after (insn);
1985 /* Add to jump_chain unless this is a new label
1986 whose UID is too large. */
1987 if (INSN_UID (JUMP_LABEL (insn)) < max_jump_chain)
1989 jump_chain[INSN_UID (insn)]
1990 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
1991 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
1993 changed = 1;
1994 next = insn;
1998 /* Cross jumping of unconditional jumps:
1999 a few differences. */
2001 if (cross_jump && simplejump_p (insn))
2003 rtx newjpos, newlpos;
2004 rtx target;
2006 newjpos = 0;
2008 /* TARGET is nonzero if it is ok to cross jump
2009 to code before TARGET. If so, see if matches. */
2010 find_cross_jump (insn, JUMP_LABEL (insn), 1,
2011 &newjpos, &newlpos);
2013 /* If cannot cross jump to code before the label,
2014 see if we can cross jump to another jump to
2015 the same label. */
2016 /* Try each other jump to this label. */
2017 if (INSN_UID (JUMP_LABEL (insn)) < max_uid)
2018 for (target = jump_chain[INSN_UID (JUMP_LABEL (insn))];
2019 target != 0 && newjpos == 0;
2020 target = jump_chain[INSN_UID (target)])
2021 if (target != insn
2022 && JUMP_LABEL (target) == JUMP_LABEL (insn)
2023 /* Ignore TARGET if it's deleted. */
2024 && ! INSN_DELETED_P (target))
2025 find_cross_jump (insn, target, 2,
2026 &newjpos, &newlpos);
2028 if (newjpos != 0)
2030 do_cross_jump (insn, newjpos, newlpos);
2031 changed = 1;
2032 next = insn;
2036 /* This code was dead in the previous jump.c! */
2037 if (cross_jump && GET_CODE (PATTERN (insn)) == RETURN)
2039 /* Return insns all "jump to the same place"
2040 so we can cross-jump between any two of them. */
2042 rtx newjpos, newlpos, target;
2044 newjpos = 0;
2046 /* If cannot cross jump to code before the label,
2047 see if we can cross jump to another jump to
2048 the same label. */
2049 /* Try each other jump to this label. */
2050 for (target = jump_chain[0];
2051 target != 0 && newjpos == 0;
2052 target = jump_chain[INSN_UID (target)])
2053 if (target != insn
2054 && ! INSN_DELETED_P (target)
2055 && GET_CODE (PATTERN (target)) == RETURN)
2056 find_cross_jump (insn, target, 2,
2057 &newjpos, &newlpos);
2059 if (newjpos != 0)
2061 do_cross_jump (insn, newjpos, newlpos);
2062 changed = 1;
2063 next = insn;
2069 first = 0;
2072 /* Delete extraneous line number notes.
2073 Note that two consecutive notes for different lines are not really
2074 extraneous. There should be some indication where that line belonged,
2075 even if it became empty. */
2078 rtx last_note = 0;
2080 for (insn = f; insn; insn = NEXT_INSN (insn))
2081 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) >= 0)
2083 /* Delete this note if it is identical to previous note. */
2084 if (last_note
2085 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
2086 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note))
2088 delete_insn (insn);
2089 continue;
2092 last_note = insn;
2096 #ifdef HAVE_return
2097 if (HAVE_return)
2099 /* If we fall through to the epilogue, see if we can insert a RETURN insn
2100 in front of it. If the machine allows it at this point (we might be
2101 after reload for a leaf routine), it will improve optimization for it
2102 to be there. We do this both here and at the start of this pass since
2103 the RETURN might have been deleted by some of our optimizations. */
2104 insn = get_last_insn ();
2105 while (insn && GET_CODE (insn) == NOTE)
2106 insn = PREV_INSN (insn);
2108 if (insn && GET_CODE (insn) != BARRIER)
2110 emit_jump_insn (gen_return ());
2111 emit_barrier ();
2114 #endif
2116 /* CAN_REACH_END is persistent for each function. Once set it should
2117 not be cleared. This is especially true for the case where we
2118 delete the NOTE_FUNCTION_END note. CAN_REACH_END is cleared by
2119 the front-end before compiling each function. */
2120 if (calculate_can_reach_end (last_insn, 0, 1))
2121 can_reach_end = 1;
2123 /* Show JUMP_CHAIN no longer valid. */
2124 jump_chain = 0;
2127 /* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
2128 notes whose labels don't occur in the insn any more. Returns the
2129 largest INSN_UID found. */
2130 static int
2131 init_label_info (f)
2132 rtx f;
2134 int largest_uid = 0;
2135 rtx insn;
2137 for (insn = f; insn; insn = NEXT_INSN (insn))
2139 if (GET_CODE (insn) == CODE_LABEL)
2140 LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
2141 else if (GET_CODE (insn) == JUMP_INSN)
2142 JUMP_LABEL (insn) = 0;
2143 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2145 rtx note, next;
2147 for (note = REG_NOTES (insn); note; note = next)
2149 next = XEXP (note, 1);
2150 if (REG_NOTE_KIND (note) == REG_LABEL
2151 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
2152 remove_note (insn, note);
2155 if (INSN_UID (insn) > largest_uid)
2156 largest_uid = INSN_UID (insn);
2159 return largest_uid;
2162 /* Delete insns following barriers, up to next label.
2164 Also delete no-op jumps created by gcse. */
2165 static void
2166 delete_barrier_successors (f)
2167 rtx f;
2169 rtx insn;
2171 for (insn = f; insn;)
2173 if (GET_CODE (insn) == BARRIER)
2175 insn = NEXT_INSN (insn);
2177 never_reached_warning (insn);
2179 while (insn != 0 && GET_CODE (insn) != CODE_LABEL)
2181 if (GET_CODE (insn) == NOTE
2182 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
2183 insn = NEXT_INSN (insn);
2184 else
2185 insn = delete_insn (insn);
2187 /* INSN is now the code_label. */
2189 /* Also remove (set (pc) (pc)) insns which can be created by
2190 gcse. We eliminate such insns now to avoid having them
2191 cause problems later. */
2192 else if (GET_CODE (insn) == JUMP_INSN
2193 && GET_CODE (PATTERN (insn)) == SET
2194 && SET_SRC (PATTERN (insn)) == pc_rtx
2195 && SET_DEST (PATTERN (insn)) == pc_rtx)
2196 insn = delete_insn (insn);
2198 else
2199 insn = NEXT_INSN (insn);
2203 /* Mark the label each jump jumps to.
2204 Combine consecutive labels, and count uses of labels.
2206 For each label, make a chain (using `jump_chain')
2207 of all the *unconditional* jumps that jump to it;
2208 also make a chain of all returns.
2210 CROSS_JUMP indicates whether we are doing cross jumping
2211 and if we are whether we will be paying attention to
2212 death notes or not. */
2214 static void
2215 mark_all_labels (f, cross_jump)
2216 rtx f;
2217 int cross_jump;
2219 rtx insn;
2221 for (insn = f; insn; insn = NEXT_INSN (insn))
2222 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2224 mark_jump_label (PATTERN (insn), insn, cross_jump);
2225 if (! INSN_DELETED_P (insn) && GET_CODE (insn) == JUMP_INSN)
2227 if (JUMP_LABEL (insn) != 0 && simplejump_p (insn))
2229 jump_chain[INSN_UID (insn)]
2230 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
2231 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
2233 if (GET_CODE (PATTERN (insn)) == RETURN)
2235 jump_chain[INSN_UID (insn)] = jump_chain[0];
2236 jump_chain[0] = insn;
2242 /* Delete all labels already not referenced.
2243 Also find and return the last insn. */
2245 static rtx
2246 delete_unreferenced_labels (f)
2247 rtx f;
2249 rtx final = NULL_RTX;
2250 rtx insn;
2252 for (insn = f; insn; )
2254 if (GET_CODE (insn) == CODE_LABEL && LABEL_NUSES (insn) == 0)
2255 insn = delete_insn (insn);
2256 else
2258 final = insn;
2259 insn = NEXT_INSN (insn);
2263 return final;
2266 /* Delete various simple forms of moves which have no necessary
2267 side effect. */
2269 static void
2270 delete_noop_moves (f)
2271 rtx f;
2273 rtx insn, next;
2275 for (insn = f; insn; )
2277 next = NEXT_INSN (insn);
2279 if (GET_CODE (insn) == INSN)
2281 register rtx body = PATTERN (insn);
2283 /* Combine stack_adjusts with following push_insns. */
2284 #ifdef PUSH_ROUNDING
2285 if (GET_CODE (body) == SET
2286 && SET_DEST (body) == stack_pointer_rtx
2287 && GET_CODE (SET_SRC (body)) == PLUS
2288 && XEXP (SET_SRC (body), 0) == stack_pointer_rtx
2289 && GET_CODE (XEXP (SET_SRC (body), 1)) == CONST_INT
2290 && INTVAL (XEXP (SET_SRC (body), 1)) > 0)
2292 rtx p;
2293 rtx stack_adjust_insn = insn;
2294 int stack_adjust_amount = INTVAL (XEXP (SET_SRC (body), 1));
2295 int total_pushed = 0;
2296 int pushes = 0;
2298 /* Find all successive push insns. */
2299 p = insn;
2300 /* Don't convert more than three pushes;
2301 that starts adding too many displaced addresses
2302 and the whole thing starts becoming a losing
2303 proposition. */
2304 while (pushes < 3)
2306 rtx pbody, dest;
2307 p = next_nonnote_insn (p);
2308 if (p == 0 || GET_CODE (p) != INSN)
2309 break;
2310 pbody = PATTERN (p);
2311 if (GET_CODE (pbody) != SET)
2312 break;
2313 dest = SET_DEST (pbody);
2314 /* Allow a no-op move between the adjust and the push. */
2315 if (GET_CODE (dest) == REG
2316 && GET_CODE (SET_SRC (pbody)) == REG
2317 && REGNO (dest) == REGNO (SET_SRC (pbody)))
2318 continue;
2319 if (! (GET_CODE (dest) == MEM
2320 && GET_CODE (XEXP (dest, 0)) == POST_INC
2321 && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
2322 break;
2323 pushes++;
2324 if (total_pushed + GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)))
2325 > stack_adjust_amount)
2326 break;
2327 total_pushed += GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)));
2330 /* Discard the amount pushed from the stack adjust;
2331 maybe eliminate it entirely. */
2332 if (total_pushed >= stack_adjust_amount)
2334 delete_computation (stack_adjust_insn);
2335 total_pushed = stack_adjust_amount;
2337 else
2338 XEXP (SET_SRC (PATTERN (stack_adjust_insn)), 1)
2339 = GEN_INT (stack_adjust_amount - total_pushed);
2341 /* Change the appropriate push insns to ordinary stores. */
2342 p = insn;
2343 while (total_pushed > 0)
2345 rtx pbody, dest;
2346 p = next_nonnote_insn (p);
2347 if (GET_CODE (p) != INSN)
2348 break;
2349 pbody = PATTERN (p);
2350 if (GET_CODE (pbody) != SET)
2351 break;
2352 dest = SET_DEST (pbody);
2353 /* Allow a no-op move between the adjust and the push. */
2354 if (GET_CODE (dest) == REG
2355 && GET_CODE (SET_SRC (pbody)) == REG
2356 && REGNO (dest) == REGNO (SET_SRC (pbody)))
2357 continue;
2358 if (! (GET_CODE (dest) == MEM
2359 && GET_CODE (XEXP (dest, 0)) == POST_INC
2360 && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
2361 break;
2362 total_pushed -= GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)));
2363 /* If this push doesn't fully fit in the space
2364 of the stack adjust that we deleted,
2365 make another stack adjust here for what we
2366 didn't use up. There should be peepholes
2367 to recognize the resulting sequence of insns. */
2368 if (total_pushed < 0)
2370 emit_insn_before (gen_add2_insn (stack_pointer_rtx,
2371 GEN_INT (- total_pushed)),
2373 break;
2375 XEXP (dest, 0)
2376 = plus_constant (stack_pointer_rtx, total_pushed);
2379 #endif
2381 /* Detect and delete no-op move instructions
2382 resulting from not allocating a parameter in a register. */
2384 if (GET_CODE (body) == SET
2385 && (SET_DEST (body) == SET_SRC (body)
2386 || (GET_CODE (SET_DEST (body)) == MEM
2387 && GET_CODE (SET_SRC (body)) == MEM
2388 && rtx_equal_p (SET_SRC (body), SET_DEST (body))))
2389 && ! (GET_CODE (SET_DEST (body)) == MEM
2390 && MEM_VOLATILE_P (SET_DEST (body)))
2391 && ! (GET_CODE (SET_SRC (body)) == MEM
2392 && MEM_VOLATILE_P (SET_SRC (body))))
2393 delete_computation (insn);
2395 /* Detect and ignore no-op move instructions
2396 resulting from smart or fortuitous register allocation. */
2398 else if (GET_CODE (body) == SET)
2400 int sreg = true_regnum (SET_SRC (body));
2401 int dreg = true_regnum (SET_DEST (body));
2403 if (sreg == dreg && sreg >= 0)
2404 delete_insn (insn);
2405 else if (sreg >= 0 && dreg >= 0)
2407 rtx trial;
2408 rtx tem = find_equiv_reg (NULL_RTX, insn, 0,
2409 sreg, NULL_PTR, dreg,
2410 GET_MODE (SET_SRC (body)));
2412 if (tem != 0
2413 && GET_MODE (tem) == GET_MODE (SET_DEST (body)))
2415 /* DREG may have been the target of a REG_DEAD note in
2416 the insn which makes INSN redundant. If so, reorg
2417 would still think it is dead. So search for such a
2418 note and delete it if we find it. */
2419 if (! find_regno_note (insn, REG_UNUSED, dreg))
2420 for (trial = prev_nonnote_insn (insn);
2421 trial && GET_CODE (trial) != CODE_LABEL;
2422 trial = prev_nonnote_insn (trial))
2423 if (find_regno_note (trial, REG_DEAD, dreg))
2425 remove_death (dreg, trial);
2426 break;
2429 /* Deleting insn could lose a death-note for SREG. */
2430 if ((trial = find_regno_note (insn, REG_DEAD, sreg)))
2432 /* Change this into a USE so that we won't emit
2433 code for it, but still can keep the note. */
2434 PATTERN (insn)
2435 = gen_rtx_USE (VOIDmode, XEXP (trial, 0));
2436 INSN_CODE (insn) = -1;
2437 /* Remove all reg notes but the REG_DEAD one. */
2438 REG_NOTES (insn) = trial;
2439 XEXP (trial, 1) = NULL_RTX;
2441 else
2442 delete_insn (insn);
2445 else if (dreg >= 0 && CONSTANT_P (SET_SRC (body))
2446 && find_equiv_reg (SET_SRC (body), insn, 0, dreg,
2447 NULL_PTR, 0,
2448 GET_MODE (SET_DEST (body))))
2450 /* This handles the case where we have two consecutive
2451 assignments of the same constant to pseudos that didn't
2452 get a hard reg. Each SET from the constant will be
2453 converted into a SET of the spill register and an
2454 output reload will be made following it. This produces
2455 two loads of the same constant into the same spill
2456 register. */
2458 rtx in_insn = insn;
2460 /* Look back for a death note for the first reg.
2461 If there is one, it is no longer accurate. */
2462 while (in_insn && GET_CODE (in_insn) != CODE_LABEL)
2464 if ((GET_CODE (in_insn) == INSN
2465 || GET_CODE (in_insn) == JUMP_INSN)
2466 && find_regno_note (in_insn, REG_DEAD, dreg))
2468 remove_death (dreg, in_insn);
2469 break;
2471 in_insn = PREV_INSN (in_insn);
2474 /* Delete the second load of the value. */
2475 delete_insn (insn);
2478 else if (GET_CODE (body) == PARALLEL)
2480 /* If each part is a set between two identical registers or
2481 a USE or CLOBBER, delete the insn. */
2482 int i, sreg, dreg;
2483 rtx tem;
2485 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
2487 tem = XVECEXP (body, 0, i);
2488 if (GET_CODE (tem) == USE || GET_CODE (tem) == CLOBBER)
2489 continue;
2491 if (GET_CODE (tem) != SET
2492 || (sreg = true_regnum (SET_SRC (tem))) < 0
2493 || (dreg = true_regnum (SET_DEST (tem))) < 0
2494 || dreg != sreg)
2495 break;
2498 if (i < 0)
2499 delete_insn (insn);
2501 /* Also delete insns to store bit fields if they are no-ops. */
2502 /* Not worth the hair to detect this in the big-endian case. */
2503 else if (! BYTES_BIG_ENDIAN
2504 && GET_CODE (body) == SET
2505 && GET_CODE (SET_DEST (body)) == ZERO_EXTRACT
2506 && XEXP (SET_DEST (body), 2) == const0_rtx
2507 && XEXP (SET_DEST (body), 0) == SET_SRC (body)
2508 && ! (GET_CODE (SET_SRC (body)) == MEM
2509 && MEM_VOLATILE_P (SET_SRC (body))))
2510 delete_insn (insn);
2512 insn = next;
2516 /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
2517 If so indicate that this function can drop off the end by returning
2518 1, else return 0.
2520 CHECK_DELETED indicates whether we must check if the note being
2521 searched for has the deleted flag set.
2523 DELETE_FINAL_NOTE indicates whether we should delete the note
2524 if we find it. */
2526 static int
2527 calculate_can_reach_end (last, check_deleted, delete_final_note)
2528 rtx last;
2529 int check_deleted;
2530 int delete_final_note;
2532 rtx insn = last;
2533 int n_labels = 1;
2535 while (insn != NULL_RTX)
2537 int ok = 0;
2539 /* One label can follow the end-note: the return label. */
2540 if (GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
2541 ok = 1;
2542 /* Ordinary insns can follow it if returning a structure. */
2543 else if (GET_CODE (insn) == INSN)
2544 ok = 1;
2545 /* If machine uses explicit RETURN insns, no epilogue,
2546 then one of them follows the note. */
2547 else if (GET_CODE (insn) == JUMP_INSN
2548 && GET_CODE (PATTERN (insn)) == RETURN)
2549 ok = 1;
2550 /* A barrier can follow the return insn. */
2551 else if (GET_CODE (insn) == BARRIER)
2552 ok = 1;
2553 /* Other kinds of notes can follow also. */
2554 else if (GET_CODE (insn) == NOTE
2555 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
2556 ok = 1;
2558 if (ok != 1)
2559 break;
2561 insn = PREV_INSN (insn);
2564 /* See if we backed up to the appropriate type of note. */
2565 if (insn != NULL_RTX
2566 && GET_CODE (insn) == NOTE
2567 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END
2568 && (check_deleted == 0
2569 || ! INSN_DELETED_P (insn)))
2571 if (delete_final_note)
2572 delete_insn (insn);
2573 return 1;
2576 return 0;
2579 /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
2580 jump. Assume that this unconditional jump is to the exit test code. If
2581 the code is sufficiently simple, make a copy of it before INSN,
2582 followed by a jump to the exit of the loop. Then delete the unconditional
2583 jump after INSN.
2585 Return 1 if we made the change, else 0.
2587 This is only safe immediately after a regscan pass because it uses the
2588 values of regno_first_uid and regno_last_uid. */
2590 static int
2591 duplicate_loop_exit_test (loop_start)
2592 rtx loop_start;
2594 rtx insn, set, reg, p, link;
2595 rtx copy = 0, first_copy = 0;
2596 int num_insns = 0;
2597 rtx exitcode = NEXT_INSN (JUMP_LABEL (next_nonnote_insn (loop_start)));
2598 rtx lastexit;
2599 int max_reg = max_reg_num ();
2600 rtx *reg_map = 0;
2602 /* Scan the exit code. We do not perform this optimization if any insn:
2604 is a CALL_INSN
2605 is a CODE_LABEL
2606 has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
2607 is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
2608 is a NOTE_INSN_BLOCK_{BEG,END} because duplicating these notes
2609 is not valid.
2611 We also do not do this if we find an insn with ASM_OPERANDS. While
2612 this restriction should not be necessary, copying an insn with
2613 ASM_OPERANDS can confuse asm_noperands in some cases.
2615 Also, don't do this if the exit code is more than 20 insns. */
2617 for (insn = exitcode;
2618 insn
2619 && ! (GET_CODE (insn) == NOTE
2620 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
2621 insn = NEXT_INSN (insn))
2623 switch (GET_CODE (insn))
2625 case CODE_LABEL:
2626 case CALL_INSN:
2627 return 0;
2628 case NOTE:
2629 /* We could be in front of the wrong NOTE_INSN_LOOP_END if there is
2630 a jump immediately after the loop start that branches outside
2631 the loop but within an outer loop, near the exit test.
2632 If we copied this exit test and created a phony
2633 NOTE_INSN_LOOP_VTOP, this could make instructions immediately
2634 before the exit test look like these could be safely moved
2635 out of the loop even if they actually may be never executed.
2636 This can be avoided by checking here for NOTE_INSN_LOOP_CONT. */
2638 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
2639 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
2640 return 0;
2642 if (optimize < 2
2643 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2644 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
2645 /* If we were to duplicate this code, we would not move
2646 the BLOCK notes, and so debugging the moved code would
2647 be difficult. Thus, we only move the code with -O2 or
2648 higher. */
2649 return 0;
2651 break;
2652 case JUMP_INSN:
2653 case INSN:
2654 /* The code below would grossly mishandle REG_WAS_0 notes,
2655 so get rid of them here. */
2656 while ((p = find_reg_note (insn, REG_WAS_0, NULL_RTX)) != 0)
2657 remove_note (insn, p);
2658 if (++num_insns > 20
2659 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
2660 || find_reg_note (insn, REG_LIBCALL, NULL_RTX)
2661 || asm_noperands (PATTERN (insn)) > 0)
2662 return 0;
2663 break;
2664 default:
2665 break;
2669 /* Unless INSN is zero, we can do the optimization. */
2670 if (insn == 0)
2671 return 0;
2673 lastexit = insn;
2675 /* See if any insn sets a register only used in the loop exit code and
2676 not a user variable. If so, replace it with a new register. */
2677 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
2678 if (GET_CODE (insn) == INSN
2679 && (set = single_set (insn)) != 0
2680 && ((reg = SET_DEST (set), GET_CODE (reg) == REG)
2681 || (GET_CODE (reg) == SUBREG
2682 && (reg = SUBREG_REG (reg), GET_CODE (reg) == REG)))
2683 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
2684 && REGNO_FIRST_UID (REGNO (reg)) == INSN_UID (insn))
2686 for (p = NEXT_INSN (insn); p != lastexit; p = NEXT_INSN (p))
2687 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (p))
2688 break;
2690 if (p != lastexit)
2692 /* We can do the replacement. Allocate reg_map if this is the
2693 first replacement we found. */
2694 if (reg_map == 0)
2696 reg_map = (rtx *) alloca (max_reg * sizeof (rtx));
2697 bzero ((char *) reg_map, max_reg * sizeof (rtx));
2700 REG_LOOP_TEST_P (reg) = 1;
2702 reg_map[REGNO (reg)] = gen_reg_rtx (GET_MODE (reg));
2706 /* Now copy each insn. */
2707 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
2709 switch (GET_CODE (insn))
2711 case BARRIER:
2712 copy = emit_barrier_before (loop_start);
2713 break;
2714 case NOTE:
2715 /* Only copy line-number notes. */
2716 if (NOTE_LINE_NUMBER (insn) >= 0)
2718 copy = emit_note_before (NOTE_LINE_NUMBER (insn), loop_start);
2719 NOTE_SOURCE_FILE (copy) = NOTE_SOURCE_FILE (insn);
2721 break;
2723 case INSN:
2724 copy = emit_insn_before (copy_rtx (PATTERN (insn)), loop_start);
2725 if (reg_map)
2726 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
2728 mark_jump_label (PATTERN (copy), copy, 0);
2730 /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
2731 make them. */
2732 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2733 if (REG_NOTE_KIND (link) != REG_LABEL)
2734 REG_NOTES (copy)
2735 = copy_rtx (gen_rtx_EXPR_LIST (REG_NOTE_KIND (link),
2736 XEXP (link, 0),
2737 REG_NOTES (copy)));
2738 if (reg_map && REG_NOTES (copy))
2739 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
2740 break;
2742 case JUMP_INSN:
2743 copy = emit_jump_insn_before (copy_rtx (PATTERN (insn)), loop_start);
2744 if (reg_map)
2745 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
2746 mark_jump_label (PATTERN (copy), copy, 0);
2747 if (REG_NOTES (insn))
2749 REG_NOTES (copy) = copy_rtx (REG_NOTES (insn));
2750 if (reg_map)
2751 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
2754 /* If this is a simple jump, add it to the jump chain. */
2756 if (INSN_UID (copy) < max_jump_chain && JUMP_LABEL (copy)
2757 && simplejump_p (copy))
2759 jump_chain[INSN_UID (copy)]
2760 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
2761 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
2763 break;
2765 default:
2766 abort ();
2769 /* Record the first insn we copied. We need it so that we can
2770 scan the copied insns for new pseudo registers. */
2771 if (! first_copy)
2772 first_copy = copy;
2775 /* Now clean up by emitting a jump to the end label and deleting the jump
2776 at the start of the loop. */
2777 if (! copy || GET_CODE (copy) != BARRIER)
2779 copy = emit_jump_insn_before (gen_jump (get_label_after (insn)),
2780 loop_start);
2782 /* Record the first insn we copied. We need it so that we can
2783 scan the copied insns for new pseudo registers. This may not
2784 be strictly necessary since we should have copied at least one
2785 insn above. But I am going to be safe. */
2786 if (! first_copy)
2787 first_copy = copy;
2789 mark_jump_label (PATTERN (copy), copy, 0);
2790 if (INSN_UID (copy) < max_jump_chain
2791 && INSN_UID (JUMP_LABEL (copy)) < max_jump_chain)
2793 jump_chain[INSN_UID (copy)]
2794 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
2795 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
2797 emit_barrier_before (loop_start);
2800 /* Now scan from the first insn we copied to the last insn we copied
2801 (copy) for new pseudo registers. Do this after the code to jump to
2802 the end label since that might create a new pseudo too. */
2803 reg_scan_update (first_copy, copy, max_reg);
2805 /* Mark the exit code as the virtual top of the converted loop. */
2806 emit_note_before (NOTE_INSN_LOOP_VTOP, exitcode);
2808 delete_insn (next_nonnote_insn (loop_start));
2810 return 1;
2813 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, and
2814 loop-end notes between START and END out before START. Assume that
2815 END is not such a note. START may be such a note. Returns the value
2816 of the new starting insn, which may be different if the original start
2817 was such a note. */
2820 squeeze_notes (start, end)
2821 rtx start, end;
2823 rtx insn;
2824 rtx next;
2826 for (insn = start; insn != end; insn = next)
2828 next = NEXT_INSN (insn);
2829 if (GET_CODE (insn) == NOTE
2830 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
2831 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2832 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
2833 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
2834 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
2835 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP))
2837 if (insn == start)
2838 start = next;
2839 else
2841 rtx prev = PREV_INSN (insn);
2842 PREV_INSN (insn) = PREV_INSN (start);
2843 NEXT_INSN (insn) = start;
2844 NEXT_INSN (PREV_INSN (insn)) = insn;
2845 PREV_INSN (NEXT_INSN (insn)) = insn;
2846 NEXT_INSN (prev) = next;
2847 PREV_INSN (next) = prev;
2852 return start;
2855 /* Compare the instructions before insn E1 with those before E2
2856 to find an opportunity for cross jumping.
2857 (This means detecting identical sequences of insns followed by
2858 jumps to the same place, or followed by a label and a jump
2859 to that label, and replacing one with a jump to the other.)
2861 Assume E1 is a jump that jumps to label E2
2862 (that is not always true but it might as well be).
2863 Find the longest possible equivalent sequences
2864 and store the first insns of those sequences into *F1 and *F2.
2865 Store zero there if no equivalent preceding instructions are found.
2867 We give up if we find a label in stream 1.
2868 Actually we could transfer that label into stream 2. */
2870 static void
2871 find_cross_jump (e1, e2, minimum, f1, f2)
2872 rtx e1, e2;
2873 int minimum;
2874 rtx *f1, *f2;
2876 register rtx i1 = e1, i2 = e2;
2877 register rtx p1, p2;
2878 int lose = 0;
2880 rtx last1 = 0, last2 = 0;
2881 rtx afterlast1 = 0, afterlast2 = 0;
2883 *f1 = 0;
2884 *f2 = 0;
2886 while (1)
2888 i1 = prev_nonnote_insn (i1);
2890 i2 = PREV_INSN (i2);
2891 while (i2 && (GET_CODE (i2) == NOTE || GET_CODE (i2) == CODE_LABEL))
2892 i2 = PREV_INSN (i2);
2894 if (i1 == 0)
2895 break;
2897 /* Don't allow the range of insns preceding E1 or E2
2898 to include the other (E2 or E1). */
2899 if (i2 == e1 || i1 == e2)
2900 break;
2902 /* If we will get to this code by jumping, those jumps will be
2903 tensioned to go directly to the new label (before I2),
2904 so this cross-jumping won't cost extra. So reduce the minimum. */
2905 if (GET_CODE (i1) == CODE_LABEL)
2907 --minimum;
2908 break;
2911 if (i2 == 0 || GET_CODE (i1) != GET_CODE (i2))
2912 break;
2914 /* Avoid moving insns across EH regions if either of the insns
2915 can throw. */
2916 if (flag_exceptions
2917 && (asynchronous_exceptions || GET_CODE (i1) == CALL_INSN)
2918 && !in_same_eh_region (i1, i2))
2919 break;
2921 p1 = PATTERN (i1);
2922 p2 = PATTERN (i2);
2924 /* If this is a CALL_INSN, compare register usage information.
2925 If we don't check this on stack register machines, the two
2926 CALL_INSNs might be merged leaving reg-stack.c with mismatching
2927 numbers of stack registers in the same basic block.
2928 If we don't check this on machines with delay slots, a delay slot may
2929 be filled that clobbers a parameter expected by the subroutine.
2931 ??? We take the simple route for now and assume that if they're
2932 equal, they were constructed identically. */
2934 if (GET_CODE (i1) == CALL_INSN
2935 && ! rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
2936 CALL_INSN_FUNCTION_USAGE (i2)))
2937 lose = 1;
2939 #ifdef STACK_REGS
2940 /* If cross_jump_death_matters is not 0, the insn's mode
2941 indicates whether or not the insn contains any stack-like
2942 regs. */
2944 if (!lose && cross_jump_death_matters && stack_regs_mentioned (i1))
2946 /* If register stack conversion has already been done, then
2947 death notes must also be compared before it is certain that
2948 the two instruction streams match. */
2950 rtx note;
2951 HARD_REG_SET i1_regset, i2_regset;
2953 CLEAR_HARD_REG_SET (i1_regset);
2954 CLEAR_HARD_REG_SET (i2_regset);
2956 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
2957 if (REG_NOTE_KIND (note) == REG_DEAD
2958 && STACK_REG_P (XEXP (note, 0)))
2959 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
2961 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
2962 if (REG_NOTE_KIND (note) == REG_DEAD
2963 && STACK_REG_P (XEXP (note, 0)))
2964 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
2966 GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
2968 lose = 1;
2970 done:
2973 #endif
2975 /* Don't allow old-style asm or volatile extended asms to be accepted
2976 for cross jumping purposes. It is conceptually correct to allow
2977 them, since cross-jumping preserves the dynamic instruction order
2978 even though it is changing the static instruction order. However,
2979 if an asm is being used to emit an assembler pseudo-op, such as
2980 the MIPS `.set reorder' pseudo-op, then the static instruction order
2981 matters and it must be preserved. */
2982 if (GET_CODE (p1) == ASM_INPUT || GET_CODE (p2) == ASM_INPUT
2983 || (GET_CODE (p1) == ASM_OPERANDS && MEM_VOLATILE_P (p1))
2984 || (GET_CODE (p2) == ASM_OPERANDS && MEM_VOLATILE_P (p2)))
2985 lose = 1;
2987 if (lose || GET_CODE (p1) != GET_CODE (p2)
2988 || ! rtx_renumbered_equal_p (p1, p2))
2990 /* The following code helps take care of G++ cleanups. */
2991 rtx equiv1;
2992 rtx equiv2;
2994 if (!lose && GET_CODE (p1) == GET_CODE (p2)
2995 && ((equiv1 = find_reg_note (i1, REG_EQUAL, NULL_RTX)) != 0
2996 || (equiv1 = find_reg_note (i1, REG_EQUIV, NULL_RTX)) != 0)
2997 && ((equiv2 = find_reg_note (i2, REG_EQUAL, NULL_RTX)) != 0
2998 || (equiv2 = find_reg_note (i2, REG_EQUIV, NULL_RTX)) != 0)
2999 /* If the equivalences are not to a constant, they may
3000 reference pseudos that no longer exist, so we can't
3001 use them. */
3002 && CONSTANT_P (XEXP (equiv1, 0))
3003 && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
3005 rtx s1 = single_set (i1);
3006 rtx s2 = single_set (i2);
3007 if (s1 != 0 && s2 != 0
3008 && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
3010 validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
3011 validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
3012 if (! rtx_renumbered_equal_p (p1, p2))
3013 cancel_changes (0);
3014 else if (apply_change_group ())
3015 goto win;
3019 /* Insns fail to match; cross jumping is limited to the following
3020 insns. */
3022 #ifdef HAVE_cc0
3023 /* Don't allow the insn after a compare to be shared by
3024 cross-jumping unless the compare is also shared.
3025 Here, if either of these non-matching insns is a compare,
3026 exclude the following insn from possible cross-jumping. */
3027 if (sets_cc0_p (p1) || sets_cc0_p (p2))
3028 last1 = afterlast1, last2 = afterlast2, ++minimum;
3029 #endif
3031 /* If cross-jumping here will feed a jump-around-jump
3032 optimization, this jump won't cost extra, so reduce
3033 the minimum. */
3034 if (GET_CODE (i1) == JUMP_INSN
3035 && JUMP_LABEL (i1)
3036 && prev_real_insn (JUMP_LABEL (i1)) == e1)
3037 --minimum;
3038 break;
3041 win:
3042 if (GET_CODE (p1) != USE && GET_CODE (p1) != CLOBBER)
3044 /* Ok, this insn is potentially includable in a cross-jump here. */
3045 afterlast1 = last1, afterlast2 = last2;
3046 last1 = i1, last2 = i2, --minimum;
3050 if (minimum <= 0 && last1 != 0 && last1 != e1)
3051 *f1 = last1, *f2 = last2;
3054 static void
3055 do_cross_jump (insn, newjpos, newlpos)
3056 rtx insn, newjpos, newlpos;
3058 /* Find an existing label at this point
3059 or make a new one if there is none. */
3060 register rtx label = get_label_before (newlpos);
3062 /* Make the same jump insn jump to the new point. */
3063 if (GET_CODE (PATTERN (insn)) == RETURN)
3065 /* Remove from jump chain of returns. */
3066 delete_from_jump_chain (insn);
3067 /* Change the insn. */
3068 PATTERN (insn) = gen_jump (label);
3069 INSN_CODE (insn) = -1;
3070 JUMP_LABEL (insn) = label;
3071 LABEL_NUSES (label)++;
3072 /* Add to new the jump chain. */
3073 if (INSN_UID (label) < max_jump_chain
3074 && INSN_UID (insn) < max_jump_chain)
3076 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (label)];
3077 jump_chain[INSN_UID (label)] = insn;
3080 else
3081 redirect_jump (insn, label);
3083 /* Delete the matching insns before the jump. Also, remove any REG_EQUAL
3084 or REG_EQUIV note in the NEWLPOS stream that isn't also present in
3085 the NEWJPOS stream. */
3087 while (newjpos != insn)
3089 rtx lnote;
3091 for (lnote = REG_NOTES (newlpos); lnote; lnote = XEXP (lnote, 1))
3092 if ((REG_NOTE_KIND (lnote) == REG_EQUAL
3093 || REG_NOTE_KIND (lnote) == REG_EQUIV)
3094 && ! find_reg_note (newjpos, REG_EQUAL, XEXP (lnote, 0))
3095 && ! find_reg_note (newjpos, REG_EQUIV, XEXP (lnote, 0)))
3096 remove_note (newlpos, lnote);
3098 delete_insn (newjpos);
3099 newjpos = next_real_insn (newjpos);
3100 newlpos = next_real_insn (newlpos);
3104 /* Return the label before INSN, or put a new label there. */
3107 get_label_before (insn)
3108 rtx insn;
3110 rtx label;
3112 /* Find an existing label at this point
3113 or make a new one if there is none. */
3114 label = prev_nonnote_insn (insn);
3116 if (label == 0 || GET_CODE (label) != CODE_LABEL)
3118 rtx prev = PREV_INSN (insn);
3120 label = gen_label_rtx ();
3121 emit_label_after (label, prev);
3122 LABEL_NUSES (label) = 0;
3124 return label;
3127 /* Return the label after INSN, or put a new label there. */
3130 get_label_after (insn)
3131 rtx insn;
3133 rtx label;
3135 /* Find an existing label at this point
3136 or make a new one if there is none. */
3137 label = next_nonnote_insn (insn);
3139 if (label == 0 || GET_CODE (label) != CODE_LABEL)
3141 label = gen_label_rtx ();
3142 emit_label_after (label, insn);
3143 LABEL_NUSES (label) = 0;
3145 return label;
3148 /* Return 1 if INSN is a jump that jumps to right after TARGET
3149 only on the condition that TARGET itself would drop through.
3150 Assumes that TARGET is a conditional jump. */
3152 static int
3153 jump_back_p (insn, target)
3154 rtx insn, target;
3156 rtx cinsn, ctarget;
3157 enum rtx_code codei, codet;
3159 if (simplejump_p (insn) || ! condjump_p (insn)
3160 || simplejump_p (target)
3161 || target != prev_real_insn (JUMP_LABEL (insn)))
3162 return 0;
3164 cinsn = XEXP (SET_SRC (PATTERN (insn)), 0);
3165 ctarget = XEXP (SET_SRC (PATTERN (target)), 0);
3167 codei = GET_CODE (cinsn);
3168 codet = GET_CODE (ctarget);
3170 if (XEXP (SET_SRC (PATTERN (insn)), 1) == pc_rtx)
3172 if (! can_reverse_comparison_p (cinsn, insn))
3173 return 0;
3174 codei = reverse_condition (codei);
3177 if (XEXP (SET_SRC (PATTERN (target)), 2) == pc_rtx)
3179 if (! can_reverse_comparison_p (ctarget, target))
3180 return 0;
3181 codet = reverse_condition (codet);
3184 return (codei == codet
3185 && rtx_renumbered_equal_p (XEXP (cinsn, 0), XEXP (ctarget, 0))
3186 && rtx_renumbered_equal_p (XEXP (cinsn, 1), XEXP (ctarget, 1)));
3189 /* Given a comparison, COMPARISON, inside a conditional jump insn, INSN,
3190 return non-zero if it is safe to reverse this comparison. It is if our
3191 floating-point is not IEEE, if this is an NE or EQ comparison, or if
3192 this is known to be an integer comparison. */
3195 can_reverse_comparison_p (comparison, insn)
3196 rtx comparison;
3197 rtx insn;
3199 rtx arg0;
3201 /* If this is not actually a comparison, we can't reverse it. */
3202 if (GET_RTX_CLASS (GET_CODE (comparison)) != '<')
3203 return 0;
3205 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3206 /* If this is an NE comparison, it is safe to reverse it to an EQ
3207 comparison and vice versa, even for floating point. If no operands
3208 are NaNs, the reversal is valid. If some operand is a NaN, EQ is
3209 always false and NE is always true, so the reversal is also valid. */
3210 || flag_fast_math
3211 || GET_CODE (comparison) == NE
3212 || GET_CODE (comparison) == EQ)
3213 return 1;
3215 arg0 = XEXP (comparison, 0);
3217 /* Make sure ARG0 is one of the actual objects being compared. If we
3218 can't do this, we can't be sure the comparison can be reversed.
3220 Handle cc0 and a MODE_CC register. */
3221 if ((GET_CODE (arg0) == REG && GET_MODE_CLASS (GET_MODE (arg0)) == MODE_CC)
3222 #ifdef HAVE_cc0
3223 || arg0 == cc0_rtx
3224 #endif
3227 rtx prev = prev_nonnote_insn (insn);
3228 rtx set;
3230 /* If the comparison itself was a loop invariant, it could have been
3231 hoisted out of the loop. If we proceed to unroll such a loop, then
3232 we may not be able to find the comparison when copying the loop.
3234 Returning zero in that case is the safe thing to do. */
3235 if (prev == 0)
3236 return 0;
3238 set = single_set (prev);
3239 if (set == 0 || SET_DEST (set) != arg0)
3240 return 0;
3242 arg0 = SET_SRC (set);
3244 if (GET_CODE (arg0) == COMPARE)
3245 arg0 = XEXP (arg0, 0);
3248 /* We can reverse this if ARG0 is a CONST_INT or if its mode is
3249 not VOIDmode and neither a MODE_CC nor MODE_FLOAT type. */
3250 return (GET_CODE (arg0) == CONST_INT
3251 || (GET_MODE (arg0) != VOIDmode
3252 && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_CC
3253 && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_FLOAT));
3256 /* Given an rtx-code for a comparison, return the code
3257 for the negated comparison.
3258 WATCH OUT! reverse_condition is not safe to use on a jump
3259 that might be acting on the results of an IEEE floating point comparison,
3260 because of the special treatment of non-signaling nans in comparisons.
3261 Use can_reverse_comparison_p to be sure. */
3263 enum rtx_code
3264 reverse_condition (code)
3265 enum rtx_code code;
3267 switch (code)
3269 case EQ:
3270 return NE;
3272 case NE:
3273 return EQ;
3275 case GT:
3276 return LE;
3278 case GE:
3279 return LT;
3281 case LT:
3282 return GE;
3284 case LE:
3285 return GT;
3287 case GTU:
3288 return LEU;
3290 case GEU:
3291 return LTU;
3293 case LTU:
3294 return GEU;
3296 case LEU:
3297 return GTU;
3299 default:
3300 abort ();
3301 return UNKNOWN;
3305 /* Similar, but return the code when two operands of a comparison are swapped.
3306 This IS safe for IEEE floating-point. */
3308 enum rtx_code
3309 swap_condition (code)
3310 enum rtx_code code;
3312 switch (code)
3314 case EQ:
3315 case NE:
3316 return code;
3318 case GT:
3319 return LT;
3321 case GE:
3322 return LE;
3324 case LT:
3325 return GT;
3327 case LE:
3328 return GE;
3330 case GTU:
3331 return LTU;
3333 case GEU:
3334 return LEU;
3336 case LTU:
3337 return GTU;
3339 case LEU:
3340 return GEU;
3342 default:
3343 abort ();
3344 return UNKNOWN;
3348 /* Given a comparison CODE, return the corresponding unsigned comparison.
3349 If CODE is an equality comparison or already an unsigned comparison,
3350 CODE is returned. */
3352 enum rtx_code
3353 unsigned_condition (code)
3354 enum rtx_code code;
3356 switch (code)
3358 case EQ:
3359 case NE:
3360 case GTU:
3361 case GEU:
3362 case LTU:
3363 case LEU:
3364 return code;
3366 case GT:
3367 return GTU;
3369 case GE:
3370 return GEU;
3372 case LT:
3373 return LTU;
3375 case LE:
3376 return LEU;
3378 default:
3379 abort ();
3383 /* Similarly, return the signed version of a comparison. */
3385 enum rtx_code
3386 signed_condition (code)
3387 enum rtx_code code;
3389 switch (code)
3391 case EQ:
3392 case NE:
3393 case GT:
3394 case GE:
3395 case LT:
3396 case LE:
3397 return code;
3399 case GTU:
3400 return GT;
3402 case GEU:
3403 return GE;
3405 case LTU:
3406 return LT;
3408 case LEU:
3409 return LE;
3411 default:
3412 abort ();
3416 /* Return non-zero if CODE1 is more strict than CODE2, i.e., if the
3417 truth of CODE1 implies the truth of CODE2. */
3420 comparison_dominates_p (code1, code2)
3421 enum rtx_code code1, code2;
3423 if (code1 == code2)
3424 return 1;
3426 switch (code1)
3428 case EQ:
3429 if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU)
3430 return 1;
3431 break;
3433 case LT:
3434 if (code2 == LE || code2 == NE)
3435 return 1;
3436 break;
3438 case GT:
3439 if (code2 == GE || code2 == NE)
3440 return 1;
3441 break;
3443 case LTU:
3444 if (code2 == LEU || code2 == NE)
3445 return 1;
3446 break;
3448 case GTU:
3449 if (code2 == GEU || code2 == NE)
3450 return 1;
3451 break;
3453 default:
3454 break;
3457 return 0;
3460 /* Return 1 if INSN is an unconditional jump and nothing else. */
3463 simplejump_p (insn)
3464 rtx insn;
3466 return (GET_CODE (insn) == JUMP_INSN
3467 && GET_CODE (PATTERN (insn)) == SET
3468 && GET_CODE (SET_DEST (PATTERN (insn))) == PC
3469 && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
3472 /* Return nonzero if INSN is a (possibly) conditional jump
3473 and nothing more. */
3476 condjump_p (insn)
3477 rtx insn;
3479 register rtx x = PATTERN (insn);
3480 if (GET_CODE (x) != SET)
3481 return 0;
3482 if (GET_CODE (SET_DEST (x)) != PC)
3483 return 0;
3484 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
3485 return 1;
3486 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
3487 return 0;
3488 if (XEXP (SET_SRC (x), 2) == pc_rtx
3489 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
3490 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
3491 return 1;
3492 if (XEXP (SET_SRC (x), 1) == pc_rtx
3493 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
3494 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
3495 return 1;
3496 return 0;
3499 /* Return nonzero if INSN is a (possibly) conditional jump
3500 and nothing more. */
3503 condjump_in_parallel_p (insn)
3504 rtx insn;
3506 register rtx x = PATTERN (insn);
3508 if (GET_CODE (x) != PARALLEL)
3509 return 0;
3510 else
3511 x = XVECEXP (x, 0, 0);
3513 if (GET_CODE (x) != SET)
3514 return 0;
3515 if (GET_CODE (SET_DEST (x)) != PC)
3516 return 0;
3517 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
3518 return 1;
3519 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
3520 return 0;
3521 if (XEXP (SET_SRC (x), 2) == pc_rtx
3522 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
3523 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
3524 return 1;
3525 if (XEXP (SET_SRC (x), 1) == pc_rtx
3526 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
3527 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
3528 return 1;
3529 return 0;
3532 /* Return the label of a conditional jump. */
3535 condjump_label (insn)
3536 rtx insn;
3538 register rtx x = PATTERN (insn);
3540 if (GET_CODE (x) == PARALLEL)
3541 x = XVECEXP (x, 0, 0);
3542 if (GET_CODE (x) != SET)
3543 return NULL_RTX;
3544 if (GET_CODE (SET_DEST (x)) != PC)
3545 return NULL_RTX;
3546 x = SET_SRC (x);
3547 if (GET_CODE (x) == LABEL_REF)
3548 return x;
3549 if (GET_CODE (x) != IF_THEN_ELSE)
3550 return NULL_RTX;
3551 if (XEXP (x, 2) == pc_rtx && GET_CODE (XEXP (x, 1)) == LABEL_REF)
3552 return XEXP (x, 1);
3553 if (XEXP (x, 1) == pc_rtx && GET_CODE (XEXP (x, 2)) == LABEL_REF)
3554 return XEXP (x, 2);
3555 return NULL_RTX;
3558 /* Return true if INSN is a (possibly conditional) return insn. */
3560 static int
3561 returnjump_p_1 (loc, data)
3562 rtx *loc;
3563 void *data ATTRIBUTE_UNUSED;
3565 rtx x = *loc;
3566 return GET_CODE (x) == RETURN;
3570 returnjump_p (insn)
3571 rtx insn;
3573 return for_each_rtx (&PATTERN (insn), returnjump_p_1, NULL);
3576 /* Return true if INSN is a jump that only transfers control and
3577 nothing more. */
3580 onlyjump_p (insn)
3581 rtx insn;
3583 rtx set;
3585 if (GET_CODE (insn) != JUMP_INSN)
3586 return 0;
3588 set = single_set (insn);
3589 if (set == NULL)
3590 return 0;
3591 if (GET_CODE (SET_DEST (set)) != PC)
3592 return 0;
3593 if (side_effects_p (SET_SRC (set)))
3594 return 0;
3596 return 1;
3599 #ifdef HAVE_cc0
3601 /* Return 1 if X is an RTX that does nothing but set the condition codes
3602 and CLOBBER or USE registers.
3603 Return -1 if X does explicitly set the condition codes,
3604 but also does other things. */
3607 sets_cc0_p (x)
3608 rtx x ATTRIBUTE_UNUSED;
3610 if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
3611 return 1;
3612 if (GET_CODE (x) == PARALLEL)
3614 int i;
3615 int sets_cc0 = 0;
3616 int other_things = 0;
3617 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3619 if (GET_CODE (XVECEXP (x, 0, i)) == SET
3620 && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
3621 sets_cc0 = 1;
3622 else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
3623 other_things = 1;
3625 return ! sets_cc0 ? 0 : other_things ? -1 : 1;
3627 return 0;
3629 #endif
3631 /* Follow any unconditional jump at LABEL;
3632 return the ultimate label reached by any such chain of jumps.
3633 If LABEL is not followed by a jump, return LABEL.
3634 If the chain loops or we can't find end, return LABEL,
3635 since that tells caller to avoid changing the insn.
3637 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
3638 a USE or CLOBBER. */
3641 follow_jumps (label)
3642 rtx label;
3644 register rtx insn;
3645 register rtx next;
3646 register rtx value = label;
3647 register int depth;
3649 for (depth = 0;
3650 (depth < 10
3651 && (insn = next_active_insn (value)) != 0
3652 && GET_CODE (insn) == JUMP_INSN
3653 && ((JUMP_LABEL (insn) != 0 && simplejump_p (insn))
3654 || GET_CODE (PATTERN (insn)) == RETURN)
3655 && (next = NEXT_INSN (insn))
3656 && GET_CODE (next) == BARRIER);
3657 depth++)
3659 /* Don't chain through the insn that jumps into a loop
3660 from outside the loop,
3661 since that would create multiple loop entry jumps
3662 and prevent loop optimization. */
3663 rtx tem;
3664 if (!reload_completed)
3665 for (tem = value; tem != insn; tem = NEXT_INSN (tem))
3666 if (GET_CODE (tem) == NOTE
3667 && (NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG
3668 /* ??? Optional. Disables some optimizations, but makes
3669 gcov output more accurate with -O. */
3670 || (flag_test_coverage && NOTE_LINE_NUMBER (tem) > 0)))
3671 return value;
3673 /* If we have found a cycle, make the insn jump to itself. */
3674 if (JUMP_LABEL (insn) == label)
3675 return label;
3677 tem = next_active_insn (JUMP_LABEL (insn));
3678 if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
3679 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
3680 break;
3682 value = JUMP_LABEL (insn);
3684 if (depth == 10)
3685 return label;
3686 return value;
3689 /* Assuming that field IDX of X is a vector of label_refs,
3690 replace each of them by the ultimate label reached by it.
3691 Return nonzero if a change is made.
3692 If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG. */
3694 static int
3695 tension_vector_labels (x, idx)
3696 register rtx x;
3697 register int idx;
3699 int changed = 0;
3700 register int i;
3701 for (i = XVECLEN (x, idx) - 1; i >= 0; i--)
3703 register rtx olabel = XEXP (XVECEXP (x, idx, i), 0);
3704 register rtx nlabel = follow_jumps (olabel);
3705 if (nlabel && nlabel != olabel)
3707 XEXP (XVECEXP (x, idx, i), 0) = nlabel;
3708 ++LABEL_NUSES (nlabel);
3709 if (--LABEL_NUSES (olabel) == 0)
3710 delete_insn (olabel);
3711 changed = 1;
3714 return changed;
3717 /* Find all CODE_LABELs referred to in X, and increment their use counts.
3718 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
3719 in INSN, then store one of them in JUMP_LABEL (INSN).
3720 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
3721 referenced in INSN, add a REG_LABEL note containing that label to INSN.
3722 Also, when there are consecutive labels, canonicalize on the last of them.
3724 Note that two labels separated by a loop-beginning note
3725 must be kept distinct if we have not yet done loop-optimization,
3726 because the gap between them is where loop-optimize
3727 will want to move invariant code to. CROSS_JUMP tells us
3728 that loop-optimization is done with.
3730 Once reload has completed (CROSS_JUMP non-zero), we need not consider
3731 two labels distinct if they are separated by only USE or CLOBBER insns. */
3733 static void
3734 mark_jump_label (x, insn, cross_jump)
3735 register rtx x;
3736 rtx insn;
3737 int cross_jump;
3739 register RTX_CODE code = GET_CODE (x);
3740 register int i;
3741 register const char *fmt;
3743 switch (code)
3745 case PC:
3746 case CC0:
3747 case REG:
3748 case SUBREG:
3749 case CONST_INT:
3750 case SYMBOL_REF:
3751 case CONST_DOUBLE:
3752 case CLOBBER:
3753 case CALL:
3754 return;
3756 case MEM:
3757 /* If this is a constant-pool reference, see if it is a label. */
3758 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3759 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
3760 mark_jump_label (get_pool_constant (XEXP (x, 0)), insn, cross_jump);
3761 break;
3763 case LABEL_REF:
3765 rtx label = XEXP (x, 0);
3766 rtx olabel = label;
3767 rtx note;
3768 rtx next;
3770 if (GET_CODE (label) != CODE_LABEL)
3771 abort ();
3773 /* Ignore references to labels of containing functions. */
3774 if (LABEL_REF_NONLOCAL_P (x))
3775 break;
3777 /* If there are other labels following this one,
3778 replace it with the last of the consecutive labels. */
3779 for (next = NEXT_INSN (label); next; next = NEXT_INSN (next))
3781 if (GET_CODE (next) == CODE_LABEL)
3782 label = next;
3783 else if (cross_jump && GET_CODE (next) == INSN
3784 && (GET_CODE (PATTERN (next)) == USE
3785 || GET_CODE (PATTERN (next)) == CLOBBER))
3786 continue;
3787 else if (GET_CODE (next) != NOTE)
3788 break;
3789 else if (! cross_jump
3790 && (NOTE_LINE_NUMBER (next) == NOTE_INSN_LOOP_BEG
3791 || NOTE_LINE_NUMBER (next) == NOTE_INSN_FUNCTION_END
3792 /* ??? Optional. Disables some optimizations, but
3793 makes gcov output more accurate with -O. */
3794 || (flag_test_coverage && NOTE_LINE_NUMBER (next) > 0)))
3795 break;
3798 XEXP (x, 0) = label;
3799 if (! insn || ! INSN_DELETED_P (insn))
3800 ++LABEL_NUSES (label);
3802 if (insn)
3804 if (GET_CODE (insn) == JUMP_INSN)
3805 JUMP_LABEL (insn) = label;
3807 /* If we've changed OLABEL and we had a REG_LABEL note
3808 for it, update it as well. */
3809 else if (label != olabel
3810 && (note = find_reg_note (insn, REG_LABEL, olabel)) != 0)
3811 XEXP (note, 0) = label;
3813 /* Otherwise, add a REG_LABEL note for LABEL unless there already
3814 is one. */
3815 else if (! find_reg_note (insn, REG_LABEL, label))
3817 /* This code used to ignore labels which refered to dispatch
3818 tables to avoid flow.c generating worse code.
3820 However, in the presense of global optimizations like
3821 gcse which call find_basic_blocks without calling
3822 life_analysis, not recording such labels will lead
3823 to compiler aborts because of inconsistencies in the
3824 flow graph. So we go ahead and record the label.
3826 It may also be the case that the optimization argument
3827 is no longer valid because of the more accurate cfg
3828 we build in find_basic_blocks -- it no longer pessimizes
3829 code when it finds a REG_LABEL note. */
3830 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL, label,
3831 REG_NOTES (insn));
3834 return;
3837 /* Do walk the labels in a vector, but not the first operand of an
3838 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
3839 case ADDR_VEC:
3840 case ADDR_DIFF_VEC:
3841 if (! INSN_DELETED_P (insn))
3843 int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
3845 for (i = 0; i < XVECLEN (x, eltnum); i++)
3846 mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, cross_jump);
3848 return;
3850 default:
3851 break;
3854 fmt = GET_RTX_FORMAT (code);
3855 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3857 if (fmt[i] == 'e')
3858 mark_jump_label (XEXP (x, i), insn, cross_jump);
3859 else if (fmt[i] == 'E')
3861 register int j;
3862 for (j = 0; j < XVECLEN (x, i); j++)
3863 mark_jump_label (XVECEXP (x, i, j), insn, cross_jump);
3868 /* If all INSN does is set the pc, delete it,
3869 and delete the insn that set the condition codes for it
3870 if that's what the previous thing was. */
3872 void
3873 delete_jump (insn)
3874 rtx insn;
3876 register rtx set = single_set (insn);
3878 if (set && GET_CODE (SET_DEST (set)) == PC)
3879 delete_computation (insn);
3882 /* Recursively delete prior insns that compute the value (used only by INSN
3883 which the caller is deleting) stored in the register mentioned by NOTE
3884 which is a REG_DEAD note associated with INSN. */
3886 static void
3887 delete_prior_computation (note, insn)
3888 rtx note;
3889 rtx insn;
3891 rtx our_prev;
3892 rtx reg = XEXP (note, 0);
3894 for (our_prev = prev_nonnote_insn (insn);
3895 our_prev && (GET_CODE (our_prev) == INSN
3896 || GET_CODE (our_prev) == CALL_INSN);
3897 our_prev = prev_nonnote_insn (our_prev))
3899 rtx pat = PATTERN (our_prev);
3901 /* If we reach a CALL which is not calling a const function
3902 or the callee pops the arguments, then give up. */
3903 if (GET_CODE (our_prev) == CALL_INSN
3904 && (! CONST_CALL_P (our_prev)
3905 || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
3906 break;
3908 /* If we reach a SEQUENCE, it is too complex to try to
3909 do anything with it, so give up. */
3910 if (GET_CODE (pat) == SEQUENCE)
3911 break;
3913 if (GET_CODE (pat) == USE
3914 && GET_CODE (XEXP (pat, 0)) == INSN)
3915 /* reorg creates USEs that look like this. We leave them
3916 alone because reorg needs them for its own purposes. */
3917 break;
3919 if (reg_set_p (reg, pat))
3921 if (side_effects_p (pat) && GET_CODE (our_prev) != CALL_INSN)
3922 break;
3924 if (GET_CODE (pat) == PARALLEL)
3926 /* If we find a SET of something else, we can't
3927 delete the insn. */
3929 int i;
3931 for (i = 0; i < XVECLEN (pat, 0); i++)
3933 rtx part = XVECEXP (pat, 0, i);
3935 if (GET_CODE (part) == SET
3936 && SET_DEST (part) != reg)
3937 break;
3940 if (i == XVECLEN (pat, 0))
3941 delete_computation (our_prev);
3943 else if (GET_CODE (pat) == SET
3944 && GET_CODE (SET_DEST (pat)) == REG)
3946 int dest_regno = REGNO (SET_DEST (pat));
3947 int dest_endregno
3948 = dest_regno + (dest_regno < FIRST_PSEUDO_REGISTER
3949 ? HARD_REGNO_NREGS (dest_regno,
3950 GET_MODE (SET_DEST (pat))) : 1);
3951 int regno = REGNO (reg);
3952 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
3953 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
3955 if (dest_regno >= regno
3956 && dest_endregno <= endregno)
3957 delete_computation (our_prev);
3959 /* We may have a multi-word hard register and some, but not
3960 all, of the words of the register are needed in subsequent
3961 insns. Write REG_UNUSED notes for those parts that were not
3962 needed. */
3963 else if (dest_regno <= regno
3964 && dest_endregno >= endregno)
3966 int i;
3968 REG_NOTES (our_prev)
3969 = gen_rtx_EXPR_LIST (REG_UNUSED, reg, REG_NOTES (our_prev));
3971 for (i = dest_regno; i < dest_endregno; i++)
3972 if (! find_regno_note (our_prev, REG_UNUSED, i))
3973 break;
3975 if (i == dest_endregno)
3976 delete_computation (our_prev);
3980 break;
3983 /* If PAT references the register that dies here, it is an
3984 additional use. Hence any prior SET isn't dead. However, this
3985 insn becomes the new place for the REG_DEAD note. */
3986 if (reg_overlap_mentioned_p (reg, pat))
3988 XEXP (note, 1) = REG_NOTES (our_prev);
3989 REG_NOTES (our_prev) = note;
3990 break;
3995 /* Delete INSN and recursively delete insns that compute values used only
3996 by INSN. This uses the REG_DEAD notes computed during flow analysis.
3997 If we are running before flow.c, we need do nothing since flow.c will
3998 delete dead code. We also can't know if the registers being used are
3999 dead or not at this point.
4001 Otherwise, look at all our REG_DEAD notes. If a previous insn does
4002 nothing other than set a register that dies in this insn, we can delete
4003 that insn as well.
4005 On machines with CC0, if CC0 is used in this insn, we may be able to
4006 delete the insn that set it. */
4008 static void
4009 delete_computation (insn)
4010 rtx insn;
4012 rtx note, next;
4013 rtx set;
4015 #ifdef HAVE_cc0
4016 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
4018 rtx prev = prev_nonnote_insn (insn);
4019 /* We assume that at this stage
4020 CC's are always set explicitly
4021 and always immediately before the jump that
4022 will use them. So if the previous insn
4023 exists to set the CC's, delete it
4024 (unless it performs auto-increments, etc.). */
4025 if (prev && GET_CODE (prev) == INSN
4026 && sets_cc0_p (PATTERN (prev)))
4028 if (sets_cc0_p (PATTERN (prev)) > 0
4029 && ! side_effects_p (PATTERN (prev)))
4030 delete_computation (prev);
4031 else
4032 /* Otherwise, show that cc0 won't be used. */
4033 REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_UNUSED,
4034 cc0_rtx, REG_NOTES (prev));
4037 #endif
4039 #ifdef INSN_SCHEDULING
4040 /* ?!? The schedulers do not keep REG_DEAD notes accurate after
4041 reload has completed. The schedulers need to be fixed. Until
4042 they are, we must not rely on the death notes here. */
4043 if (reload_completed && flag_schedule_insns_after_reload)
4045 delete_insn (insn);
4046 return;
4048 #endif
4050 /* The REG_DEAD note may have been omitted for a register
4051 which is both set and used by the insn. */
4052 set = single_set (insn);
4053 if (set && GET_CODE (SET_DEST (set)) == REG)
4055 int dest_regno = REGNO (SET_DEST (set));
4056 int dest_endregno
4057 = dest_regno + (dest_regno < FIRST_PSEUDO_REGISTER
4058 ? HARD_REGNO_NREGS (dest_regno,
4059 GET_MODE (SET_DEST (set))) : 1);
4060 int i;
4062 for (i = dest_regno; i < dest_endregno; i++)
4064 if (! refers_to_regno_p (i, i + 1, SET_SRC (set), NULL_PTR)
4065 || find_regno_note (insn, REG_DEAD, i))
4066 continue;
4068 note = gen_rtx_EXPR_LIST (REG_DEAD, (i < FIRST_PSEUDO_REGISTER
4069 ? gen_rtx_REG (reg_raw_mode[i], i)
4070 : SET_DEST (set)), NULL_RTX);
4071 delete_prior_computation (note, insn);
4075 for (note = REG_NOTES (insn); note; note = next)
4077 next = XEXP (note, 1);
4079 if (REG_NOTE_KIND (note) != REG_DEAD
4080 /* Verify that the REG_NOTE is legitimate. */
4081 || GET_CODE (XEXP (note, 0)) != REG)
4082 continue;
4084 delete_prior_computation (note, insn);
4087 delete_insn (insn);
4090 /* Delete insn INSN from the chain of insns and update label ref counts.
4091 May delete some following insns as a consequence; may even delete
4092 a label elsewhere and insns that follow it.
4094 Returns the first insn after INSN that was not deleted. */
4097 delete_insn (insn)
4098 register rtx insn;
4100 register rtx next = NEXT_INSN (insn);
4101 register rtx prev = PREV_INSN (insn);
4102 register int was_code_label = (GET_CODE (insn) == CODE_LABEL);
4103 register int dont_really_delete = 0;
4105 while (next && INSN_DELETED_P (next))
4106 next = NEXT_INSN (next);
4108 /* This insn is already deleted => return first following nondeleted. */
4109 if (INSN_DELETED_P (insn))
4110 return next;
4112 if (was_code_label)
4113 remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
4115 /* Don't delete user-declared labels. Convert them to special NOTEs
4116 instead. */
4117 if (was_code_label && LABEL_NAME (insn) != 0
4118 && optimize && ! dont_really_delete)
4120 PUT_CODE (insn, NOTE);
4121 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
4122 NOTE_SOURCE_FILE (insn) = 0;
4123 dont_really_delete = 1;
4125 else
4126 /* Mark this insn as deleted. */
4127 INSN_DELETED_P (insn) = 1;
4129 /* If this is an unconditional jump, delete it from the jump chain. */
4130 if (simplejump_p (insn))
4131 delete_from_jump_chain (insn);
4133 /* If instruction is followed by a barrier,
4134 delete the barrier too. */
4136 if (next != 0 && GET_CODE (next) == BARRIER)
4138 INSN_DELETED_P (next) = 1;
4139 next = NEXT_INSN (next);
4142 /* Patch out INSN (and the barrier if any) */
4144 if (optimize && ! dont_really_delete)
4146 if (prev)
4148 NEXT_INSN (prev) = next;
4149 if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
4150 NEXT_INSN (XVECEXP (PATTERN (prev), 0,
4151 XVECLEN (PATTERN (prev), 0) - 1)) = next;
4154 if (next)
4156 PREV_INSN (next) = prev;
4157 if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
4158 PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
4161 if (prev && NEXT_INSN (prev) == 0)
4162 set_last_insn (prev);
4165 /* If deleting a jump, decrement the count of the label,
4166 and delete the label if it is now unused. */
4168 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
4170 rtx lab = JUMP_LABEL (insn), lab_next;
4172 if (--LABEL_NUSES (lab) == 0)
4174 /* This can delete NEXT or PREV,
4175 either directly if NEXT is JUMP_LABEL (INSN),
4176 or indirectly through more levels of jumps. */
4177 delete_insn (lab);
4179 /* I feel a little doubtful about this loop,
4180 but I see no clean and sure alternative way
4181 to find the first insn after INSN that is not now deleted.
4182 I hope this works. */
4183 while (next && INSN_DELETED_P (next))
4184 next = NEXT_INSN (next);
4185 return next;
4187 else if ((lab_next = next_nonnote_insn (lab)) != NULL
4188 && GET_CODE (lab_next) == JUMP_INSN
4189 && (GET_CODE (PATTERN (lab_next)) == ADDR_VEC
4190 || GET_CODE (PATTERN (lab_next)) == ADDR_DIFF_VEC))
4192 /* If we're deleting the tablejump, delete the dispatch table.
4193 We may not be able to kill the label immediately preceeding
4194 just yet, as it might be referenced in code leading up to
4195 the tablejump. */
4196 delete_insn (lab_next);
4200 /* Likewise if we're deleting a dispatch table. */
4202 if (GET_CODE (insn) == JUMP_INSN
4203 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
4204 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
4206 rtx pat = PATTERN (insn);
4207 int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
4208 int len = XVECLEN (pat, diff_vec_p);
4210 for (i = 0; i < len; i++)
4211 if (--LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
4212 delete_insn (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
4213 while (next && INSN_DELETED_P (next))
4214 next = NEXT_INSN (next);
4215 return next;
4218 while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
4219 prev = PREV_INSN (prev);
4221 /* If INSN was a label and a dispatch table follows it,
4222 delete the dispatch table. The tablejump must have gone already.
4223 It isn't useful to fall through into a table. */
4225 if (was_code_label
4226 && NEXT_INSN (insn) != 0
4227 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
4228 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
4229 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
4230 next = delete_insn (NEXT_INSN (insn));
4232 /* If INSN was a label, delete insns following it if now unreachable. */
4234 if (was_code_label && prev && GET_CODE (prev) == BARRIER)
4236 register RTX_CODE code;
4237 while (next != 0
4238 && (GET_RTX_CLASS (code = GET_CODE (next)) == 'i'
4239 || code == NOTE || code == BARRIER
4240 || (code == CODE_LABEL && INSN_DELETED_P (next))))
4242 if (code == NOTE
4243 && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
4244 next = NEXT_INSN (next);
4245 /* Keep going past other deleted labels to delete what follows. */
4246 else if (code == CODE_LABEL && INSN_DELETED_P (next))
4247 next = NEXT_INSN (next);
4248 else
4249 /* Note: if this deletes a jump, it can cause more
4250 deletion of unreachable code, after a different label.
4251 As long as the value from this recursive call is correct,
4252 this invocation functions correctly. */
4253 next = delete_insn (next);
4257 return next;
4260 /* Advance from INSN till reaching something not deleted
4261 then return that. May return INSN itself. */
4264 next_nondeleted_insn (insn)
4265 rtx insn;
4267 while (INSN_DELETED_P (insn))
4268 insn = NEXT_INSN (insn);
4269 return insn;
4272 /* Delete a range of insns from FROM to TO, inclusive.
4273 This is for the sake of peephole optimization, so assume
4274 that whatever these insns do will still be done by a new
4275 peephole insn that will replace them. */
4277 void
4278 delete_for_peephole (from, to)
4279 register rtx from, to;
4281 register rtx insn = from;
4283 while (1)
4285 register rtx next = NEXT_INSN (insn);
4286 register rtx prev = PREV_INSN (insn);
4288 if (GET_CODE (insn) != NOTE)
4290 INSN_DELETED_P (insn) = 1;
4292 /* Patch this insn out of the chain. */
4293 /* We don't do this all at once, because we
4294 must preserve all NOTEs. */
4295 if (prev)
4296 NEXT_INSN (prev) = next;
4298 if (next)
4299 PREV_INSN (next) = prev;
4302 if (insn == to)
4303 break;
4304 insn = next;
4307 /* Note that if TO is an unconditional jump
4308 we *do not* delete the BARRIER that follows,
4309 since the peephole that replaces this sequence
4310 is also an unconditional jump in that case. */
4313 /* We have determined that INSN is never reached, and are about to
4314 delete it. Print a warning if the user asked for one.
4316 To try to make this warning more useful, this should only be called
4317 once per basic block not reached, and it only warns when the basic
4318 block contains more than one line from the current function, and
4319 contains at least one operation. CSE and inlining can duplicate insns,
4320 so it's possible to get spurious warnings from this. */
4322 void
4323 never_reached_warning (avoided_insn)
4324 rtx avoided_insn;
4326 rtx insn;
4327 rtx a_line_note = NULL;
4328 int two_avoided_lines = 0;
4329 int contains_insn = 0;
4331 if (! warn_notreached)
4332 return;
4334 /* Scan forwards, looking at LINE_NUMBER notes, until
4335 we hit a LABEL or we run out of insns. */
4337 for (insn = avoided_insn; insn != NULL; insn = NEXT_INSN (insn))
4339 if (GET_CODE (insn) == CODE_LABEL)
4340 break;
4341 else if (GET_CODE (insn) == NOTE /* A line number note? */
4342 && NOTE_LINE_NUMBER (insn) >= 0)
4344 if (a_line_note == NULL)
4345 a_line_note = insn;
4346 else
4347 two_avoided_lines |= (NOTE_LINE_NUMBER (a_line_note)
4348 != NOTE_LINE_NUMBER (insn));
4350 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4351 contains_insn = 1;
4353 if (two_avoided_lines && contains_insn)
4354 warning_with_file_and_line (NOTE_SOURCE_FILE (a_line_note),
4355 NOTE_LINE_NUMBER (a_line_note),
4356 "will never be executed");
4359 /* Invert the condition of the jump JUMP, and make it jump
4360 to label NLABEL instead of where it jumps now. */
4363 invert_jump (jump, nlabel)
4364 rtx jump, nlabel;
4366 /* We have to either invert the condition and change the label or
4367 do neither. Either operation could fail. We first try to invert
4368 the jump. If that succeeds, we try changing the label. If that fails,
4369 we invert the jump back to what it was. */
4371 if (! invert_exp (PATTERN (jump), jump))
4372 return 0;
4374 if (redirect_jump (jump, nlabel))
4376 if (flag_branch_probabilities)
4378 rtx note = find_reg_note (jump, REG_BR_PROB, 0);
4380 /* An inverted jump means that a probability taken becomes a
4381 probability not taken. Subtract the branch probability from the
4382 probability base to convert it back to a taken probability.
4383 (We don't flip the probability on a branch that's never taken. */
4384 if (note && XINT (XEXP (note, 0), 0) >= 0)
4385 XINT (XEXP (note, 0), 0) = REG_BR_PROB_BASE - XINT (XEXP (note, 0), 0);
4388 return 1;
4391 if (! invert_exp (PATTERN (jump), jump))
4392 /* This should just be putting it back the way it was. */
4393 abort ();
4395 return 0;
4398 /* Invert the jump condition of rtx X contained in jump insn, INSN.
4400 Return 1 if we can do so, 0 if we cannot find a way to do so that
4401 matches a pattern. */
4404 invert_exp (x, insn)
4405 rtx x;
4406 rtx insn;
4408 register RTX_CODE code;
4409 register int i;
4410 register const char *fmt;
4412 code = GET_CODE (x);
4414 if (code == IF_THEN_ELSE)
4416 register rtx comp = XEXP (x, 0);
4417 register rtx tem;
4419 /* We can do this in two ways: The preferable way, which can only
4420 be done if this is not an integer comparison, is to reverse
4421 the comparison code. Otherwise, swap the THEN-part and ELSE-part
4422 of the IF_THEN_ELSE. If we can't do either, fail. */
4424 if (can_reverse_comparison_p (comp, insn)
4425 && validate_change (insn, &XEXP (x, 0),
4426 gen_rtx_fmt_ee (reverse_condition (GET_CODE (comp)),
4427 GET_MODE (comp), XEXP (comp, 0),
4428 XEXP (comp, 1)), 0))
4429 return 1;
4431 tem = XEXP (x, 1);
4432 validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
4433 validate_change (insn, &XEXP (x, 2), tem, 1);
4434 return apply_change_group ();
4437 fmt = GET_RTX_FORMAT (code);
4438 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4440 if (fmt[i] == 'e')
4441 if (! invert_exp (XEXP (x, i), insn))
4442 return 0;
4443 if (fmt[i] == 'E')
4445 register int j;
4446 for (j = 0; j < XVECLEN (x, i); j++)
4447 if (!invert_exp (XVECEXP (x, i, j), insn))
4448 return 0;
4452 return 1;
4455 /* Make jump JUMP jump to label NLABEL instead of where it jumps now.
4456 If the old jump target label is unused as a result,
4457 it and the code following it may be deleted.
4459 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
4460 RETURN insn.
4462 The return value will be 1 if the change was made, 0 if it wasn't (this
4463 can only occur for NLABEL == 0). */
4466 redirect_jump (jump, nlabel)
4467 rtx jump, nlabel;
4469 register rtx olabel = JUMP_LABEL (jump);
4471 if (nlabel == olabel)
4472 return 1;
4474 if (! redirect_exp (&PATTERN (jump), olabel, nlabel, jump))
4475 return 0;
4477 /* If this is an unconditional branch, delete it from the jump_chain of
4478 OLABEL and add it to the jump_chain of NLABEL (assuming both labels
4479 have UID's in range and JUMP_CHAIN is valid). */
4480 if (jump_chain && (simplejump_p (jump)
4481 || GET_CODE (PATTERN (jump)) == RETURN))
4483 int label_index = nlabel ? INSN_UID (nlabel) : 0;
4485 delete_from_jump_chain (jump);
4486 if (label_index < max_jump_chain
4487 && INSN_UID (jump) < max_jump_chain)
4489 jump_chain[INSN_UID (jump)] = jump_chain[label_index];
4490 jump_chain[label_index] = jump;
4494 JUMP_LABEL (jump) = nlabel;
4495 if (nlabel)
4496 ++LABEL_NUSES (nlabel);
4498 if (olabel && --LABEL_NUSES (olabel) == 0)
4499 delete_insn (olabel);
4501 return 1;
4504 /* Delete the instruction JUMP from any jump chain it might be on. */
4506 static void
4507 delete_from_jump_chain (jump)
4508 rtx jump;
4510 int index;
4511 rtx olabel = JUMP_LABEL (jump);
4513 /* Handle unconditional jumps. */
4514 if (jump_chain && olabel != 0
4515 && INSN_UID (olabel) < max_jump_chain
4516 && simplejump_p (jump))
4517 index = INSN_UID (olabel);
4518 /* Handle return insns. */
4519 else if (jump_chain && GET_CODE (PATTERN (jump)) == RETURN)
4520 index = 0;
4521 else return;
4523 if (jump_chain[index] == jump)
4524 jump_chain[index] = jump_chain[INSN_UID (jump)];
4525 else
4527 rtx insn;
4529 for (insn = jump_chain[index];
4530 insn != 0;
4531 insn = jump_chain[INSN_UID (insn)])
4532 if (jump_chain[INSN_UID (insn)] == jump)
4534 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (jump)];
4535 break;
4540 /* If NLABEL is nonzero, throughout the rtx at LOC,
4541 alter (LABEL_REF OLABEL) to (LABEL_REF NLABEL). If OLABEL is
4542 zero, alter (RETURN) to (LABEL_REF NLABEL).
4544 If NLABEL is zero, alter (LABEL_REF OLABEL) to (RETURN) and check
4545 validity with validate_change. Convert (set (pc) (label_ref olabel))
4546 to (return).
4548 Return 0 if we found a change we would like to make but it is invalid.
4549 Otherwise, return 1. */
4552 redirect_exp (loc, olabel, nlabel, insn)
4553 rtx *loc;
4554 rtx olabel, nlabel;
4555 rtx insn;
4557 register rtx x = *loc;
4558 register RTX_CODE code = GET_CODE (x);
4559 register int i;
4560 register const char *fmt;
4562 if (code == LABEL_REF)
4564 if (XEXP (x, 0) == olabel)
4566 if (nlabel)
4567 XEXP (x, 0) = nlabel;
4568 else
4569 return validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 0);
4570 return 1;
4573 else if (code == RETURN && olabel == 0)
4575 x = gen_rtx_LABEL_REF (VOIDmode, nlabel);
4576 if (loc == &PATTERN (insn))
4577 x = gen_rtx_SET (VOIDmode, pc_rtx, x);
4578 return validate_change (insn, loc, x, 0);
4581 if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
4582 && GET_CODE (SET_SRC (x)) == LABEL_REF
4583 && XEXP (SET_SRC (x), 0) == olabel)
4584 return validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 0);
4586 fmt = GET_RTX_FORMAT (code);
4587 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4589 if (fmt[i] == 'e')
4590 if (! redirect_exp (&XEXP (x, i), olabel, nlabel, insn))
4591 return 0;
4592 if (fmt[i] == 'E')
4594 register int j;
4595 for (j = 0; j < XVECLEN (x, i); j++)
4596 if (! redirect_exp (&XVECEXP (x, i, j), olabel, nlabel, insn))
4597 return 0;
4601 return 1;
4604 /* Make jump JUMP jump to label NLABEL, assuming it used to be a tablejump.
4606 If the old jump target label (before the dispatch table) becomes unused,
4607 it and the dispatch table may be deleted. In that case, find the insn
4608 before the jump references that label and delete it and logical successors
4609 too. */
4611 static void
4612 redirect_tablejump (jump, nlabel)
4613 rtx jump, nlabel;
4615 register rtx olabel = JUMP_LABEL (jump);
4617 /* Add this jump to the jump_chain of NLABEL. */
4618 if (jump_chain && INSN_UID (nlabel) < max_jump_chain
4619 && INSN_UID (jump) < max_jump_chain)
4621 jump_chain[INSN_UID (jump)] = jump_chain[INSN_UID (nlabel)];
4622 jump_chain[INSN_UID (nlabel)] = jump;
4625 PATTERN (jump) = gen_jump (nlabel);
4626 JUMP_LABEL (jump) = nlabel;
4627 ++LABEL_NUSES (nlabel);
4628 INSN_CODE (jump) = -1;
4630 if (--LABEL_NUSES (olabel) == 0)
4632 delete_labelref_insn (jump, olabel, 0);
4633 delete_insn (olabel);
4637 /* Find the insn referencing LABEL that is a logical predecessor of INSN.
4638 If we found one, delete it and then delete this insn if DELETE_THIS is
4639 non-zero. Return non-zero if INSN or a predecessor references LABEL. */
4641 static int
4642 delete_labelref_insn (insn, label, delete_this)
4643 rtx insn, label;
4644 int delete_this;
4646 int deleted = 0;
4647 rtx link;
4649 if (GET_CODE (insn) != NOTE
4650 && reg_mentioned_p (label, PATTERN (insn)))
4652 if (delete_this)
4654 delete_insn (insn);
4655 deleted = 1;
4657 else
4658 return 1;
4661 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
4662 if (delete_labelref_insn (XEXP (link, 0), label, 1))
4664 if (delete_this)
4666 delete_insn (insn);
4667 deleted = 1;
4669 else
4670 return 1;
4673 return deleted;
4676 /* Like rtx_equal_p except that it considers two REGs as equal
4677 if they renumber to the same value and considers two commutative
4678 operations to be the same if the order of the operands has been
4679 reversed.
4681 ??? Addition is not commutative on the PA due to the weird implicit
4682 space register selection rules for memory addresses. Therefore, we
4683 don't consider a + b == b + a.
4685 We could/should make this test a little tighter. Possibly only
4686 disabling it on the PA via some backend macro or only disabling this
4687 case when the PLUS is inside a MEM. */
4690 rtx_renumbered_equal_p (x, y)
4691 rtx x, y;
4693 register int i;
4694 register RTX_CODE code = GET_CODE (x);
4695 register const char *fmt;
4697 if (x == y)
4698 return 1;
4700 if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
4701 && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
4702 && GET_CODE (SUBREG_REG (y)) == REG)))
4704 int reg_x = -1, reg_y = -1;
4705 int word_x = 0, word_y = 0;
4707 if (GET_MODE (x) != GET_MODE (y))
4708 return 0;
4710 /* If we haven't done any renumbering, don't
4711 make any assumptions. */
4712 if (reg_renumber == 0)
4713 return rtx_equal_p (x, y);
4715 if (code == SUBREG)
4717 reg_x = REGNO (SUBREG_REG (x));
4718 word_x = SUBREG_WORD (x);
4720 if (reg_renumber[reg_x] >= 0)
4722 reg_x = reg_renumber[reg_x] + word_x;
4723 word_x = 0;
4727 else
4729 reg_x = REGNO (x);
4730 if (reg_renumber[reg_x] >= 0)
4731 reg_x = reg_renumber[reg_x];
4734 if (GET_CODE (y) == SUBREG)
4736 reg_y = REGNO (SUBREG_REG (y));
4737 word_y = SUBREG_WORD (y);
4739 if (reg_renumber[reg_y] >= 0)
4741 reg_y = reg_renumber[reg_y];
4742 word_y = 0;
4746 else
4748 reg_y = REGNO (y);
4749 if (reg_renumber[reg_y] >= 0)
4750 reg_y = reg_renumber[reg_y];
4753 return reg_x >= 0 && reg_x == reg_y && word_x == word_y;
4756 /* Now we have disposed of all the cases
4757 in which different rtx codes can match. */
4758 if (code != GET_CODE (y))
4759 return 0;
4761 switch (code)
4763 case PC:
4764 case CC0:
4765 case ADDR_VEC:
4766 case ADDR_DIFF_VEC:
4767 return 0;
4769 case CONST_INT:
4770 return INTVAL (x) == INTVAL (y);
4772 case LABEL_REF:
4773 /* We can't assume nonlocal labels have their following insns yet. */
4774 if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
4775 return XEXP (x, 0) == XEXP (y, 0);
4777 /* Two label-refs are equivalent if they point at labels
4778 in the same position in the instruction stream. */
4779 return (next_real_insn (XEXP (x, 0))
4780 == next_real_insn (XEXP (y, 0)));
4782 case SYMBOL_REF:
4783 return XSTR (x, 0) == XSTR (y, 0);
4785 case CODE_LABEL:
4786 /* If we didn't match EQ equality above, they aren't the same. */
4787 return 0;
4789 default:
4790 break;
4793 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
4795 if (GET_MODE (x) != GET_MODE (y))
4796 return 0;
4798 /* For commutative operations, the RTX match if the operand match in any
4799 order. Also handle the simple binary and unary cases without a loop.
4801 ??? Don't consider PLUS a commutative operator; see comments above. */
4802 if ((code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
4803 && code != PLUS)
4804 return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
4805 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
4806 || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
4807 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
4808 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
4809 return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
4810 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
4811 else if (GET_RTX_CLASS (code) == '1')
4812 return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
4814 /* Compare the elements. If any pair of corresponding elements
4815 fail to match, return 0 for the whole things. */
4817 fmt = GET_RTX_FORMAT (code);
4818 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4820 register int j;
4821 switch (fmt[i])
4823 case 'w':
4824 if (XWINT (x, i) != XWINT (y, i))
4825 return 0;
4826 break;
4828 case 'i':
4829 if (XINT (x, i) != XINT (y, i))
4830 return 0;
4831 break;
4833 case 's':
4834 if (strcmp (XSTR (x, i), XSTR (y, i)))
4835 return 0;
4836 break;
4838 case 'e':
4839 if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
4840 return 0;
4841 break;
4843 case 'u':
4844 if (XEXP (x, i) != XEXP (y, i))
4845 return 0;
4846 /* fall through. */
4847 case '0':
4848 break;
4850 case 'E':
4851 if (XVECLEN (x, i) != XVECLEN (y, i))
4852 return 0;
4853 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4854 if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
4855 return 0;
4856 break;
4858 default:
4859 abort ();
4862 return 1;
4865 /* If X is a hard register or equivalent to one or a subregister of one,
4866 return the hard register number. If X is a pseudo register that was not
4867 assigned a hard register, return the pseudo register number. Otherwise,
4868 return -1. Any rtx is valid for X. */
4871 true_regnum (x)
4872 rtx x;
4874 if (GET_CODE (x) == REG)
4876 if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
4877 return reg_renumber[REGNO (x)];
4878 return REGNO (x);
4880 if (GET_CODE (x) == SUBREG)
4882 int base = true_regnum (SUBREG_REG (x));
4883 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
4884 return SUBREG_WORD (x) + base;
4886 return -1;
4889 /* Optimize code of the form:
4891 for (x = a[i]; x; ...)
4893 for (x = a[i]; x; ...)
4895 foo:
4897 Loop optimize will change the above code into
4899 if (x = a[i])
4900 for (;;)
4901 { ...; if (! (x = ...)) break; }
4902 if (x = a[i])
4903 for (;;)
4904 { ...; if (! (x = ...)) break; }
4905 foo:
4907 In general, if the first test fails, the program can branch
4908 directly to `foo' and skip the second try which is doomed to fail.
4909 We run this after loop optimization and before flow analysis. */
4911 /* When comparing the insn patterns, we track the fact that different
4912 pseudo-register numbers may have been used in each computation.
4913 The following array stores an equivalence -- same_regs[I] == J means
4914 that pseudo register I was used in the first set of tests in a context
4915 where J was used in the second set. We also count the number of such
4916 pending equivalences. If nonzero, the expressions really aren't the
4917 same. */
4919 static int *same_regs;
4921 static int num_same_regs;
4923 /* Track any registers modified between the target of the first jump and
4924 the second jump. They never compare equal. */
4926 static char *modified_regs;
4928 /* Record if memory was modified. */
4930 static int modified_mem;
4932 /* Called via note_stores on each insn between the target of the first
4933 branch and the second branch. It marks any changed registers. */
4935 static void
4936 mark_modified_reg (dest, x)
4937 rtx dest;
4938 rtx x ATTRIBUTE_UNUSED;
4940 int regno, i;
4942 if (GET_CODE (dest) == SUBREG)
4943 dest = SUBREG_REG (dest);
4945 if (GET_CODE (dest) == MEM)
4946 modified_mem = 1;
4948 if (GET_CODE (dest) != REG)
4949 return;
4951 regno = REGNO (dest);
4952 if (regno >= FIRST_PSEUDO_REGISTER)
4953 modified_regs[regno] = 1;
4954 else
4955 for (i = 0; i < HARD_REGNO_NREGS (regno, GET_MODE (dest)); i++)
4956 modified_regs[regno + i] = 1;
4959 /* F is the first insn in the chain of insns. */
4961 void
4962 thread_jumps (f, max_reg, flag_before_loop)
4963 rtx f;
4964 int max_reg;
4965 int flag_before_loop;
4967 /* Basic algorithm is to find a conditional branch,
4968 the label it may branch to, and the branch after
4969 that label. If the two branches test the same condition,
4970 walk back from both branch paths until the insn patterns
4971 differ, or code labels are hit. If we make it back to
4972 the target of the first branch, then we know that the first branch
4973 will either always succeed or always fail depending on the relative
4974 senses of the two branches. So adjust the first branch accordingly
4975 in this case. */
4977 rtx label, b1, b2, t1, t2;
4978 enum rtx_code code1, code2;
4979 rtx b1op0, b1op1, b2op0, b2op1;
4980 int changed = 1;
4981 int i;
4982 int *all_reset;
4984 /* Allocate register tables and quick-reset table. */
4985 modified_regs = (char *) alloca (max_reg * sizeof (char));
4986 same_regs = (int *) alloca (max_reg * sizeof (int));
4987 all_reset = (int *) alloca (max_reg * sizeof (int));
4988 for (i = 0; i < max_reg; i++)
4989 all_reset[i] = -1;
4991 while (changed)
4993 changed = 0;
4995 for (b1 = f; b1; b1 = NEXT_INSN (b1))
4997 /* Get to a candidate branch insn. */
4998 if (GET_CODE (b1) != JUMP_INSN
4999 || ! condjump_p (b1) || simplejump_p (b1)
5000 || JUMP_LABEL (b1) == 0)
5001 continue;
5003 bzero (modified_regs, max_reg * sizeof (char));
5004 modified_mem = 0;
5006 bcopy ((char *) all_reset, (char *) same_regs,
5007 max_reg * sizeof (int));
5008 num_same_regs = 0;
5010 label = JUMP_LABEL (b1);
5012 /* Look for a branch after the target. Record any registers and
5013 memory modified between the target and the branch. Stop when we
5014 get to a label since we can't know what was changed there. */
5015 for (b2 = NEXT_INSN (label); b2; b2 = NEXT_INSN (b2))
5017 if (GET_CODE (b2) == CODE_LABEL)
5018 break;
5020 else if (GET_CODE (b2) == JUMP_INSN)
5022 /* If this is an unconditional jump and is the only use of
5023 its target label, we can follow it. */
5024 if (simplejump_p (b2)
5025 && JUMP_LABEL (b2) != 0
5026 && LABEL_NUSES (JUMP_LABEL (b2)) == 1)
5028 b2 = JUMP_LABEL (b2);
5029 continue;
5031 else
5032 break;
5035 if (GET_CODE (b2) != CALL_INSN && GET_CODE (b2) != INSN)
5036 continue;
5038 if (GET_CODE (b2) == CALL_INSN)
5040 modified_mem = 1;
5041 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5042 if (call_used_regs[i] && ! fixed_regs[i]
5043 && i != STACK_POINTER_REGNUM
5044 && i != FRAME_POINTER_REGNUM
5045 && i != HARD_FRAME_POINTER_REGNUM
5046 && i != ARG_POINTER_REGNUM)
5047 modified_regs[i] = 1;
5050 note_stores (PATTERN (b2), mark_modified_reg);
5053 /* Check the next candidate branch insn from the label
5054 of the first. */
5055 if (b2 == 0
5056 || GET_CODE (b2) != JUMP_INSN
5057 || b2 == b1
5058 || ! condjump_p (b2)
5059 || simplejump_p (b2))
5060 continue;
5062 /* Get the comparison codes and operands, reversing the
5063 codes if appropriate. If we don't have comparison codes,
5064 we can't do anything. */
5065 b1op0 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 0);
5066 b1op1 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 1);
5067 code1 = GET_CODE (XEXP (SET_SRC (PATTERN (b1)), 0));
5068 if (XEXP (SET_SRC (PATTERN (b1)), 1) == pc_rtx)
5069 code1 = reverse_condition (code1);
5071 b2op0 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 0);
5072 b2op1 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 1);
5073 code2 = GET_CODE (XEXP (SET_SRC (PATTERN (b2)), 0));
5074 if (XEXP (SET_SRC (PATTERN (b2)), 1) == pc_rtx)
5075 code2 = reverse_condition (code2);
5077 /* If they test the same things and knowing that B1 branches
5078 tells us whether or not B2 branches, check if we
5079 can thread the branch. */
5080 if (rtx_equal_for_thread_p (b1op0, b2op0, b2)
5081 && rtx_equal_for_thread_p (b1op1, b2op1, b2)
5082 && (comparison_dominates_p (code1, code2)
5083 || (comparison_dominates_p (code1, reverse_condition (code2))
5084 && can_reverse_comparison_p (XEXP (SET_SRC (PATTERN (b1)),
5086 b1))))
5088 t1 = prev_nonnote_insn (b1);
5089 t2 = prev_nonnote_insn (b2);
5091 while (t1 != 0 && t2 != 0)
5093 if (t2 == label)
5095 /* We have reached the target of the first branch.
5096 If there are no pending register equivalents,
5097 we know that this branch will either always
5098 succeed (if the senses of the two branches are
5099 the same) or always fail (if not). */
5100 rtx new_label;
5102 if (num_same_regs != 0)
5103 break;
5105 if (comparison_dominates_p (code1, code2))
5106 new_label = JUMP_LABEL (b2);
5107 else
5108 new_label = get_label_after (b2);
5110 if (JUMP_LABEL (b1) != new_label)
5112 rtx prev = PREV_INSN (new_label);
5114 if (flag_before_loop
5115 && GET_CODE (prev) == NOTE
5116 && NOTE_LINE_NUMBER (prev) == NOTE_INSN_LOOP_BEG)
5118 /* Don't thread to the loop label. If a loop
5119 label is reused, loop optimization will
5120 be disabled for that loop. */
5121 new_label = gen_label_rtx ();
5122 emit_label_after (new_label, PREV_INSN (prev));
5124 changed |= redirect_jump (b1, new_label);
5126 break;
5129 /* If either of these is not a normal insn (it might be
5130 a JUMP_INSN, CALL_INSN, or CODE_LABEL) we fail. (NOTEs
5131 have already been skipped above.) Similarly, fail
5132 if the insns are different. */
5133 if (GET_CODE (t1) != INSN || GET_CODE (t2) != INSN
5134 || recog_memoized (t1) != recog_memoized (t2)
5135 || ! rtx_equal_for_thread_p (PATTERN (t1),
5136 PATTERN (t2), t2))
5137 break;
5139 t1 = prev_nonnote_insn (t1);
5140 t2 = prev_nonnote_insn (t2);
5147 /* This is like RTX_EQUAL_P except that it knows about our handling of
5148 possibly equivalent registers and knows to consider volatile and
5149 modified objects as not equal.
5151 YINSN is the insn containing Y. */
5154 rtx_equal_for_thread_p (x, y, yinsn)
5155 rtx x, y;
5156 rtx yinsn;
5158 register int i;
5159 register int j;
5160 register enum rtx_code code;
5161 register const char *fmt;
5163 code = GET_CODE (x);
5164 /* Rtx's of different codes cannot be equal. */
5165 if (code != GET_CODE (y))
5166 return 0;
5168 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
5169 (REG:SI x) and (REG:HI x) are NOT equivalent. */
5171 if (GET_MODE (x) != GET_MODE (y))
5172 return 0;
5174 /* For floating-point, consider everything unequal. This is a bit
5175 pessimistic, but this pass would only rarely do anything for FP
5176 anyway. */
5177 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
5178 && FLOAT_MODE_P (GET_MODE (x)) && ! flag_fast_math)
5179 return 0;
5181 /* For commutative operations, the RTX match if the operand match in any
5182 order. Also handle the simple binary and unary cases without a loop. */
5183 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
5184 return ((rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
5185 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn))
5186 || (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 1), yinsn)
5187 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 0), yinsn)));
5188 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
5189 return (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
5190 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn));
5191 else if (GET_RTX_CLASS (code) == '1')
5192 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
5194 /* Handle special-cases first. */
5195 switch (code)
5197 case REG:
5198 if (REGNO (x) == REGNO (y) && ! modified_regs[REGNO (x)])
5199 return 1;
5201 /* If neither is user variable or hard register, check for possible
5202 equivalence. */
5203 if (REG_USERVAR_P (x) || REG_USERVAR_P (y)
5204 || REGNO (x) < FIRST_PSEUDO_REGISTER
5205 || REGNO (y) < FIRST_PSEUDO_REGISTER)
5206 return 0;
5208 if (same_regs[REGNO (x)] == -1)
5210 same_regs[REGNO (x)] = REGNO (y);
5211 num_same_regs++;
5213 /* If this is the first time we are seeing a register on the `Y'
5214 side, see if it is the last use. If not, we can't thread the
5215 jump, so mark it as not equivalent. */
5216 if (REGNO_LAST_UID (REGNO (y)) != INSN_UID (yinsn))
5217 return 0;
5219 return 1;
5221 else
5222 return (same_regs[REGNO (x)] == REGNO (y));
5224 break;
5226 case MEM:
5227 /* If memory modified or either volatile, not equivalent.
5228 Else, check address. */
5229 if (modified_mem || MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
5230 return 0;
5232 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
5234 case ASM_INPUT:
5235 if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
5236 return 0;
5238 break;
5240 case SET:
5241 /* Cancel a pending `same_regs' if setting equivalenced registers.
5242 Then process source. */
5243 if (GET_CODE (SET_DEST (x)) == REG
5244 && GET_CODE (SET_DEST (y)) == REG)
5246 if (same_regs[REGNO (SET_DEST (x))] == REGNO (SET_DEST (y)))
5248 same_regs[REGNO (SET_DEST (x))] = -1;
5249 num_same_regs--;
5251 else if (REGNO (SET_DEST (x)) != REGNO (SET_DEST (y)))
5252 return 0;
5254 else
5255 if (rtx_equal_for_thread_p (SET_DEST (x), SET_DEST (y), yinsn) == 0)
5256 return 0;
5258 return rtx_equal_for_thread_p (SET_SRC (x), SET_SRC (y), yinsn);
5260 case LABEL_REF:
5261 return XEXP (x, 0) == XEXP (y, 0);
5263 case SYMBOL_REF:
5264 return XSTR (x, 0) == XSTR (y, 0);
5266 default:
5267 break;
5270 if (x == y)
5271 return 1;
5273 fmt = GET_RTX_FORMAT (code);
5274 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5276 switch (fmt[i])
5278 case 'w':
5279 if (XWINT (x, i) != XWINT (y, i))
5280 return 0;
5281 break;
5283 case 'n':
5284 case 'i':
5285 if (XINT (x, i) != XINT (y, i))
5286 return 0;
5287 break;
5289 case 'V':
5290 case 'E':
5291 /* Two vectors must have the same length. */
5292 if (XVECLEN (x, i) != XVECLEN (y, i))
5293 return 0;
5295 /* And the corresponding elements must match. */
5296 for (j = 0; j < XVECLEN (x, i); j++)
5297 if (rtx_equal_for_thread_p (XVECEXP (x, i, j),
5298 XVECEXP (y, i, j), yinsn) == 0)
5299 return 0;
5300 break;
5302 case 'e':
5303 if (rtx_equal_for_thread_p (XEXP (x, i), XEXP (y, i), yinsn) == 0)
5304 return 0;
5305 break;
5307 case 'S':
5308 case 's':
5309 if (strcmp (XSTR (x, i), XSTR (y, i)))
5310 return 0;
5311 break;
5313 case 'u':
5314 /* These are just backpointers, so they don't matter. */
5315 break;
5317 case '0':
5318 case 't':
5319 break;
5321 /* It is believed that rtx's at this level will never
5322 contain anything but integers and other rtx's,
5323 except for within LABEL_REFs and SYMBOL_REFs. */
5324 default:
5325 abort ();
5328 return 1;
5332 #ifndef HAVE_cc0
5333 /* Return the insn that NEW can be safely inserted in front of starting at
5334 the jump insn INSN. Return 0 if it is not safe to do this jump
5335 optimization. Note that NEW must contain a single set. */
5337 static rtx
5338 find_insert_position (insn, new)
5339 rtx insn;
5340 rtx new;
5342 int i;
5343 rtx prev;
5345 /* If NEW does not clobber, it is safe to insert NEW before INSN. */
5346 if (GET_CODE (PATTERN (new)) != PARALLEL)
5347 return insn;
5349 for (i = XVECLEN (PATTERN (new), 0) - 1; i >= 0; i--)
5350 if (GET_CODE (XVECEXP (PATTERN (new), 0, i)) == CLOBBER
5351 && reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (new), 0, i), 0),
5352 insn))
5353 break;
5355 if (i < 0)
5356 return insn;
5358 /* There is a good chance that the previous insn PREV sets the thing
5359 being clobbered (often the CC in a hard reg). If PREV does not
5360 use what NEW sets, we can insert NEW before PREV. */
5362 prev = prev_active_insn (insn);
5363 for (i = XVECLEN (PATTERN (new), 0) - 1; i >= 0; i--)
5364 if (GET_CODE (XVECEXP (PATTERN (new), 0, i)) == CLOBBER
5365 && reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (new), 0, i), 0),
5366 insn)
5367 && ! modified_in_p (XEXP (XVECEXP (PATTERN (new), 0, i), 0),
5368 prev))
5369 return 0;
5371 return reg_mentioned_p (SET_DEST (single_set (new)), prev) ? 0 : prev;
5373 #endif /* !HAVE_cc0 */