gcse.c (run_jump_opt_after_gcse): New variable.
[gcc.git] / gcc / jump.c
1 /* Optimize jump instructions, for GNU compiler.
2 Copyright (C) 1987, 88, 89, 91-98, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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.
10
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.
15
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. */
20
21
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).
25
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.
30
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.
38
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'.
43
44 Jump optimization is done after cse when cse's constant-propagation
45 causes jumps to become unconditional or to be deleted.
46
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.
50
51 The subroutines delete_insn, redirect_jump, and invert_jump are used
52 from other passes as well. */
53
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 "expr.h"
65 #include "real.h"
66 #include "except.h"
67 #include "toplev.h"
68
69 /* ??? Eventually must record somehow the labels used by jumps
70 from nested functions. */
71 /* Pre-record the next or previous real insn for each label?
72 No, this pass is very fast anyway. */
73 /* Condense consecutive labels?
74 This would make life analysis faster, maybe. */
75 /* Optimize jump y; x: ... y: jumpif... x?
76 Don't know if it is worth bothering with. */
77 /* Optimize two cases of conditional jump to conditional jump?
78 This can never delete any instruction or make anything dead,
79 or even change what is live at any point.
80 So perhaps let combiner do it. */
81
82 /* Vector indexed by uid.
83 For each CODE_LABEL, index by its uid to get first unconditional jump
84 that jumps to the label.
85 For each JUMP_INSN, index by its uid to get the next unconditional jump
86 that jumps to the same label.
87 Element 0 is the start of a chain of all return insns.
88 (It is safe to use element 0 because insn uid 0 is not used. */
89
90 static rtx *jump_chain;
91
92 /* List of labels referred to from initializers.
93 These can never be deleted. */
94 rtx forced_labels;
95
96 /* Maximum index in jump_chain. */
97
98 static int max_jump_chain;
99
100 /* Set nonzero by jump_optimize if control can fall through
101 to the end of the function. */
102 int can_reach_end;
103
104 /* Indicates whether death notes are significant in cross jump analysis.
105 Normally they are not significant, because of A and B jump to C,
106 and R dies in A, it must die in B. But this might not be true after
107 stack register conversion, and we must compare death notes in that
108 case. */
109
110 static int cross_jump_death_matters = 0;
111
112 static int init_label_info PROTO((rtx));
113 static void delete_barrier_successors PROTO((rtx));
114 static void mark_all_labels PROTO((rtx, int));
115 static rtx delete_unreferenced_labels PROTO((rtx));
116 static void delete_noop_moves PROTO((rtx));
117 static int calculate_can_reach_end PROTO((rtx, int, int));
118 static int duplicate_loop_exit_test PROTO((rtx));
119 static void find_cross_jump PROTO((rtx, rtx, int, rtx *, rtx *));
120 static void do_cross_jump PROTO((rtx, rtx, rtx));
121 static int jump_back_p PROTO((rtx, rtx));
122 static int tension_vector_labels PROTO((rtx, int));
123 static void mark_jump_label PROTO((rtx, rtx, int));
124 static void delete_computation PROTO((rtx));
125 static void delete_from_jump_chain PROTO((rtx));
126 static int delete_labelref_insn PROTO((rtx, rtx, int));
127 static void mark_modified_reg PROTO((rtx, rtx));
128 static void redirect_tablejump PROTO((rtx, rtx));
129 #ifndef HAVE_cc0
130 static rtx find_insert_position PROTO((rtx, rtx));
131 #endif
132 \f
133 /* Delete no-op jumps and optimize jumps to jumps
134 and jumps around jumps.
135 Delete unused labels and unreachable code.
136
137 If CROSS_JUMP is 1, detect matching code
138 before a jump and its destination and unify them.
139 If CROSS_JUMP is 2, do cross-jumping, but pay attention to death notes.
140
141 If NOOP_MOVES is nonzero, delete no-op move insns.
142
143 If AFTER_REGSCAN is nonzero, then this jump pass is being run immediately
144 after regscan, and it is safe to use regno_first_uid and regno_last_uid.
145
146 If `optimize' is zero, don't change any code,
147 just determine whether control drops off the end of the function.
148 This case occurs when we have -W and not -O.
149 It works because `delete_insn' checks the value of `optimize'
150 and refrains from actually deleting when that is 0. */
151
152 void
153 jump_optimize (f, cross_jump, noop_moves, after_regscan)
154 rtx f;
155 int cross_jump;
156 int noop_moves;
157 int after_regscan;
158 {
159 register rtx insn, next;
160 int changed;
161 int old_max_reg;
162 int first = 1;
163 int max_uid = 0;
164 rtx last_insn;
165
166 cross_jump_death_matters = (cross_jump == 2);
167 max_uid = init_label_info (f) + 1;
168
169 /* If we are performing cross jump optimizations, then initialize
170 tables mapping UIDs to EH regions to avoid incorrect movement
171 of insns from one EH region to another. */
172 if (flag_exceptions && cross_jump)
173 init_insn_eh_region (f, max_uid);
174
175 delete_barrier_successors (f);
176
177 /* Leave some extra room for labels and duplicate exit test insns
178 we make. */
179 max_jump_chain = max_uid * 14 / 10;
180 jump_chain = (rtx *) alloca (max_jump_chain * sizeof (rtx));
181 bzero ((char *) jump_chain, max_jump_chain * sizeof (rtx));
182
183 mark_all_labels (f, cross_jump);
184
185 /* Keep track of labels used from static data;
186 they cannot ever be deleted. */
187
188 for (insn = forced_labels; insn; insn = XEXP (insn, 1))
189 LABEL_NUSES (XEXP (insn, 0))++;
190
191 check_exception_handler_labels ();
192
193 /* Keep track of labels used for marking handlers for exception
194 regions; they cannot usually be deleted. */
195
196 for (insn = exception_handler_labels; insn; insn = XEXP (insn, 1))
197 LABEL_NUSES (XEXP (insn, 0))++;
198
199 exception_optimize ();
200
201 last_insn = delete_unreferenced_labels (f);
202
203 if (!optimize)
204 {
205 can_reach_end = calculate_can_reach_end (last_insn, 1, 0);
206
207 /* Zero the "deleted" flag of all the "deleted" insns. */
208 for (insn = f; insn; insn = NEXT_INSN (insn))
209 INSN_DELETED_P (insn) = 0;
210
211 /* Show that the jump chain is not valid. */
212 jump_chain = 0;
213 return;
214 }
215
216 #ifdef HAVE_return
217 if (HAVE_return)
218 {
219 /* If we fall through to the epilogue, see if we can insert a RETURN insn
220 in front of it. If the machine allows it at this point (we might be
221 after reload for a leaf routine), it will improve optimization for it
222 to be there. */
223 insn = get_last_insn ();
224 while (insn && GET_CODE (insn) == NOTE)
225 insn = PREV_INSN (insn);
226
227 if (insn && GET_CODE (insn) != BARRIER)
228 {
229 emit_jump_insn (gen_return ());
230 emit_barrier ();
231 }
232 }
233 #endif
234
235 if (noop_moves)
236 delete_noop_moves (f);
237
238 /* If we haven't yet gotten to reload and we have just run regscan,
239 delete any insn that sets a register that isn't used elsewhere.
240 This helps some of the optimizations below by having less insns
241 being jumped around. */
242
243 if (! reload_completed && after_regscan)
244 for (insn = f; insn; insn = next)
245 {
246 rtx set = single_set (insn);
247
248 next = NEXT_INSN (insn);
249
250 if (set && GET_CODE (SET_DEST (set)) == REG
251 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
252 && REGNO_FIRST_UID (REGNO (SET_DEST (set))) == INSN_UID (insn)
253 /* We use regno_last_note_uid so as not to delete the setting
254 of a reg that's used in notes. A subsequent optimization
255 might arrange to use that reg for real. */
256 && REGNO_LAST_NOTE_UID (REGNO (SET_DEST (set))) == INSN_UID (insn)
257 && ! side_effects_p (SET_SRC (set))
258 && ! find_reg_note (insn, REG_RETVAL, 0))
259 delete_insn (insn);
260 }
261
262 /* Now iterate optimizing jumps until nothing changes over one pass. */
263 changed = 1;
264 old_max_reg = max_reg_num ();
265 while (changed)
266 {
267 changed = 0;
268
269 for (insn = f; insn; insn = next)
270 {
271 rtx reallabelprev;
272 rtx temp, temp1, temp2, temp3, temp4, temp5, temp6;
273 rtx nlabel;
274 int this_is_simplejump, this_is_condjump, reversep = 0;
275 int this_is_condjump_in_parallel;
276
277 #if 0
278 /* If NOT the first iteration, if this is the last jump pass
279 (just before final), do the special peephole optimizations.
280 Avoiding the first iteration gives ordinary jump opts
281 a chance to work before peephole opts. */
282
283 if (reload_completed && !first && !flag_no_peephole)
284 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
285 peephole (insn);
286 #endif
287
288 /* That could have deleted some insns after INSN, so check now
289 what the following insn is. */
290
291 next = NEXT_INSN (insn);
292
293 /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
294 jump. Try to optimize by duplicating the loop exit test if so.
295 This is only safe immediately after regscan, because it uses
296 the values of regno_first_uid and regno_last_uid. */
297 if (after_regscan && GET_CODE (insn) == NOTE
298 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
299 && (temp1 = next_nonnote_insn (insn)) != 0
300 && simplejump_p (temp1))
301 {
302 temp = PREV_INSN (insn);
303 if (duplicate_loop_exit_test (insn))
304 {
305 changed = 1;
306 next = NEXT_INSN (temp);
307 continue;
308 }
309 }
310
311 if (GET_CODE (insn) != JUMP_INSN)
312 continue;
313
314 this_is_simplejump = simplejump_p (insn);
315 this_is_condjump = condjump_p (insn);
316 this_is_condjump_in_parallel = condjump_in_parallel_p (insn);
317
318 /* Tension the labels in dispatch tables. */
319
320 if (GET_CODE (PATTERN (insn)) == ADDR_VEC)
321 changed |= tension_vector_labels (PATTERN (insn), 0);
322 if (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
323 changed |= tension_vector_labels (PATTERN (insn), 1);
324
325 /* If a dispatch table always goes to the same place,
326 get rid of it and replace the insn that uses it. */
327
328 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
329 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
330 {
331 int i;
332 rtx pat = PATTERN (insn);
333 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
334 int len = XVECLEN (pat, diff_vec_p);
335 rtx dispatch = prev_real_insn (insn);
336
337 for (i = 0; i < len; i++)
338 if (XEXP (XVECEXP (pat, diff_vec_p, i), 0)
339 != XEXP (XVECEXP (pat, diff_vec_p, 0), 0))
340 break;
341 if (i == len
342 && dispatch != 0
343 && GET_CODE (dispatch) == JUMP_INSN
344 && JUMP_LABEL (dispatch) != 0
345 /* Don't mess with a casesi insn. */
346 && !(GET_CODE (PATTERN (dispatch)) == SET
347 && (GET_CODE (SET_SRC (PATTERN (dispatch)))
348 == IF_THEN_ELSE))
349 && next_real_insn (JUMP_LABEL (dispatch)) == insn)
350 {
351 redirect_tablejump (dispatch,
352 XEXP (XVECEXP (pat, diff_vec_p, 0), 0));
353 changed = 1;
354 }
355 }
356
357 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
358
359 /* If a jump references the end of the function, try to turn
360 it into a RETURN insn, possibly a conditional one. */
361 if (JUMP_LABEL (insn)
362 && (next_active_insn (JUMP_LABEL (insn)) == 0
363 || GET_CODE (PATTERN (next_active_insn (JUMP_LABEL (insn))))
364 == RETURN))
365 changed |= redirect_jump (insn, NULL_RTX);
366
367 /* Detect jump to following insn. */
368 if (reallabelprev == insn && condjump_p (insn))
369 {
370 next = next_real_insn (JUMP_LABEL (insn));
371 delete_jump (insn);
372 changed = 1;
373 continue;
374 }
375
376 /* If we have an unconditional jump preceded by a USE, try to put
377 the USE before the target and jump there. This simplifies many
378 of the optimizations below since we don't have to worry about
379 dealing with these USE insns. We only do this if the label
380 being branch to already has the identical USE or if code
381 never falls through to that label. */
382
383 if (this_is_simplejump
384 && (temp = prev_nonnote_insn (insn)) != 0
385 && GET_CODE (temp) == INSN && GET_CODE (PATTERN (temp)) == USE
386 && (temp1 = prev_nonnote_insn (JUMP_LABEL (insn))) != 0
387 && (GET_CODE (temp1) == BARRIER
388 || (GET_CODE (temp1) == INSN
389 && rtx_equal_p (PATTERN (temp), PATTERN (temp1))))
390 /* Don't do this optimization if we have a loop containing only
391 the USE instruction, and the loop start label has a usage
392 count of 1. This is because we will redo this optimization
393 everytime through the outer loop, and jump opt will never
394 exit. */
395 && ! ((temp2 = prev_nonnote_insn (temp)) != 0
396 && temp2 == JUMP_LABEL (insn)
397 && LABEL_NUSES (temp2) == 1))
398 {
399 if (GET_CODE (temp1) == BARRIER)
400 {
401 emit_insn_after (PATTERN (temp), temp1);
402 temp1 = NEXT_INSN (temp1);
403 }
404
405 delete_insn (temp);
406 redirect_jump (insn, get_label_before (temp1));
407 reallabelprev = prev_real_insn (temp1);
408 changed = 1;
409 }
410
411 /* Simplify if (...) x = a; else x = b; by converting it
412 to x = b; if (...) x = a;
413 if B is sufficiently simple, the test doesn't involve X,
414 and nothing in the test modifies B or X.
415
416 If we have small register classes, we also can't do this if X
417 is a hard register.
418
419 If the "x = b;" insn has any REG_NOTES, we don't do this because
420 of the possibility that we are running after CSE and there is a
421 REG_EQUAL note that is only valid if the branch has already been
422 taken. If we move the insn with the REG_EQUAL note, we may
423 fold the comparison to always be false in a later CSE pass.
424 (We could also delete the REG_NOTES when moving the insn, but it
425 seems simpler to not move it.) An exception is that we can move
426 the insn if the only note is a REG_EQUAL or REG_EQUIV whose
427 value is the same as "b".
428
429 INSN is the branch over the `else' part.
430
431 We set:
432
433 TEMP to the jump insn preceding "x = a;"
434 TEMP1 to X
435 TEMP2 to the insn that sets "x = b;"
436 TEMP3 to the insn that sets "x = a;"
437 TEMP4 to the set of "x = b"; */
438
439 if (this_is_simplejump
440 && (temp3 = prev_active_insn (insn)) != 0
441 && GET_CODE (temp3) == INSN
442 && (temp4 = single_set (temp3)) != 0
443 && GET_CODE (temp1 = SET_DEST (temp4)) == REG
444 && (! SMALL_REGISTER_CLASSES
445 || REGNO (temp1) >= FIRST_PSEUDO_REGISTER)
446 && (temp2 = next_active_insn (insn)) != 0
447 && GET_CODE (temp2) == INSN
448 && (temp4 = single_set (temp2)) != 0
449 && rtx_equal_p (SET_DEST (temp4), temp1)
450 && ! side_effects_p (SET_SRC (temp4))
451 && ! may_trap_p (SET_SRC (temp4))
452 && (REG_NOTES (temp2) == 0
453 || ((REG_NOTE_KIND (REG_NOTES (temp2)) == REG_EQUAL
454 || REG_NOTE_KIND (REG_NOTES (temp2)) == REG_EQUIV)
455 && XEXP (REG_NOTES (temp2), 1) == 0
456 && rtx_equal_p (XEXP (REG_NOTES (temp2), 0),
457 SET_SRC (temp4))))
458 && (temp = prev_active_insn (temp3)) != 0
459 && condjump_p (temp) && ! simplejump_p (temp)
460 /* TEMP must skip over the "x = a;" insn */
461 && prev_real_insn (JUMP_LABEL (temp)) == insn
462 && no_labels_between_p (insn, JUMP_LABEL (temp))
463 /* There must be no other entries to the "x = b;" insn. */
464 && no_labels_between_p (JUMP_LABEL (temp), temp2)
465 /* INSN must either branch to the insn after TEMP2 or the insn
466 after TEMP2 must branch to the same place as INSN. */
467 && (reallabelprev == temp2
468 || ((temp5 = next_active_insn (temp2)) != 0
469 && simplejump_p (temp5)
470 && JUMP_LABEL (temp5) == JUMP_LABEL (insn))))
471 {
472 /* The test expression, X, may be a complicated test with
473 multiple branches. See if we can find all the uses of
474 the label that TEMP branches to without hitting a CALL_INSN
475 or a jump to somewhere else. */
476 rtx target = JUMP_LABEL (temp);
477 int nuses = LABEL_NUSES (target);
478 rtx p;
479 #ifdef HAVE_cc0
480 rtx q;
481 #endif
482
483 /* Set P to the first jump insn that goes around "x = a;". */
484 for (p = temp; nuses && p; p = prev_nonnote_insn (p))
485 {
486 if (GET_CODE (p) == JUMP_INSN)
487 {
488 if (condjump_p (p) && ! simplejump_p (p)
489 && JUMP_LABEL (p) == target)
490 {
491 nuses--;
492 if (nuses == 0)
493 break;
494 }
495 else
496 break;
497 }
498 else if (GET_CODE (p) == CALL_INSN)
499 break;
500 }
501
502 #ifdef HAVE_cc0
503 /* We cannot insert anything between a set of cc and its use
504 so if P uses cc0, we must back up to the previous insn. */
505 q = prev_nonnote_insn (p);
506 if (q && GET_RTX_CLASS (GET_CODE (q)) == 'i'
507 && sets_cc0_p (PATTERN (q)))
508 p = q;
509 #endif
510
511 if (p)
512 p = PREV_INSN (p);
513
514 /* If we found all the uses and there was no data conflict, we
515 can move the assignment unless we can branch into the middle
516 from somewhere. */
517 if (nuses == 0 && p
518 && no_labels_between_p (p, insn)
519 && ! reg_referenced_between_p (temp1, p, NEXT_INSN (temp3))
520 && ! reg_set_between_p (temp1, p, temp3)
521 && (GET_CODE (SET_SRC (temp4)) == CONST_INT
522 || ! modified_between_p (SET_SRC (temp4), p, temp2))
523 /* Verify that registers used by the jump are not clobbered
524 by the instruction being moved. */
525 && ! regs_set_between_p (PATTERN (temp),
526 PREV_INSN (temp2),
527 NEXT_INSN (temp2)))
528 {
529 emit_insn_after_with_line_notes (PATTERN (temp2), p, temp2);
530 delete_insn (temp2);
531
532 /* Set NEXT to an insn that we know won't go away. */
533 next = next_active_insn (insn);
534
535 /* Delete the jump around the set. Note that we must do
536 this before we redirect the test jumps so that it won't
537 delete the code immediately following the assignment
538 we moved (which might be a jump). */
539
540 delete_insn (insn);
541
542 /* We either have two consecutive labels or a jump to
543 a jump, so adjust all the JUMP_INSNs to branch to where
544 INSN branches to. */
545 for (p = NEXT_INSN (p); p != next; p = NEXT_INSN (p))
546 if (GET_CODE (p) == JUMP_INSN)
547 redirect_jump (p, target);
548
549 changed = 1;
550 continue;
551 }
552 }
553
554 /* Simplify if (...) { x = a; goto l; } x = b; by converting it
555 to x = a; if (...) goto l; x = b;
556 if A is sufficiently simple, the test doesn't involve X,
557 and nothing in the test modifies A or X.
558
559 If we have small register classes, we also can't do this if X
560 is a hard register.
561
562 If the "x = a;" insn has any REG_NOTES, we don't do this because
563 of the possibility that we are running after CSE and there is a
564 REG_EQUAL note that is only valid if the branch has already been
565 taken. If we move the insn with the REG_EQUAL note, we may
566 fold the comparison to always be false in a later CSE pass.
567 (We could also delete the REG_NOTES when moving the insn, but it
568 seems simpler to not move it.) An exception is that we can move
569 the insn if the only note is a REG_EQUAL or REG_EQUIV whose
570 value is the same as "a".
571
572 INSN is the goto.
573
574 We set:
575
576 TEMP to the jump insn preceding "x = a;"
577 TEMP1 to X
578 TEMP2 to the insn that sets "x = b;"
579 TEMP3 to the insn that sets "x = a;"
580 TEMP4 to the set of "x = a"; */
581
582 if (this_is_simplejump
583 && (temp2 = next_active_insn (insn)) != 0
584 && GET_CODE (temp2) == INSN
585 && (temp4 = single_set (temp2)) != 0
586 && GET_CODE (temp1 = SET_DEST (temp4)) == REG
587 && (! SMALL_REGISTER_CLASSES
588 || REGNO (temp1) >= FIRST_PSEUDO_REGISTER)
589 && (temp3 = prev_active_insn (insn)) != 0
590 && GET_CODE (temp3) == INSN
591 && (temp4 = single_set (temp3)) != 0
592 && rtx_equal_p (SET_DEST (temp4), temp1)
593 && ! side_effects_p (SET_SRC (temp4))
594 && ! may_trap_p (SET_SRC (temp4))
595 && (REG_NOTES (temp3) == 0
596 || ((REG_NOTE_KIND (REG_NOTES (temp3)) == REG_EQUAL
597 || REG_NOTE_KIND (REG_NOTES (temp3)) == REG_EQUIV)
598 && XEXP (REG_NOTES (temp3), 1) == 0
599 && rtx_equal_p (XEXP (REG_NOTES (temp3), 0),
600 SET_SRC (temp4))))
601 && (temp = prev_active_insn (temp3)) != 0
602 && condjump_p (temp) && ! simplejump_p (temp)
603 /* TEMP must skip over the "x = a;" insn */
604 && prev_real_insn (JUMP_LABEL (temp)) == insn
605 && no_labels_between_p (temp, insn))
606 {
607 rtx prev_label = JUMP_LABEL (temp);
608 rtx insert_after = prev_nonnote_insn (temp);
609
610 #ifdef HAVE_cc0
611 /* We cannot insert anything between a set of cc and its use. */
612 if (insert_after && GET_RTX_CLASS (GET_CODE (insert_after)) == 'i'
613 && sets_cc0_p (PATTERN (insert_after)))
614 insert_after = prev_nonnote_insn (insert_after);
615 #endif
616 ++LABEL_NUSES (prev_label);
617
618 if (insert_after
619 && no_labels_between_p (insert_after, temp)
620 && ! reg_referenced_between_p (temp1, insert_after, temp3)
621 && ! reg_referenced_between_p (temp1, temp3,
622 NEXT_INSN (temp2))
623 && ! reg_set_between_p (temp1, insert_after, temp)
624 && ! modified_between_p (SET_SRC (temp4), insert_after, temp)
625 /* Verify that registers used by the jump are not clobbered
626 by the instruction being moved. */
627 && ! regs_set_between_p (PATTERN (temp),
628 PREV_INSN (temp3),
629 NEXT_INSN (temp3))
630 && invert_jump (temp, JUMP_LABEL (insn)))
631 {
632 emit_insn_after_with_line_notes (PATTERN (temp3),
633 insert_after, temp3);
634 delete_insn (temp3);
635 delete_insn (insn);
636 /* Set NEXT to an insn that we know won't go away. */
637 next = temp2;
638 changed = 1;
639 }
640 if (prev_label && --LABEL_NUSES (prev_label) == 0)
641 delete_insn (prev_label);
642 if (changed)
643 continue;
644 }
645
646 #ifndef HAVE_cc0
647 /* If we have if (...) x = exp; and branches are expensive,
648 EXP is a single insn, does not have any side effects, cannot
649 trap, and is not too costly, convert this to
650 t = exp; if (...) x = t;
651
652 Don't do this when we have CC0 because it is unlikely to help
653 and we'd need to worry about where to place the new insn and
654 the potential for conflicts. We also can't do this when we have
655 notes on the insn for the same reason as above.
656
657 We set:
658
659 TEMP to the "x = exp;" insn.
660 TEMP1 to the single set in the "x = exp;" insn.
661 TEMP2 to "x". */
662
663 if (! reload_completed
664 && this_is_condjump && ! this_is_simplejump
665 && BRANCH_COST >= 3
666 && (temp = next_nonnote_insn (insn)) != 0
667 && GET_CODE (temp) == INSN
668 && REG_NOTES (temp) == 0
669 && (reallabelprev == temp
670 || ((temp2 = next_active_insn (temp)) != 0
671 && simplejump_p (temp2)
672 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
673 && (temp1 = single_set (temp)) != 0
674 && (temp2 = SET_DEST (temp1), GET_CODE (temp2) == REG)
675 && (! SMALL_REGISTER_CLASSES
676 || REGNO (temp2) >= FIRST_PSEUDO_REGISTER)
677 && GET_CODE (SET_SRC (temp1)) != REG
678 && GET_CODE (SET_SRC (temp1)) != SUBREG
679 && GET_CODE (SET_SRC (temp1)) != CONST_INT
680 && ! side_effects_p (SET_SRC (temp1))
681 && ! may_trap_p (SET_SRC (temp1))
682 && rtx_cost (SET_SRC (temp1), SET) < 10)
683 {
684 rtx new = gen_reg_rtx (GET_MODE (temp2));
685
686 if ((temp3 = find_insert_position (insn, temp))
687 && validate_change (temp, &SET_DEST (temp1), new, 0))
688 {
689 next = emit_insn_after (gen_move_insn (temp2, new), insn);
690 emit_insn_after_with_line_notes (PATTERN (temp),
691 PREV_INSN (temp3), temp);
692 delete_insn (temp);
693 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
694
695 if (after_regscan)
696 {
697 reg_scan_update (temp3, NEXT_INSN (next), old_max_reg);
698 old_max_reg = max_reg_num ();
699 }
700 }
701 }
702
703 /* Similarly, if it takes two insns to compute EXP but they
704 have the same destination. Here TEMP3 will be the second
705 insn and TEMP4 the SET from that insn. */
706
707 if (! reload_completed
708 && this_is_condjump && ! this_is_simplejump
709 && BRANCH_COST >= 4
710 && (temp = next_nonnote_insn (insn)) != 0
711 && GET_CODE (temp) == INSN
712 && REG_NOTES (temp) == 0
713 && (temp3 = next_nonnote_insn (temp)) != 0
714 && GET_CODE (temp3) == INSN
715 && REG_NOTES (temp3) == 0
716 && (reallabelprev == temp3
717 || ((temp2 = next_active_insn (temp3)) != 0
718 && simplejump_p (temp2)
719 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
720 && (temp1 = single_set (temp)) != 0
721 && (temp2 = SET_DEST (temp1), GET_CODE (temp2) == REG)
722 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
723 && (! SMALL_REGISTER_CLASSES
724 || REGNO (temp2) >= FIRST_PSEUDO_REGISTER)
725 && ! side_effects_p (SET_SRC (temp1))
726 && ! may_trap_p (SET_SRC (temp1))
727 && rtx_cost (SET_SRC (temp1), SET) < 10
728 && (temp4 = single_set (temp3)) != 0
729 && rtx_equal_p (SET_DEST (temp4), temp2)
730 && ! side_effects_p (SET_SRC (temp4))
731 && ! may_trap_p (SET_SRC (temp4))
732 && rtx_cost (SET_SRC (temp4), SET) < 10)
733 {
734 rtx new = gen_reg_rtx (GET_MODE (temp2));
735
736 if ((temp5 = find_insert_position (insn, temp))
737 && (temp6 = find_insert_position (insn, temp3))
738 && validate_change (temp, &SET_DEST (temp1), new, 0))
739 {
740 /* Use the earliest of temp5 and temp6. */
741 if (temp5 != insn)
742 temp6 = temp5;
743 next = emit_insn_after (gen_move_insn (temp2, new), insn);
744 emit_insn_after_with_line_notes (PATTERN (temp),
745 PREV_INSN (temp6), temp);
746 emit_insn_after_with_line_notes
747 (replace_rtx (PATTERN (temp3), temp2, new),
748 PREV_INSN (temp6), temp3);
749 delete_insn (temp);
750 delete_insn (temp3);
751 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
752
753 if (after_regscan)
754 {
755 reg_scan_update (temp6, NEXT_INSN (next), old_max_reg);
756 old_max_reg = max_reg_num ();
757 }
758 }
759 }
760
761 /* Finally, handle the case where two insns are used to
762 compute EXP but a temporary register is used. Here we must
763 ensure that the temporary register is not used anywhere else. */
764
765 if (! reload_completed
766 && after_regscan
767 && this_is_condjump && ! this_is_simplejump
768 && BRANCH_COST >= 4
769 && (temp = next_nonnote_insn (insn)) != 0
770 && GET_CODE (temp) == INSN
771 && REG_NOTES (temp) == 0
772 && (temp3 = next_nonnote_insn (temp)) != 0
773 && GET_CODE (temp3) == INSN
774 && REG_NOTES (temp3) == 0
775 && (reallabelprev == temp3
776 || ((temp2 = next_active_insn (temp3)) != 0
777 && simplejump_p (temp2)
778 && JUMP_LABEL (temp2) == JUMP_LABEL (insn)))
779 && (temp1 = single_set (temp)) != 0
780 && (temp5 = SET_DEST (temp1),
781 (GET_CODE (temp5) == REG
782 || (GET_CODE (temp5) == SUBREG
783 && (temp5 = SUBREG_REG (temp5),
784 GET_CODE (temp5) == REG))))
785 && REGNO (temp5) >= FIRST_PSEUDO_REGISTER
786 && REGNO_FIRST_UID (REGNO (temp5)) == INSN_UID (temp)
787 && REGNO_LAST_UID (REGNO (temp5)) == INSN_UID (temp3)
788 && ! side_effects_p (SET_SRC (temp1))
789 && ! may_trap_p (SET_SRC (temp1))
790 && rtx_cost (SET_SRC (temp1), SET) < 10
791 && (temp4 = single_set (temp3)) != 0
792 && (temp2 = SET_DEST (temp4), GET_CODE (temp2) == REG)
793 && GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT
794 && (! SMALL_REGISTER_CLASSES
795 || REGNO (temp2) >= FIRST_PSEUDO_REGISTER)
796 && rtx_equal_p (SET_DEST (temp4), temp2)
797 && ! side_effects_p (SET_SRC (temp4))
798 && ! may_trap_p (SET_SRC (temp4))
799 && rtx_cost (SET_SRC (temp4), SET) < 10)
800 {
801 rtx new = gen_reg_rtx (GET_MODE (temp2));
802
803 if ((temp5 = find_insert_position (insn, temp))
804 && (temp6 = find_insert_position (insn, temp3))
805 && validate_change (temp3, &SET_DEST (temp4), new, 0))
806 {
807 /* Use the earliest of temp5 and temp6. */
808 if (temp5 != insn)
809 temp6 = temp5;
810 next = emit_insn_after (gen_move_insn (temp2, new), insn);
811 emit_insn_after_with_line_notes (PATTERN (temp),
812 PREV_INSN (temp6), temp);
813 emit_insn_after_with_line_notes (PATTERN (temp3),
814 PREV_INSN (temp6), temp3);
815 delete_insn (temp);
816 delete_insn (temp3);
817 reallabelprev = prev_active_insn (JUMP_LABEL (insn));
818
819 if (after_regscan)
820 {
821 reg_scan_update (temp6, NEXT_INSN (next), old_max_reg);
822 old_max_reg = max_reg_num ();
823 }
824 }
825 }
826 #endif /* HAVE_cc0 */
827
828 /* Try to use a conditional move (if the target has them), or a
829 store-flag insn. The general case is:
830
831 1) x = a; if (...) x = b; and
832 2) if (...) x = b;
833
834 If the jump would be faster, the machine should not have defined
835 the movcc or scc insns!. These cases are often made by the
836 previous optimization.
837
838 The second case is treated as x = x; if (...) x = b;.
839
840 INSN here is the jump around the store. We set:
841
842 TEMP to the "x = b;" insn.
843 TEMP1 to X.
844 TEMP2 to B.
845 TEMP3 to A (X in the second case).
846 TEMP4 to the condition being tested.
847 TEMP5 to the earliest insn used to find the condition. */
848
849 if (/* We can't do this after reload has completed. */
850 ! reload_completed
851 && this_is_condjump && ! this_is_simplejump
852 /* Set TEMP to the "x = b;" insn. */
853 && (temp = next_nonnote_insn (insn)) != 0
854 && GET_CODE (temp) == INSN
855 && GET_CODE (PATTERN (temp)) == SET
856 && GET_CODE (temp1 = SET_DEST (PATTERN (temp))) == REG
857 && (! SMALL_REGISTER_CLASSES
858 || REGNO (temp1) >= FIRST_PSEUDO_REGISTER)
859 && ! side_effects_p (temp2 = SET_SRC (PATTERN (temp)))
860 && ! may_trap_p (temp2)
861 /* Allow either form, but prefer the former if both apply.
862 There is no point in using the old value of TEMP1 if
863 it is a register, since cse will alias them. It can
864 lose if the old value were a hard register since CSE
865 won't replace hard registers. Avoid using TEMP3 if
866 small register classes and it is a hard register. */
867 && (((temp3 = reg_set_last (temp1, insn)) != 0
868 && ! (SMALL_REGISTER_CLASSES && GET_CODE (temp3) == REG
869 && REGNO (temp3) < FIRST_PSEUDO_REGISTER))
870 /* Make the latter case look like x = x; if (...) x = b; */
871 || (temp3 = temp1, 1))
872 /* INSN must either branch to the insn after TEMP or the insn
873 after TEMP must branch to the same place as INSN. */
874 && (reallabelprev == temp
875 || ((temp4 = next_active_insn (temp)) != 0
876 && simplejump_p (temp4)
877 && JUMP_LABEL (temp4) == JUMP_LABEL (insn)))
878 && (temp4 = get_condition (insn, &temp5)) != 0
879 /* We must be comparing objects whose modes imply the size.
880 We could handle BLKmode if (1) emit_store_flag could
881 and (2) we could find the size reliably. */
882 && GET_MODE (XEXP (temp4, 0)) != BLKmode
883 /* Even if branches are cheap, the store_flag optimization
884 can win when the operation to be performed can be
885 expressed directly. */
886 #ifdef HAVE_cc0
887 /* If the previous insn sets CC0 and something else, we can't
888 do this since we are going to delete that insn. */
889
890 && ! ((temp6 = prev_nonnote_insn (insn)) != 0
891 && GET_CODE (temp6) == INSN
892 && (sets_cc0_p (PATTERN (temp6)) == -1
893 || (sets_cc0_p (PATTERN (temp6)) == 1
894 && FIND_REG_INC_NOTE (temp6, NULL_RTX))))
895 #endif
896 )
897 {
898 #ifdef HAVE_conditional_move
899 /* First try a conditional move. */
900 {
901 enum rtx_code code = GET_CODE (temp4);
902 rtx var = temp1;
903 rtx cond0, cond1, aval, bval;
904 rtx target;
905
906 /* Copy the compared variables into cond0 and cond1, so that
907 any side effects performed in or after the old comparison,
908 will not affect our compare which will come later. */
909 /* ??? Is it possible to just use the comparison in the jump
910 insn? After all, we're going to delete it. We'd have
911 to modify emit_conditional_move to take a comparison rtx
912 instead or write a new function. */
913 cond0 = gen_reg_rtx (GET_MODE (XEXP (temp4, 0)));
914 /* We want the target to be able to simplify comparisons with
915 zero (and maybe other constants as well), so don't create
916 pseudos for them. There's no need to either. */
917 if (GET_CODE (XEXP (temp4, 1)) == CONST_INT
918 || GET_CODE (XEXP (temp4, 1)) == CONST_DOUBLE)
919 cond1 = XEXP (temp4, 1);
920 else
921 cond1 = gen_reg_rtx (GET_MODE (XEXP (temp4, 1)));
922
923 aval = temp3;
924 bval = temp2;
925
926 start_sequence ();
927 target = emit_conditional_move (var, code,
928 cond0, cond1, VOIDmode,
929 aval, bval, GET_MODE (var),
930 (code == LTU || code == GEU
931 || code == LEU || code == GTU));
932
933 if (target)
934 {
935 rtx seq1,seq2,last;
936
937 /* Save the conditional move sequence but don't emit it
938 yet. On some machines, like the alpha, it is possible
939 that temp5 == insn, so next generate the sequence that
940 saves the compared values and then emit both
941 sequences ensuring seq1 occurs before seq2. */
942 seq2 = get_insns ();
943 end_sequence ();
944
945 /* Now that we can't fail, generate the copy insns that
946 preserve the compared values. */
947 start_sequence ();
948 emit_move_insn (cond0, XEXP (temp4, 0));
949 if (cond1 != XEXP (temp4, 1))
950 emit_move_insn (cond1, XEXP (temp4, 1));
951 seq1 = get_insns ();
952 end_sequence ();
953
954 emit_insns_before (seq1, temp5);
955 /* Insert conditional move after insn, to be sure that
956 the jump and a possible compare won't be separated */
957 last = emit_insns_after (seq2, insn);
958
959 /* ??? We can also delete the insn that sets X to A.
960 Flow will do it too though. */
961 delete_insn (temp);
962 next = NEXT_INSN (insn);
963 delete_jump (insn);
964
965 if (after_regscan)
966 {
967 reg_scan_update (seq1, NEXT_INSN (last), old_max_reg);
968 old_max_reg = max_reg_num ();
969 }
970
971 changed = 1;
972 continue;
973 }
974 else
975 end_sequence ();
976 }
977 #endif
978
979 /* That didn't work, try a store-flag insn.
980
981 We further divide the cases into:
982
983 1) x = a; if (...) x = b; and either A or B is zero,
984 2) if (...) x = 0; and jumps are expensive,
985 3) x = a; if (...) x = b; and A and B are constants where all
986 the set bits in A are also set in B and jumps are expensive,
987 4) x = a; if (...) x = b; and A and B non-zero, and jumps are
988 more expensive, and
989 5) if (...) x = b; if jumps are even more expensive. */
990
991 if (GET_MODE_CLASS (GET_MODE (temp1)) == MODE_INT
992 && ((GET_CODE (temp3) == CONST_INT)
993 /* Make the latter case look like
994 x = x; if (...) x = 0; */
995 || (temp3 = temp1,
996 ((BRANCH_COST >= 2
997 && temp2 == const0_rtx)
998 || BRANCH_COST >= 3)))
999 /* If B is zero, OK; if A is zero, can only do (1) if we
1000 can reverse the condition. See if (3) applies possibly
1001 by reversing the condition. Prefer reversing to (4) when
1002 branches are very expensive. */
1003 && (((BRANCH_COST >= 2
1004 || STORE_FLAG_VALUE == -1
1005 || (STORE_FLAG_VALUE == 1
1006 /* Check that the mask is a power of two,
1007 so that it can probably be generated
1008 with a shift. */
1009 && GET_CODE (temp3) == CONST_INT
1010 && exact_log2 (INTVAL (temp3)) >= 0))
1011 && (reversep = 0, temp2 == const0_rtx))
1012 || ((BRANCH_COST >= 2
1013 || STORE_FLAG_VALUE == -1
1014 || (STORE_FLAG_VALUE == 1
1015 && GET_CODE (temp2) == CONST_INT
1016 && exact_log2 (INTVAL (temp2)) >= 0))
1017 && temp3 == const0_rtx
1018 && (reversep = can_reverse_comparison_p (temp4, insn)))
1019 || (BRANCH_COST >= 2
1020 && GET_CODE (temp2) == CONST_INT
1021 && GET_CODE (temp3) == CONST_INT
1022 && ((INTVAL (temp2) & INTVAL (temp3)) == INTVAL (temp2)
1023 || ((INTVAL (temp2) & INTVAL (temp3)) == INTVAL (temp3)
1024 && (reversep = can_reverse_comparison_p (temp4,
1025 insn)))))
1026 || BRANCH_COST >= 3)
1027 )
1028 {
1029 enum rtx_code code = GET_CODE (temp4);
1030 rtx uval, cval, var = temp1;
1031 int normalizep;
1032 rtx target;
1033
1034 /* If necessary, reverse the condition. */
1035 if (reversep)
1036 code = reverse_condition (code), uval = temp2, cval = temp3;
1037 else
1038 uval = temp3, cval = temp2;
1039
1040 /* If CVAL is non-zero, normalize to -1. Otherwise, if UVAL
1041 is the constant 1, it is best to just compute the result
1042 directly. If UVAL is constant and STORE_FLAG_VALUE
1043 includes all of its bits, it is best to compute the flag
1044 value unnormalized and `and' it with UVAL. Otherwise,
1045 normalize to -1 and `and' with UVAL. */
1046 normalizep = (cval != const0_rtx ? -1
1047 : (uval == const1_rtx ? 1
1048 : (GET_CODE (uval) == CONST_INT
1049 && (INTVAL (uval) & ~STORE_FLAG_VALUE) == 0)
1050 ? 0 : -1));
1051
1052 /* We will be putting the store-flag insn immediately in
1053 front of the comparison that was originally being done,
1054 so we know all the variables in TEMP4 will be valid.
1055 However, this might be in front of the assignment of
1056 A to VAR. If it is, it would clobber the store-flag
1057 we will be emitting.
1058
1059 Therefore, emit into a temporary which will be copied to
1060 VAR immediately after TEMP. */
1061
1062 start_sequence ();
1063 target = emit_store_flag (gen_reg_rtx (GET_MODE (var)), code,
1064 XEXP (temp4, 0), XEXP (temp4, 1),
1065 VOIDmode,
1066 (code == LTU || code == LEU
1067 || code == GEU || code == GTU),
1068 normalizep);
1069 if (target)
1070 {
1071 rtx seq;
1072 rtx before = insn;
1073
1074 seq = get_insns ();
1075 end_sequence ();
1076
1077 /* Put the store-flag insns in front of the first insn
1078 used to compute the condition to ensure that we
1079 use the same values of them as the current
1080 comparison. However, the remainder of the insns we
1081 generate will be placed directly in front of the
1082 jump insn, in case any of the pseudos we use
1083 are modified earlier. */
1084
1085 emit_insns_before (seq, temp5);
1086
1087 start_sequence ();
1088
1089 /* Both CVAL and UVAL are non-zero. */
1090 if (cval != const0_rtx && uval != const0_rtx)
1091 {
1092 rtx tem1, tem2;
1093
1094 tem1 = expand_and (uval, target, NULL_RTX);
1095 if (GET_CODE (cval) == CONST_INT
1096 && GET_CODE (uval) == CONST_INT
1097 && (INTVAL (cval) & INTVAL (uval)) == INTVAL (cval))
1098 tem2 = cval;
1099 else
1100 {
1101 tem2 = expand_unop (GET_MODE (var), one_cmpl_optab,
1102 target, NULL_RTX, 0);
1103 tem2 = expand_and (cval, tem2,
1104 (GET_CODE (tem2) == REG
1105 ? tem2 : 0));
1106 }
1107
1108 /* If we usually make new pseudos, do so here. This
1109 turns out to help machines that have conditional
1110 move insns. */
1111 /* ??? Conditional moves have already been handled.
1112 This may be obsolete. */
1113
1114 if (flag_expensive_optimizations)
1115 target = 0;
1116
1117 target = expand_binop (GET_MODE (var), ior_optab,
1118 tem1, tem2, target,
1119 1, OPTAB_WIDEN);
1120 }
1121 else if (normalizep != 1)
1122 {
1123 /* We know that either CVAL or UVAL is zero. If
1124 UVAL is zero, negate TARGET and `and' with CVAL.
1125 Otherwise, `and' with UVAL. */
1126 if (uval == const0_rtx)
1127 {
1128 target = expand_unop (GET_MODE (var), one_cmpl_optab,
1129 target, NULL_RTX, 0);
1130 uval = cval;
1131 }
1132
1133 target = expand_and (uval, target,
1134 (GET_CODE (target) == REG
1135 && ! preserve_subexpressions_p ()
1136 ? target : NULL_RTX));
1137 }
1138
1139 emit_move_insn (var, target);
1140 seq = get_insns ();
1141 end_sequence ();
1142 #ifdef HAVE_cc0
1143 /* If INSN uses CC0, we must not separate it from the
1144 insn that sets cc0. */
1145 if (reg_mentioned_p (cc0_rtx, PATTERN (before)))
1146 before = prev_nonnote_insn (before);
1147 #endif
1148 emit_insns_before (seq, before);
1149
1150 delete_insn (temp);
1151 next = NEXT_INSN (insn);
1152 delete_jump (insn);
1153
1154 if (after_regscan)
1155 {
1156 reg_scan_update (seq, NEXT_INSN (next), old_max_reg);
1157 old_max_reg = max_reg_num ();
1158 }
1159
1160 changed = 1;
1161 continue;
1162 }
1163 else
1164 end_sequence ();
1165 }
1166 }
1167
1168 /* If branches are expensive, convert
1169 if (foo) bar++; to bar += (foo != 0);
1170 and similarly for "bar--;"
1171
1172 INSN is the conditional branch around the arithmetic. We set:
1173
1174 TEMP is the arithmetic insn.
1175 TEMP1 is the SET doing the arithmetic.
1176 TEMP2 is the operand being incremented or decremented.
1177 TEMP3 to the condition being tested.
1178 TEMP4 to the earliest insn used to find the condition. */
1179
1180 if ((BRANCH_COST >= 2
1181 #ifdef HAVE_incscc
1182 || HAVE_incscc
1183 #endif
1184 #ifdef HAVE_decscc
1185 || HAVE_decscc
1186 #endif
1187 )
1188 && ! reload_completed
1189 && this_is_condjump && ! this_is_simplejump
1190 && (temp = next_nonnote_insn (insn)) != 0
1191 && (temp1 = single_set (temp)) != 0
1192 && (temp2 = SET_DEST (temp1),
1193 GET_MODE_CLASS (GET_MODE (temp2)) == MODE_INT)
1194 && GET_CODE (SET_SRC (temp1)) == PLUS
1195 && (XEXP (SET_SRC (temp1), 1) == const1_rtx
1196 || XEXP (SET_SRC (temp1), 1) == constm1_rtx)
1197 && rtx_equal_p (temp2, XEXP (SET_SRC (temp1), 0))
1198 && ! side_effects_p (temp2)
1199 && ! may_trap_p (temp2)
1200 /* INSN must either branch to the insn after TEMP or the insn
1201 after TEMP must branch to the same place as INSN. */
1202 && (reallabelprev == temp
1203 || ((temp3 = next_active_insn (temp)) != 0
1204 && simplejump_p (temp3)
1205 && JUMP_LABEL (temp3) == JUMP_LABEL (insn)))
1206 && (temp3 = get_condition (insn, &temp4)) != 0
1207 /* We must be comparing objects whose modes imply the size.
1208 We could handle BLKmode if (1) emit_store_flag could
1209 and (2) we could find the size reliably. */
1210 && GET_MODE (XEXP (temp3, 0)) != BLKmode
1211 && can_reverse_comparison_p (temp3, insn))
1212 {
1213 rtx temp6, target = 0, seq, init_insn = 0, init = temp2;
1214 enum rtx_code code = reverse_condition (GET_CODE (temp3));
1215
1216 start_sequence ();
1217
1218 /* It must be the case that TEMP2 is not modified in the range
1219 [TEMP4, INSN). The one exception we make is if the insn
1220 before INSN sets TEMP2 to something which is also unchanged
1221 in that range. In that case, we can move the initialization
1222 into our sequence. */
1223
1224 if ((temp5 = prev_active_insn (insn)) != 0
1225 && no_labels_between_p (temp5, insn)
1226 && GET_CODE (temp5) == INSN
1227 && (temp6 = single_set (temp5)) != 0
1228 && rtx_equal_p (temp2, SET_DEST (temp6))
1229 && (CONSTANT_P (SET_SRC (temp6))
1230 || GET_CODE (SET_SRC (temp6)) == REG
1231 || GET_CODE (SET_SRC (temp6)) == SUBREG))
1232 {
1233 emit_insn (PATTERN (temp5));
1234 init_insn = temp5;
1235 init = SET_SRC (temp6);
1236 }
1237
1238 if (CONSTANT_P (init)
1239 || ! reg_set_between_p (init, PREV_INSN (temp4), insn))
1240 target = emit_store_flag (gen_reg_rtx (GET_MODE (temp2)), code,
1241 XEXP (temp3, 0), XEXP (temp3, 1),
1242 VOIDmode,
1243 (code == LTU || code == LEU
1244 || code == GTU || code == GEU), 1);
1245
1246 /* If we can do the store-flag, do the addition or
1247 subtraction. */
1248
1249 if (target)
1250 target = expand_binop (GET_MODE (temp2),
1251 (XEXP (SET_SRC (temp1), 1) == const1_rtx
1252 ? add_optab : sub_optab),
1253 temp2, target, temp2, 0, OPTAB_WIDEN);
1254
1255 if (target != 0)
1256 {
1257 /* Put the result back in temp2 in case it isn't already.
1258 Then replace the jump, possible a CC0-setting insn in
1259 front of the jump, and TEMP, with the sequence we have
1260 made. */
1261
1262 if (target != temp2)
1263 emit_move_insn (temp2, target);
1264
1265 seq = get_insns ();
1266 end_sequence ();
1267
1268 emit_insns_before (seq, temp4);
1269 delete_insn (temp);
1270
1271 if (init_insn)
1272 delete_insn (init_insn);
1273
1274 next = NEXT_INSN (insn);
1275 #ifdef HAVE_cc0
1276 delete_insn (prev_nonnote_insn (insn));
1277 #endif
1278 delete_insn (insn);
1279
1280 if (after_regscan)
1281 {
1282 reg_scan_update (seq, NEXT_INSN (next), old_max_reg);
1283 old_max_reg = max_reg_num ();
1284 }
1285
1286 changed = 1;
1287 continue;
1288 }
1289 else
1290 end_sequence ();
1291 }
1292
1293 /* Simplify if (...) x = 1; else {...} if (x) ...
1294 We recognize this case scanning backwards as well.
1295
1296 TEMP is the assignment to x;
1297 TEMP1 is the label at the head of the second if. */
1298 /* ?? This should call get_condition to find the values being
1299 compared, instead of looking for a COMPARE insn when HAVE_cc0
1300 is not defined. This would allow it to work on the m88k. */
1301 /* ?? This optimization is only safe before cse is run if HAVE_cc0
1302 is not defined and the condition is tested by a separate compare
1303 insn. This is because the code below assumes that the result
1304 of the compare dies in the following branch.
1305
1306 Not only that, but there might be other insns between the
1307 compare and branch whose results are live. Those insns need
1308 to be executed.
1309
1310 A way to fix this is to move the insns at JUMP_LABEL (insn)
1311 to before INSN. If we are running before flow, they will
1312 be deleted if they aren't needed. But this doesn't work
1313 well after flow.
1314
1315 This is really a special-case of jump threading, anyway. The
1316 right thing to do is to replace this and jump threading with
1317 much simpler code in cse.
1318
1319 This code has been turned off in the non-cc0 case in the
1320 meantime. */
1321
1322 #ifdef HAVE_cc0
1323 else if (this_is_simplejump
1324 /* Safe to skip USE and CLOBBER insns here
1325 since they will not be deleted. */
1326 && (temp = prev_active_insn (insn))
1327 && no_labels_between_p (temp, insn)
1328 && GET_CODE (temp) == INSN
1329 && GET_CODE (PATTERN (temp)) == SET
1330 && GET_CODE (SET_DEST (PATTERN (temp))) == REG
1331 && CONSTANT_P (SET_SRC (PATTERN (temp)))
1332 && (temp1 = next_active_insn (JUMP_LABEL (insn)))
1333 /* If we find that the next value tested is `x'
1334 (TEMP1 is the insn where this happens), win. */
1335 && GET_CODE (temp1) == INSN
1336 && GET_CODE (PATTERN (temp1)) == SET
1337 #ifdef HAVE_cc0
1338 /* Does temp1 `tst' the value of x? */
1339 && SET_SRC (PATTERN (temp1)) == SET_DEST (PATTERN (temp))
1340 && SET_DEST (PATTERN (temp1)) == cc0_rtx
1341 && (temp1 = next_nonnote_insn (temp1))
1342 #else
1343 /* Does temp1 compare the value of x against zero? */
1344 && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
1345 && XEXP (SET_SRC (PATTERN (temp1)), 1) == const0_rtx
1346 && (XEXP (SET_SRC (PATTERN (temp1)), 0)
1347 == SET_DEST (PATTERN (temp)))
1348 && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
1349 && (temp1 = find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
1350 #endif
1351 && condjump_p (temp1))
1352 {
1353 /* Get the if_then_else from the condjump. */
1354 rtx choice = SET_SRC (PATTERN (temp1));
1355 if (GET_CODE (choice) == IF_THEN_ELSE)
1356 {
1357 enum rtx_code code = GET_CODE (XEXP (choice, 0));
1358 rtx val = SET_SRC (PATTERN (temp));
1359 rtx cond
1360 = simplify_relational_operation (code, GET_MODE (SET_DEST (PATTERN (temp))),
1361 val, const0_rtx);
1362 rtx ultimate;
1363
1364 if (cond == const_true_rtx)
1365 ultimate = XEXP (choice, 1);
1366 else if (cond == const0_rtx)
1367 ultimate = XEXP (choice, 2);
1368 else
1369 ultimate = 0;
1370
1371 if (ultimate == pc_rtx)
1372 ultimate = get_label_after (temp1);
1373 else if (ultimate && GET_CODE (ultimate) != RETURN)
1374 ultimate = XEXP (ultimate, 0);
1375
1376 if (ultimate && JUMP_LABEL(insn) != ultimate)
1377 changed |= redirect_jump (insn, ultimate);
1378 }
1379 }
1380 #endif
1381
1382 #if 0
1383 /* @@ This needs a bit of work before it will be right.
1384
1385 Any type of comparison can be accepted for the first and
1386 second compare. When rewriting the first jump, we must
1387 compute the what conditions can reach label3, and use the
1388 appropriate code. We can not simply reverse/swap the code
1389 of the first jump. In some cases, the second jump must be
1390 rewritten also.
1391
1392 For example,
1393 < == converts to > ==
1394 < != converts to == >
1395 etc.
1396
1397 If the code is written to only accept an '==' test for the second
1398 compare, then all that needs to be done is to swap the condition
1399 of the first branch.
1400
1401 It is questionable whether we want this optimization anyways,
1402 since if the user wrote code like this because he/she knew that
1403 the jump to label1 is taken most of the time, then rewriting
1404 this gives slower code. */
1405 /* @@ This should call get_condition to find the values being
1406 compared, instead of looking for a COMPARE insn when HAVE_cc0
1407 is not defined. This would allow it to work on the m88k. */
1408 /* @@ This optimization is only safe before cse is run if HAVE_cc0
1409 is not defined and the condition is tested by a separate compare
1410 insn. This is because the code below assumes that the result
1411 of the compare dies in the following branch. */
1412
1413 /* Simplify test a ~= b
1414 condjump label1;
1415 test a == b
1416 condjump label2;
1417 jump label3;
1418 label1:
1419
1420 rewriting as
1421 test a ~~= b
1422 condjump label3
1423 test a == b
1424 condjump label2
1425 label1:
1426
1427 where ~= is an inequality, e.g. >, and ~~= is the swapped
1428 inequality, e.g. <.
1429
1430 We recognize this case scanning backwards.
1431
1432 TEMP is the conditional jump to `label2';
1433 TEMP1 is the test for `a == b';
1434 TEMP2 is the conditional jump to `label1';
1435 TEMP3 is the test for `a ~= b'. */
1436 else if (this_is_simplejump
1437 && (temp = prev_active_insn (insn))
1438 && no_labels_between_p (temp, insn)
1439 && condjump_p (temp)
1440 && (temp1 = prev_active_insn (temp))
1441 && no_labels_between_p (temp1, temp)
1442 && GET_CODE (temp1) == INSN
1443 && GET_CODE (PATTERN (temp1)) == SET
1444 #ifdef HAVE_cc0
1445 && sets_cc0_p (PATTERN (temp1)) == 1
1446 #else
1447 && GET_CODE (SET_SRC (PATTERN (temp1))) == COMPARE
1448 && GET_CODE (SET_DEST (PATTERN (temp1))) == REG
1449 && (temp == find_next_ref (SET_DEST (PATTERN (temp1)), temp1))
1450 #endif
1451 && (temp2 = prev_active_insn (temp1))
1452 && no_labels_between_p (temp2, temp1)
1453 && condjump_p (temp2)
1454 && JUMP_LABEL (temp2) == next_nonnote_insn (NEXT_INSN (insn))
1455 && (temp3 = prev_active_insn (temp2))
1456 && no_labels_between_p (temp3, temp2)
1457 && GET_CODE (PATTERN (temp3)) == SET
1458 && rtx_equal_p (SET_DEST (PATTERN (temp3)),
1459 SET_DEST (PATTERN (temp1)))
1460 && rtx_equal_p (SET_SRC (PATTERN (temp1)),
1461 SET_SRC (PATTERN (temp3)))
1462 && ! inequality_comparisons_p (PATTERN (temp))
1463 && inequality_comparisons_p (PATTERN (temp2)))
1464 {
1465 rtx fallthrough_label = JUMP_LABEL (temp2);
1466
1467 ++LABEL_NUSES (fallthrough_label);
1468 if (swap_jump (temp2, JUMP_LABEL (insn)))
1469 {
1470 delete_insn (insn);
1471 changed = 1;
1472 }
1473
1474 if (--LABEL_NUSES (fallthrough_label) == 0)
1475 delete_insn (fallthrough_label);
1476 }
1477 #endif
1478 /* Simplify if (...) {... x = 1;} if (x) ...
1479
1480 We recognize this case backwards.
1481
1482 TEMP is the test of `x';
1483 TEMP1 is the assignment to `x' at the end of the
1484 previous statement. */
1485 /* @@ This should call get_condition to find the values being
1486 compared, instead of looking for a COMPARE insn when HAVE_cc0
1487 is not defined. This would allow it to work on the m88k. */
1488 /* @@ This optimization is only safe before cse is run if HAVE_cc0
1489 is not defined and the condition is tested by a separate compare
1490 insn. This is because the code below assumes that the result
1491 of the compare dies in the following branch. */
1492
1493 /* ??? This has to be turned off. The problem is that the
1494 unconditional jump might indirectly end up branching to the
1495 label between TEMP1 and TEMP. We can't detect this, in general,
1496 since it may become a jump to there after further optimizations.
1497 If that jump is done, it will be deleted, so we will retry
1498 this optimization in the next pass, thus an infinite loop.
1499
1500 The present code prevents this by putting the jump after the
1501 label, but this is not logically correct. */
1502 #if 0
1503 else if (this_is_condjump
1504 /* Safe to skip USE and CLOBBER insns here
1505 since they will not be deleted. */
1506 && (temp = prev_active_insn (insn))
1507 && no_labels_between_p (temp, insn)
1508 && GET_CODE (temp) == INSN
1509 && GET_CODE (PATTERN (temp)) == SET
1510 #ifdef HAVE_cc0
1511 && sets_cc0_p (PATTERN (temp)) == 1
1512 && GET_CODE (SET_SRC (PATTERN (temp))) == REG
1513 #else
1514 /* Temp must be a compare insn, we can not accept a register
1515 to register move here, since it may not be simply a
1516 tst insn. */
1517 && GET_CODE (SET_SRC (PATTERN (temp))) == COMPARE
1518 && XEXP (SET_SRC (PATTERN (temp)), 1) == const0_rtx
1519 && GET_CODE (XEXP (SET_SRC (PATTERN (temp)), 0)) == REG
1520 && GET_CODE (SET_DEST (PATTERN (temp))) == REG
1521 && insn == find_next_ref (SET_DEST (PATTERN (temp)), temp)
1522 #endif
1523 /* May skip USE or CLOBBER insns here
1524 for checking for opportunity, since we
1525 take care of them later. */
1526 && (temp1 = prev_active_insn (temp))
1527 && GET_CODE (temp1) == INSN
1528 && GET_CODE (PATTERN (temp1)) == SET
1529 #ifdef HAVE_cc0
1530 && SET_SRC (PATTERN (temp)) == SET_DEST (PATTERN (temp1))
1531 #else
1532 && (XEXP (SET_SRC (PATTERN (temp)), 0)
1533 == SET_DEST (PATTERN (temp1)))
1534 #endif
1535 && CONSTANT_P (SET_SRC (PATTERN (temp1)))
1536 /* If this isn't true, cse will do the job. */
1537 && ! no_labels_between_p (temp1, temp))
1538 {
1539 /* Get the if_then_else from the condjump. */
1540 rtx choice = SET_SRC (PATTERN (insn));
1541 if (GET_CODE (choice) == IF_THEN_ELSE
1542 && (GET_CODE (XEXP (choice, 0)) == EQ
1543 || GET_CODE (XEXP (choice, 0)) == NE))
1544 {
1545 int want_nonzero = (GET_CODE (XEXP (choice, 0)) == NE);
1546 rtx last_insn;
1547 rtx ultimate;
1548 rtx p;
1549
1550 /* Get the place that condjump will jump to
1551 if it is reached from here. */
1552 if ((SET_SRC (PATTERN (temp1)) != const0_rtx)
1553 == want_nonzero)
1554 ultimate = XEXP (choice, 1);
1555 else
1556 ultimate = XEXP (choice, 2);
1557 /* Get it as a CODE_LABEL. */
1558 if (ultimate == pc_rtx)
1559 ultimate = get_label_after (insn);
1560 else
1561 /* Get the label out of the LABEL_REF. */
1562 ultimate = XEXP (ultimate, 0);
1563
1564 /* Insert the jump immediately before TEMP, specifically
1565 after the label that is between TEMP1 and TEMP. */
1566 last_insn = PREV_INSN (temp);
1567
1568 /* If we would be branching to the next insn, the jump
1569 would immediately be deleted and the re-inserted in
1570 a subsequent pass over the code. So don't do anything
1571 in that case. */
1572 if (next_active_insn (last_insn)
1573 != next_active_insn (ultimate))
1574 {
1575 emit_barrier_after (last_insn);
1576 p = emit_jump_insn_after (gen_jump (ultimate),
1577 last_insn);
1578 JUMP_LABEL (p) = ultimate;
1579 ++LABEL_NUSES (ultimate);
1580 if (INSN_UID (ultimate) < max_jump_chain
1581 && INSN_CODE (p) < max_jump_chain)
1582 {
1583 jump_chain[INSN_UID (p)]
1584 = jump_chain[INSN_UID (ultimate)];
1585 jump_chain[INSN_UID (ultimate)] = p;
1586 }
1587 changed = 1;
1588 continue;
1589 }
1590 }
1591 }
1592 #endif
1593 /* Detect a conditional jump going to the same place
1594 as an immediately following unconditional jump. */
1595 else if (this_is_condjump
1596 && (temp = next_active_insn (insn)) != 0
1597 && simplejump_p (temp)
1598 && (next_active_insn (JUMP_LABEL (insn))
1599 == next_active_insn (JUMP_LABEL (temp))))
1600 {
1601 rtx tem = temp;
1602
1603 /* ??? Optional. Disables some optimizations, but makes
1604 gcov output more accurate with -O. */
1605 if (flag_test_coverage && !reload_completed)
1606 for (tem = insn; tem != temp; tem = NEXT_INSN (tem))
1607 if (GET_CODE (tem) == NOTE && NOTE_LINE_NUMBER (tem) > 0)
1608 break;
1609
1610 if (tem == temp)
1611 {
1612 delete_jump (insn);
1613 changed = 1;
1614 continue;
1615 }
1616 }
1617 #ifdef HAVE_trap
1618 /* Detect a conditional jump jumping over an unconditional trap. */
1619 else if (HAVE_trap
1620 && this_is_condjump && ! this_is_simplejump
1621 && reallabelprev != 0
1622 && GET_CODE (reallabelprev) == INSN
1623 && GET_CODE (PATTERN (reallabelprev)) == TRAP_IF
1624 && TRAP_CONDITION (PATTERN (reallabelprev)) == const_true_rtx
1625 && prev_active_insn (reallabelprev) == insn
1626 && no_labels_between_p (insn, reallabelprev)
1627 && (temp2 = get_condition (insn, &temp4))
1628 && can_reverse_comparison_p (temp2, insn))
1629 {
1630 rtx new = gen_cond_trap (reverse_condition (GET_CODE (temp2)),
1631 XEXP (temp2, 0), XEXP (temp2, 1),
1632 TRAP_CODE (PATTERN (reallabelprev)));
1633
1634 if (new)
1635 {
1636 emit_insn_before (new, temp4);
1637 delete_insn (reallabelprev);
1638 delete_jump (insn);
1639 changed = 1;
1640 continue;
1641 }
1642 }
1643 /* Detect a jump jumping to an unconditional trap. */
1644 else if (HAVE_trap && this_is_condjump
1645 && (temp = next_active_insn (JUMP_LABEL (insn)))
1646 && GET_CODE (temp) == INSN
1647 && GET_CODE (PATTERN (temp)) == TRAP_IF
1648 && (this_is_simplejump
1649 || (temp2 = get_condition (insn, &temp4))))
1650 {
1651 rtx tc = TRAP_CONDITION (PATTERN (temp));
1652
1653 if (tc == const_true_rtx
1654 || (! this_is_simplejump && rtx_equal_p (temp2, tc)))
1655 {
1656 rtx new;
1657 /* Replace an unconditional jump to a trap with a trap. */
1658 if (this_is_simplejump)
1659 {
1660 emit_barrier_after (emit_insn_before (gen_trap (), insn));
1661 delete_jump (insn);
1662 changed = 1;
1663 continue;
1664 }
1665 new = gen_cond_trap (GET_CODE (temp2), XEXP (temp2, 0),
1666 XEXP (temp2, 1),
1667 TRAP_CODE (PATTERN (temp)));
1668 if (new)
1669 {
1670 emit_insn_before (new, temp4);
1671 delete_jump (insn);
1672 changed = 1;
1673 continue;
1674 }
1675 }
1676 /* If the trap condition and jump condition are mutually
1677 exclusive, redirect the jump to the following insn. */
1678 else if (GET_RTX_CLASS (GET_CODE (tc)) == '<'
1679 && ! this_is_simplejump
1680 && swap_condition (GET_CODE (temp2)) == GET_CODE (tc)
1681 && rtx_equal_p (XEXP (tc, 0), XEXP (temp2, 0))
1682 && rtx_equal_p (XEXP (tc, 1), XEXP (temp2, 1))
1683 && redirect_jump (insn, get_label_after (temp)))
1684 {
1685 changed = 1;
1686 continue;
1687 }
1688 }
1689 #endif
1690
1691 /* Detect a conditional jump jumping over an unconditional jump. */
1692
1693 else if ((this_is_condjump || this_is_condjump_in_parallel)
1694 && ! this_is_simplejump
1695 && reallabelprev != 0
1696 && GET_CODE (reallabelprev) == JUMP_INSN
1697 && prev_active_insn (reallabelprev) == insn
1698 && no_labels_between_p (insn, reallabelprev)
1699 && simplejump_p (reallabelprev))
1700 {
1701 /* When we invert the unconditional jump, we will be
1702 decrementing the usage count of its old label.
1703 Make sure that we don't delete it now because that
1704 might cause the following code to be deleted. */
1705 rtx prev_uses = prev_nonnote_insn (reallabelprev);
1706 rtx prev_label = JUMP_LABEL (insn);
1707
1708 if (prev_label)
1709 ++LABEL_NUSES (prev_label);
1710
1711 if (invert_jump (insn, JUMP_LABEL (reallabelprev)))
1712 {
1713 /* It is very likely that if there are USE insns before
1714 this jump, they hold REG_DEAD notes. These REG_DEAD
1715 notes are no longer valid due to this optimization,
1716 and will cause the life-analysis that following passes
1717 (notably delayed-branch scheduling) to think that
1718 these registers are dead when they are not.
1719
1720 To prevent this trouble, we just remove the USE insns
1721 from the insn chain. */
1722
1723 while (prev_uses && GET_CODE (prev_uses) == INSN
1724 && GET_CODE (PATTERN (prev_uses)) == USE)
1725 {
1726 rtx useless = prev_uses;
1727 prev_uses = prev_nonnote_insn (prev_uses);
1728 delete_insn (useless);
1729 }
1730
1731 delete_insn (reallabelprev);
1732 next = insn;
1733 changed = 1;
1734 }
1735
1736 /* We can now safely delete the label if it is unreferenced
1737 since the delete_insn above has deleted the BARRIER. */
1738 if (prev_label && --LABEL_NUSES (prev_label) == 0)
1739 delete_insn (prev_label);
1740 continue;
1741 }
1742 else
1743 {
1744 /* Detect a jump to a jump. */
1745
1746 nlabel = follow_jumps (JUMP_LABEL (insn));
1747 if (nlabel != JUMP_LABEL (insn)
1748 && redirect_jump (insn, nlabel))
1749 {
1750 changed = 1;
1751 next = insn;
1752 }
1753
1754 /* Look for if (foo) bar; else break; */
1755 /* The insns look like this:
1756 insn = condjump label1;
1757 ...range1 (some insns)...
1758 jump label2;
1759 label1:
1760 ...range2 (some insns)...
1761 jump somewhere unconditionally
1762 label2: */
1763 {
1764 rtx label1 = next_label (insn);
1765 rtx range1end = label1 ? prev_active_insn (label1) : 0;
1766 /* Don't do this optimization on the first round, so that
1767 jump-around-a-jump gets simplified before we ask here
1768 whether a jump is unconditional.
1769
1770 Also don't do it when we are called after reload since
1771 it will confuse reorg. */
1772 if (! first
1773 && (reload_completed ? ! flag_delayed_branch : 1)
1774 /* Make sure INSN is something we can invert. */
1775 && condjump_p (insn)
1776 && label1 != 0
1777 && JUMP_LABEL (insn) == label1
1778 && LABEL_NUSES (label1) == 1
1779 && GET_CODE (range1end) == JUMP_INSN
1780 && simplejump_p (range1end))
1781 {
1782 rtx label2 = next_label (label1);
1783 rtx range2end = label2 ? prev_active_insn (label2) : 0;
1784 if (range1end != range2end
1785 && JUMP_LABEL (range1end) == label2
1786 && GET_CODE (range2end) == JUMP_INSN
1787 && GET_CODE (NEXT_INSN (range2end)) == BARRIER
1788 /* Invert the jump condition, so we
1789 still execute the same insns in each case. */
1790 && invert_jump (insn, label1))
1791 {
1792 rtx range1beg = next_active_insn (insn);
1793 rtx range2beg = next_active_insn (label1);
1794 rtx range1after, range2after;
1795 rtx range1before, range2before;
1796 rtx rangenext;
1797
1798 /* Include in each range any notes before it, to be
1799 sure that we get the line number note if any, even
1800 if there are other notes here. */
1801 while (PREV_INSN (range1beg)
1802 && GET_CODE (PREV_INSN (range1beg)) == NOTE)
1803 range1beg = PREV_INSN (range1beg);
1804
1805 while (PREV_INSN (range2beg)
1806 && GET_CODE (PREV_INSN (range2beg)) == NOTE)
1807 range2beg = PREV_INSN (range2beg);
1808
1809 /* Don't move NOTEs for blocks or loops; shift them
1810 outside the ranges, where they'll stay put. */
1811 range1beg = squeeze_notes (range1beg, range1end);
1812 range2beg = squeeze_notes (range2beg, range2end);
1813
1814 /* Get current surrounds of the 2 ranges. */
1815 range1before = PREV_INSN (range1beg);
1816 range2before = PREV_INSN (range2beg);
1817 range1after = NEXT_INSN (range1end);
1818 range2after = NEXT_INSN (range2end);
1819
1820 /* Splice range2 where range1 was. */
1821 NEXT_INSN (range1before) = range2beg;
1822 PREV_INSN (range2beg) = range1before;
1823 NEXT_INSN (range2end) = range1after;
1824 PREV_INSN (range1after) = range2end;
1825 /* Splice range1 where range2 was. */
1826 NEXT_INSN (range2before) = range1beg;
1827 PREV_INSN (range1beg) = range2before;
1828 NEXT_INSN (range1end) = range2after;
1829 PREV_INSN (range2after) = range1end;
1830
1831 /* Check for a loop end note between the end of
1832 range2, and the next code label. If there is one,
1833 then what we have really seen is
1834 if (foo) break; end_of_loop;
1835 and moved the break sequence outside the loop.
1836 We must move the LOOP_END note to where the
1837 loop really ends now, or we will confuse loop
1838 optimization. Stop if we find a LOOP_BEG note
1839 first, since we don't want to move the LOOP_END
1840 note in that case. */
1841 for (;range2after != label2; range2after = rangenext)
1842 {
1843 rangenext = NEXT_INSN (range2after);
1844 if (GET_CODE (range2after) == NOTE)
1845 {
1846 if (NOTE_LINE_NUMBER (range2after)
1847 == NOTE_INSN_LOOP_END)
1848 {
1849 NEXT_INSN (PREV_INSN (range2after))
1850 = rangenext;
1851 PREV_INSN (rangenext)
1852 = PREV_INSN (range2after);
1853 PREV_INSN (range2after)
1854 = PREV_INSN (range1beg);
1855 NEXT_INSN (range2after) = range1beg;
1856 NEXT_INSN (PREV_INSN (range1beg))
1857 = range2after;
1858 PREV_INSN (range1beg) = range2after;
1859 }
1860 else if (NOTE_LINE_NUMBER (range2after)
1861 == NOTE_INSN_LOOP_BEG)
1862 break;
1863 }
1864 }
1865 changed = 1;
1866 continue;
1867 }
1868 }
1869 }
1870
1871 /* Now that the jump has been tensioned,
1872 try cross jumping: check for identical code
1873 before the jump and before its target label. */
1874
1875 /* First, cross jumping of conditional jumps: */
1876
1877 if (cross_jump && condjump_p (insn))
1878 {
1879 rtx newjpos, newlpos;
1880 rtx x = prev_real_insn (JUMP_LABEL (insn));
1881
1882 /* A conditional jump may be crossjumped
1883 only if the place it jumps to follows
1884 an opposing jump that comes back here. */
1885
1886 if (x != 0 && ! jump_back_p (x, insn))
1887 /* We have no opposing jump;
1888 cannot cross jump this insn. */
1889 x = 0;
1890
1891 newjpos = 0;
1892 /* TARGET is nonzero if it is ok to cross jump
1893 to code before TARGET. If so, see if matches. */
1894 if (x != 0)
1895 find_cross_jump (insn, x, 2,
1896 &newjpos, &newlpos);
1897
1898 if (newjpos != 0)
1899 {
1900 do_cross_jump (insn, newjpos, newlpos);
1901 /* Make the old conditional jump
1902 into an unconditional one. */
1903 SET_SRC (PATTERN (insn))
1904 = gen_rtx_LABEL_REF (VOIDmode, JUMP_LABEL (insn));
1905 INSN_CODE (insn) = -1;
1906 emit_barrier_after (insn);
1907 /* Add to jump_chain unless this is a new label
1908 whose UID is too large. */
1909 if (INSN_UID (JUMP_LABEL (insn)) < max_jump_chain)
1910 {
1911 jump_chain[INSN_UID (insn)]
1912 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
1913 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
1914 }
1915 changed = 1;
1916 next = insn;
1917 }
1918 }
1919
1920 /* Cross jumping of unconditional jumps:
1921 a few differences. */
1922
1923 if (cross_jump && simplejump_p (insn))
1924 {
1925 rtx newjpos, newlpos;
1926 rtx target;
1927
1928 newjpos = 0;
1929
1930 /* TARGET is nonzero if it is ok to cross jump
1931 to code before TARGET. If so, see if matches. */
1932 find_cross_jump (insn, JUMP_LABEL (insn), 1,
1933 &newjpos, &newlpos);
1934
1935 /* If cannot cross jump to code before the label,
1936 see if we can cross jump to another jump to
1937 the same label. */
1938 /* Try each other jump to this label. */
1939 if (INSN_UID (JUMP_LABEL (insn)) < max_uid)
1940 for (target = jump_chain[INSN_UID (JUMP_LABEL (insn))];
1941 target != 0 && newjpos == 0;
1942 target = jump_chain[INSN_UID (target)])
1943 if (target != insn
1944 && JUMP_LABEL (target) == JUMP_LABEL (insn)
1945 /* Ignore TARGET if it's deleted. */
1946 && ! INSN_DELETED_P (target))
1947 find_cross_jump (insn, target, 2,
1948 &newjpos, &newlpos);
1949
1950 if (newjpos != 0)
1951 {
1952 do_cross_jump (insn, newjpos, newlpos);
1953 changed = 1;
1954 next = insn;
1955 }
1956 }
1957
1958 /* This code was dead in the previous jump.c! */
1959 if (cross_jump && GET_CODE (PATTERN (insn)) == RETURN)
1960 {
1961 /* Return insns all "jump to the same place"
1962 so we can cross-jump between any two of them. */
1963
1964 rtx newjpos, newlpos, target;
1965
1966 newjpos = 0;
1967
1968 /* If cannot cross jump to code before the label,
1969 see if we can cross jump to another jump to
1970 the same label. */
1971 /* Try each other jump to this label. */
1972 for (target = jump_chain[0];
1973 target != 0 && newjpos == 0;
1974 target = jump_chain[INSN_UID (target)])
1975 if (target != insn
1976 && ! INSN_DELETED_P (target)
1977 && GET_CODE (PATTERN (target)) == RETURN)
1978 find_cross_jump (insn, target, 2,
1979 &newjpos, &newlpos);
1980
1981 if (newjpos != 0)
1982 {
1983 do_cross_jump (insn, newjpos, newlpos);
1984 changed = 1;
1985 next = insn;
1986 }
1987 }
1988 }
1989 }
1990
1991 first = 0;
1992 }
1993
1994 /* Delete extraneous line number notes.
1995 Note that two consecutive notes for different lines are not really
1996 extraneous. There should be some indication where that line belonged,
1997 even if it became empty. */
1998
1999 {
2000 rtx last_note = 0;
2001
2002 for (insn = f; insn; insn = NEXT_INSN (insn))
2003 if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) >= 0)
2004 {
2005 /* Delete this note if it is identical to previous note. */
2006 if (last_note
2007 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
2008 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note))
2009 {
2010 delete_insn (insn);
2011 continue;
2012 }
2013
2014 last_note = insn;
2015 }
2016 }
2017
2018 #ifdef HAVE_return
2019 if (HAVE_return)
2020 {
2021 /* If we fall through to the epilogue, see if we can insert a RETURN insn
2022 in front of it. If the machine allows it at this point (we might be
2023 after reload for a leaf routine), it will improve optimization for it
2024 to be there. We do this both here and at the start of this pass since
2025 the RETURN might have been deleted by some of our optimizations. */
2026 insn = get_last_insn ();
2027 while (insn && GET_CODE (insn) == NOTE)
2028 insn = PREV_INSN (insn);
2029
2030 if (insn && GET_CODE (insn) != BARRIER)
2031 {
2032 emit_jump_insn (gen_return ());
2033 emit_barrier ();
2034 }
2035 }
2036 #endif
2037
2038 can_reach_end = calculate_can_reach_end (last_insn, 0, 1);
2039
2040 /* Show JUMP_CHAIN no longer valid. */
2041 jump_chain = 0;
2042 }
2043 \f
2044 /* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
2045 notes whose labels don't occur in the insn any more. Returns the
2046 largest INSN_UID found. */
2047 static int
2048 init_label_info (f)
2049 rtx f;
2050 {
2051 int largest_uid = 0;
2052 rtx insn;
2053
2054 for (insn = f; insn; insn = NEXT_INSN (insn))
2055 {
2056 if (GET_CODE (insn) == CODE_LABEL)
2057 LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
2058 else if (GET_CODE (insn) == JUMP_INSN)
2059 JUMP_LABEL (insn) = 0;
2060 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2061 {
2062 rtx note, next;
2063
2064 for (note = REG_NOTES (insn); note; note = next)
2065 {
2066 next = XEXP (note, 1);
2067 if (REG_NOTE_KIND (note) == REG_LABEL
2068 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
2069 remove_note (insn, note);
2070 }
2071 }
2072 if (INSN_UID (insn) > largest_uid)
2073 largest_uid = INSN_UID (insn);
2074 }
2075
2076 return largest_uid;
2077 }
2078
2079 /* Delete insns following barriers, up to next label.
2080
2081 Also delete no-op jumps created by gcse. */
2082 static void
2083 delete_barrier_successors (f)
2084 rtx f;
2085 {
2086 rtx insn;
2087
2088 for (insn = f; insn;)
2089 {
2090 if (GET_CODE (insn) == BARRIER)
2091 {
2092 insn = NEXT_INSN (insn);
2093 while (insn != 0 && GET_CODE (insn) != CODE_LABEL)
2094 {
2095 if (GET_CODE (insn) == NOTE
2096 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
2097 insn = NEXT_INSN (insn);
2098 else
2099 insn = delete_insn (insn);
2100 }
2101 /* INSN is now the code_label. */
2102 }
2103 /* Also remove (set (pc) (pc)) insns which can be created by
2104 gcse. We eliminate such insns now to avoid having them
2105 cause problems later. */
2106 else if (GET_CODE (insn) == JUMP_INSN
2107 && SET_SRC (PATTERN (insn)) == pc_rtx
2108 && SET_DEST (PATTERN (insn)) == pc_rtx)
2109 insn = delete_insn (insn);
2110
2111 else
2112 insn = NEXT_INSN (insn);
2113 }
2114 }
2115
2116 /* Mark the label each jump jumps to.
2117 Combine consecutive labels, and count uses of labels.
2118
2119 For each label, make a chain (using `jump_chain')
2120 of all the *unconditional* jumps that jump to it;
2121 also make a chain of all returns.
2122
2123 CROSS_JUMP indicates whether we are doing cross jumping
2124 and if we are whether we will be paying attention to
2125 death notes or not. */
2126
2127 static void
2128 mark_all_labels (f, cross_jump)
2129 rtx f;
2130 int cross_jump;
2131 {
2132 rtx insn;
2133
2134 for (insn = f; insn; insn = NEXT_INSN (insn))
2135 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2136 {
2137 mark_jump_label (PATTERN (insn), insn, cross_jump);
2138 if (! INSN_DELETED_P (insn) && GET_CODE (insn) == JUMP_INSN)
2139 {
2140 if (JUMP_LABEL (insn) != 0 && simplejump_p (insn))
2141 {
2142 jump_chain[INSN_UID (insn)]
2143 = jump_chain[INSN_UID (JUMP_LABEL (insn))];
2144 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
2145 }
2146 if (GET_CODE (PATTERN (insn)) == RETURN)
2147 {
2148 jump_chain[INSN_UID (insn)] = jump_chain[0];
2149 jump_chain[0] = insn;
2150 }
2151 }
2152 }
2153 }
2154
2155 /* Delete all labels already not referenced.
2156 Also find and return the last insn. */
2157
2158 static rtx
2159 delete_unreferenced_labels (f)
2160 rtx f;
2161 {
2162 rtx final = NULL_RTX;
2163 rtx insn;
2164
2165 for (insn = f; insn; )
2166 {
2167 if (GET_CODE (insn) == CODE_LABEL && LABEL_NUSES (insn) == 0)
2168 insn = delete_insn (insn);
2169 else
2170 {
2171 final = insn;
2172 insn = NEXT_INSN (insn);
2173 }
2174 }
2175
2176 return final;
2177 }
2178
2179 /* Delete various simple forms of moves which have no necessary
2180 side effect. */
2181
2182 static void
2183 delete_noop_moves (f)
2184 rtx f;
2185 {
2186 rtx insn, next;
2187
2188 for (insn = f; insn; )
2189 {
2190 next = NEXT_INSN (insn);
2191
2192 if (GET_CODE (insn) == INSN)
2193 {
2194 register rtx body = PATTERN (insn);
2195
2196 /* Combine stack_adjusts with following push_insns. */
2197 #ifdef PUSH_ROUNDING
2198 if (GET_CODE (body) == SET
2199 && SET_DEST (body) == stack_pointer_rtx
2200 && GET_CODE (SET_SRC (body)) == PLUS
2201 && XEXP (SET_SRC (body), 0) == stack_pointer_rtx
2202 && GET_CODE (XEXP (SET_SRC (body), 1)) == CONST_INT
2203 && INTVAL (XEXP (SET_SRC (body), 1)) > 0)
2204 {
2205 rtx p;
2206 rtx stack_adjust_insn = insn;
2207 int stack_adjust_amount = INTVAL (XEXP (SET_SRC (body), 1));
2208 int total_pushed = 0;
2209 int pushes = 0;
2210
2211 /* Find all successive push insns. */
2212 p = insn;
2213 /* Don't convert more than three pushes;
2214 that starts adding too many displaced addresses
2215 and the whole thing starts becoming a losing
2216 proposition. */
2217 while (pushes < 3)
2218 {
2219 rtx pbody, dest;
2220 p = next_nonnote_insn (p);
2221 if (p == 0 || GET_CODE (p) != INSN)
2222 break;
2223 pbody = PATTERN (p);
2224 if (GET_CODE (pbody) != SET)
2225 break;
2226 dest = SET_DEST (pbody);
2227 /* Allow a no-op move between the adjust and the push. */
2228 if (GET_CODE (dest) == REG
2229 && GET_CODE (SET_SRC (pbody)) == REG
2230 && REGNO (dest) == REGNO (SET_SRC (pbody)))
2231 continue;
2232 if (! (GET_CODE (dest) == MEM
2233 && GET_CODE (XEXP (dest, 0)) == POST_INC
2234 && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
2235 break;
2236 pushes++;
2237 if (total_pushed + GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)))
2238 > stack_adjust_amount)
2239 break;
2240 total_pushed += GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)));
2241 }
2242
2243 /* Discard the amount pushed from the stack adjust;
2244 maybe eliminate it entirely. */
2245 if (total_pushed >= stack_adjust_amount)
2246 {
2247 delete_computation (stack_adjust_insn);
2248 total_pushed = stack_adjust_amount;
2249 }
2250 else
2251 XEXP (SET_SRC (PATTERN (stack_adjust_insn)), 1)
2252 = GEN_INT (stack_adjust_amount - total_pushed);
2253
2254 /* Change the appropriate push insns to ordinary stores. */
2255 p = insn;
2256 while (total_pushed > 0)
2257 {
2258 rtx pbody, dest;
2259 p = next_nonnote_insn (p);
2260 if (GET_CODE (p) != INSN)
2261 break;
2262 pbody = PATTERN (p);
2263 if (GET_CODE (pbody) != SET)
2264 break;
2265 dest = SET_DEST (pbody);
2266 /* Allow a no-op move between the adjust and the push. */
2267 if (GET_CODE (dest) == REG
2268 && GET_CODE (SET_SRC (pbody)) == REG
2269 && REGNO (dest) == REGNO (SET_SRC (pbody)))
2270 continue;
2271 if (! (GET_CODE (dest) == MEM
2272 && GET_CODE (XEXP (dest, 0)) == POST_INC
2273 && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx))
2274 break;
2275 total_pushed -= GET_MODE_SIZE (GET_MODE (SET_DEST (pbody)));
2276 /* If this push doesn't fully fit in the space
2277 of the stack adjust that we deleted,
2278 make another stack adjust here for what we
2279 didn't use up. There should be peepholes
2280 to recognize the resulting sequence of insns. */
2281 if (total_pushed < 0)
2282 {
2283 emit_insn_before (gen_add2_insn (stack_pointer_rtx,
2284 GEN_INT (- total_pushed)),
2285 p);
2286 break;
2287 }
2288 XEXP (dest, 0)
2289 = plus_constant (stack_pointer_rtx, total_pushed);
2290 }
2291 }
2292 #endif
2293
2294 /* Detect and delete no-op move instructions
2295 resulting from not allocating a parameter in a register. */
2296
2297 if (GET_CODE (body) == SET
2298 && (SET_DEST (body) == SET_SRC (body)
2299 || (GET_CODE (SET_DEST (body)) == MEM
2300 && GET_CODE (SET_SRC (body)) == MEM
2301 && rtx_equal_p (SET_SRC (body), SET_DEST (body))))
2302 && ! (GET_CODE (SET_DEST (body)) == MEM
2303 && MEM_VOLATILE_P (SET_DEST (body)))
2304 && ! (GET_CODE (SET_SRC (body)) == MEM
2305 && MEM_VOLATILE_P (SET_SRC (body))))
2306 delete_computation (insn);
2307
2308 /* Detect and ignore no-op move instructions
2309 resulting from smart or fortuitous register allocation. */
2310
2311 else if (GET_CODE (body) == SET)
2312 {
2313 int sreg = true_regnum (SET_SRC (body));
2314 int dreg = true_regnum (SET_DEST (body));
2315
2316 if (sreg == dreg && sreg >= 0)
2317 delete_insn (insn);
2318 else if (sreg >= 0 && dreg >= 0)
2319 {
2320 rtx trial;
2321 rtx tem = find_equiv_reg (NULL_RTX, insn, 0,
2322 sreg, NULL_PTR, dreg,
2323 GET_MODE (SET_SRC (body)));
2324
2325 if (tem != 0
2326 && GET_MODE (tem) == GET_MODE (SET_DEST (body)))
2327 {
2328 /* DREG may have been the target of a REG_DEAD note in
2329 the insn which makes INSN redundant. If so, reorg
2330 would still think it is dead. So search for such a
2331 note and delete it if we find it. */
2332 if (! find_regno_note (insn, REG_UNUSED, dreg))
2333 for (trial = prev_nonnote_insn (insn);
2334 trial && GET_CODE (trial) != CODE_LABEL;
2335 trial = prev_nonnote_insn (trial))
2336 if (find_regno_note (trial, REG_DEAD, dreg))
2337 {
2338 remove_death (dreg, trial);
2339 break;
2340 }
2341
2342 /* Deleting insn could lose a death-note for SREG. */
2343 if ((trial = find_regno_note (insn, REG_DEAD, sreg)))
2344 {
2345 /* Change this into a USE so that we won't emit
2346 code for it, but still can keep the note. */
2347 PATTERN (insn)
2348 = gen_rtx_USE (VOIDmode, XEXP (trial, 0));
2349 INSN_CODE (insn) = -1;
2350 /* Remove all reg notes but the REG_DEAD one. */
2351 REG_NOTES (insn) = trial;
2352 XEXP (trial, 1) = NULL_RTX;
2353 }
2354 else
2355 delete_insn (insn);
2356 }
2357 }
2358 else if (dreg >= 0 && CONSTANT_P (SET_SRC (body))
2359 && find_equiv_reg (SET_SRC (body), insn, 0, dreg,
2360 NULL_PTR, 0,
2361 GET_MODE (SET_DEST (body))))
2362 {
2363 /* This handles the case where we have two consecutive
2364 assignments of the same constant to pseudos that didn't
2365 get a hard reg. Each SET from the constant will be
2366 converted into a SET of the spill register and an
2367 output reload will be made following it. This produces
2368 two loads of the same constant into the same spill
2369 register. */
2370
2371 rtx in_insn = insn;
2372
2373 /* Look back for a death note for the first reg.
2374 If there is one, it is no longer accurate. */
2375 while (in_insn && GET_CODE (in_insn) != CODE_LABEL)
2376 {
2377 if ((GET_CODE (in_insn) == INSN
2378 || GET_CODE (in_insn) == JUMP_INSN)
2379 && find_regno_note (in_insn, REG_DEAD, dreg))
2380 {
2381 remove_death (dreg, in_insn);
2382 break;
2383 }
2384 in_insn = PREV_INSN (in_insn);
2385 }
2386
2387 /* Delete the second load of the value. */
2388 delete_insn (insn);
2389 }
2390 }
2391 else if (GET_CODE (body) == PARALLEL)
2392 {
2393 /* If each part is a set between two identical registers or
2394 a USE or CLOBBER, delete the insn. */
2395 int i, sreg, dreg;
2396 rtx tem;
2397
2398 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
2399 {
2400 tem = XVECEXP (body, 0, i);
2401 if (GET_CODE (tem) == USE || GET_CODE (tem) == CLOBBER)
2402 continue;
2403
2404 if (GET_CODE (tem) != SET
2405 || (sreg = true_regnum (SET_SRC (tem))) < 0
2406 || (dreg = true_regnum (SET_DEST (tem))) < 0
2407 || dreg != sreg)
2408 break;
2409 }
2410
2411 if (i < 0)
2412 delete_insn (insn);
2413 }
2414 /* Also delete insns to store bit fields if they are no-ops. */
2415 /* Not worth the hair to detect this in the big-endian case. */
2416 else if (! BYTES_BIG_ENDIAN
2417 && GET_CODE (body) == SET
2418 && GET_CODE (SET_DEST (body)) == ZERO_EXTRACT
2419 && XEXP (SET_DEST (body), 2) == const0_rtx
2420 && XEXP (SET_DEST (body), 0) == SET_SRC (body)
2421 && ! (GET_CODE (SET_SRC (body)) == MEM
2422 && MEM_VOLATILE_P (SET_SRC (body))))
2423 delete_insn (insn);
2424 }
2425 insn = next;
2426 }
2427 }
2428
2429 /* See if there is still a NOTE_INSN_FUNCTION_END in this function.
2430 If so indicate that this function can drop off the end by returning
2431 1, else return 0.
2432
2433 CHECK_DELETED indicates whether we must check if the note being
2434 searched for has the deleted flag set.
2435
2436 DELETE_FINAL_NOTE indicates whether we should delete the note
2437 if we find it. */
2438
2439 static int
2440 calculate_can_reach_end (last, check_deleted, delete_final_note)
2441 rtx last;
2442 int check_deleted;
2443 int delete_final_note;
2444 {
2445 rtx insn = last;
2446 int n_labels = 1;
2447
2448 while (insn != NULL_RTX)
2449 {
2450 int ok = 0;
2451
2452 /* One label can follow the end-note: the return label. */
2453 if (GET_CODE (insn) == CODE_LABEL && n_labels-- > 0)
2454 ok = 1;
2455 /* Ordinary insns can follow it if returning a structure. */
2456 else if (GET_CODE (insn) == INSN)
2457 ok = 1;
2458 /* If machine uses explicit RETURN insns, no epilogue,
2459 then one of them follows the note. */
2460 else if (GET_CODE (insn) == JUMP_INSN
2461 && GET_CODE (PATTERN (insn)) == RETURN)
2462 ok = 1;
2463 /* A barrier can follow the return insn. */
2464 else if (GET_CODE (insn) == BARRIER)
2465 ok = 1;
2466 /* Other kinds of notes can follow also. */
2467 else if (GET_CODE (insn) == NOTE
2468 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
2469 ok = 1;
2470
2471 if (ok != 1)
2472 break;
2473
2474 insn = PREV_INSN (insn);
2475 }
2476
2477 /* See if we backed up to the appropriate type of note. */
2478 if (insn != NULL_RTX
2479 && GET_CODE (insn) == NOTE
2480 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_END
2481 && (check_deleted == 0
2482 || ! INSN_DELETED_P (insn)))
2483 {
2484 if (delete_final_note)
2485 delete_insn (insn);
2486 return 1;
2487 }
2488
2489 return 0;
2490 }
2491
2492 /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
2493 jump. Assume that this unconditional jump is to the exit test code. If
2494 the code is sufficiently simple, make a copy of it before INSN,
2495 followed by a jump to the exit of the loop. Then delete the unconditional
2496 jump after INSN.
2497
2498 Return 1 if we made the change, else 0.
2499
2500 This is only safe immediately after a regscan pass because it uses the
2501 values of regno_first_uid and regno_last_uid. */
2502
2503 static int
2504 duplicate_loop_exit_test (loop_start)
2505 rtx loop_start;
2506 {
2507 rtx insn, set, reg, p, link;
2508 rtx copy = 0;
2509 int num_insns = 0;
2510 rtx exitcode = NEXT_INSN (JUMP_LABEL (next_nonnote_insn (loop_start)));
2511 rtx lastexit;
2512 int max_reg = max_reg_num ();
2513 rtx *reg_map = 0;
2514
2515 /* Scan the exit code. We do not perform this optimization if any insn:
2516
2517 is a CALL_INSN
2518 is a CODE_LABEL
2519 has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
2520 is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
2521 is a NOTE_INSN_BLOCK_{BEG,END} because duplicating these notes
2522 is not valid.
2523
2524 We also do not do this if we find an insn with ASM_OPERANDS. While
2525 this restriction should not be necessary, copying an insn with
2526 ASM_OPERANDS can confuse asm_noperands in some cases.
2527
2528 Also, don't do this if the exit code is more than 20 insns. */
2529
2530 for (insn = exitcode;
2531 insn
2532 && ! (GET_CODE (insn) == NOTE
2533 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
2534 insn = NEXT_INSN (insn))
2535 {
2536 switch (GET_CODE (insn))
2537 {
2538 case CODE_LABEL:
2539 case CALL_INSN:
2540 return 0;
2541 case NOTE:
2542 /* We could be in front of the wrong NOTE_INSN_LOOP_END if there is
2543 a jump immediately after the loop start that branches outside
2544 the loop but within an outer loop, near the exit test.
2545 If we copied this exit test and created a phony
2546 NOTE_INSN_LOOP_VTOP, this could make instructions immediately
2547 before the exit test look like these could be safely moved
2548 out of the loop even if they actually may be never executed.
2549 This can be avoided by checking here for NOTE_INSN_LOOP_CONT. */
2550
2551 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
2552 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
2553 return 0;
2554
2555 if (optimize < 2
2556 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2557 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
2558 /* If we were to duplicate this code, we would not move
2559 the BLOCK notes, and so debugging the moved code would
2560 be difficult. Thus, we only move the code with -O2 or
2561 higher. */
2562 return 0;
2563
2564 break;
2565 case JUMP_INSN:
2566 case INSN:
2567 /* The code below would grossly mishandle REG_WAS_0 notes,
2568 so get rid of them here. */
2569 while ((p = find_reg_note (insn, REG_WAS_0, NULL_RTX)) != 0)
2570 remove_note (insn, p);
2571 if (++num_insns > 20
2572 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
2573 || find_reg_note (insn, REG_LIBCALL, NULL_RTX)
2574 || asm_noperands (PATTERN (insn)) > 0)
2575 return 0;
2576 break;
2577 default:
2578 break;
2579 }
2580 }
2581
2582 /* Unless INSN is zero, we can do the optimization. */
2583 if (insn == 0)
2584 return 0;
2585
2586 lastexit = insn;
2587
2588 /* See if any insn sets a register only used in the loop exit code and
2589 not a user variable. If so, replace it with a new register. */
2590 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
2591 if (GET_CODE (insn) == INSN
2592 && (set = single_set (insn)) != 0
2593 && ((reg = SET_DEST (set), GET_CODE (reg) == REG)
2594 || (GET_CODE (reg) == SUBREG
2595 && (reg = SUBREG_REG (reg), GET_CODE (reg) == REG)))
2596 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
2597 && REGNO_FIRST_UID (REGNO (reg)) == INSN_UID (insn))
2598 {
2599 for (p = NEXT_INSN (insn); p != lastexit; p = NEXT_INSN (p))
2600 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (p))
2601 break;
2602
2603 if (p != lastexit)
2604 {
2605 /* We can do the replacement. Allocate reg_map if this is the
2606 first replacement we found. */
2607 if (reg_map == 0)
2608 {
2609 reg_map = (rtx *) alloca (max_reg * sizeof (rtx));
2610 bzero ((char *) reg_map, max_reg * sizeof (rtx));
2611 }
2612
2613 REG_LOOP_TEST_P (reg) = 1;
2614
2615 reg_map[REGNO (reg)] = gen_reg_rtx (GET_MODE (reg));
2616 }
2617 }
2618
2619 /* Now copy each insn. */
2620 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
2621 switch (GET_CODE (insn))
2622 {
2623 case BARRIER:
2624 copy = emit_barrier_before (loop_start);
2625 break;
2626 case NOTE:
2627 /* Only copy line-number notes. */
2628 if (NOTE_LINE_NUMBER (insn) >= 0)
2629 {
2630 copy = emit_note_before (NOTE_LINE_NUMBER (insn), loop_start);
2631 NOTE_SOURCE_FILE (copy) = NOTE_SOURCE_FILE (insn);
2632 }
2633 break;
2634
2635 case INSN:
2636 copy = emit_insn_before (copy_rtx (PATTERN (insn)), loop_start);
2637 if (reg_map)
2638 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
2639
2640 mark_jump_label (PATTERN (copy), copy, 0);
2641
2642 /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
2643 make them. */
2644 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2645 if (REG_NOTE_KIND (link) != REG_LABEL)
2646 REG_NOTES (copy)
2647 = copy_rtx (gen_rtx_EXPR_LIST (REG_NOTE_KIND (link),
2648 XEXP (link, 0),
2649 REG_NOTES (copy)));
2650 if (reg_map && REG_NOTES (copy))
2651 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
2652 break;
2653
2654 case JUMP_INSN:
2655 copy = emit_jump_insn_before (copy_rtx (PATTERN (insn)), loop_start);
2656 if (reg_map)
2657 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
2658 mark_jump_label (PATTERN (copy), copy, 0);
2659 if (REG_NOTES (insn))
2660 {
2661 REG_NOTES (copy) = copy_rtx (REG_NOTES (insn));
2662 if (reg_map)
2663 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
2664 }
2665
2666 /* If this is a simple jump, add it to the jump chain. */
2667
2668 if (INSN_UID (copy) < max_jump_chain && JUMP_LABEL (copy)
2669 && simplejump_p (copy))
2670 {
2671 jump_chain[INSN_UID (copy)]
2672 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
2673 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
2674 }
2675 break;
2676
2677 default:
2678 abort ();
2679 }
2680
2681 /* Now clean up by emitting a jump to the end label and deleting the jump
2682 at the start of the loop. */
2683 if (! copy || GET_CODE (copy) != BARRIER)
2684 {
2685 copy = emit_jump_insn_before (gen_jump (get_label_after (insn)),
2686 loop_start);
2687 mark_jump_label (PATTERN (copy), copy, 0);
2688 if (INSN_UID (copy) < max_jump_chain
2689 && INSN_UID (JUMP_LABEL (copy)) < max_jump_chain)
2690 {
2691 jump_chain[INSN_UID (copy)]
2692 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
2693 jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
2694 }
2695 emit_barrier_before (loop_start);
2696 }
2697
2698 /* Mark the exit code as the virtual top of the converted loop. */
2699 emit_note_before (NOTE_INSN_LOOP_VTOP, exitcode);
2700
2701 delete_insn (next_nonnote_insn (loop_start));
2702
2703 return 1;
2704 }
2705 \f
2706 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, and
2707 loop-end notes between START and END out before START. Assume that
2708 END is not such a note. START may be such a note. Returns the value
2709 of the new starting insn, which may be different if the original start
2710 was such a note. */
2711
2712 rtx
2713 squeeze_notes (start, end)
2714 rtx start, end;
2715 {
2716 rtx insn;
2717 rtx next;
2718
2719 for (insn = start; insn != end; insn = next)
2720 {
2721 next = NEXT_INSN (insn);
2722 if (GET_CODE (insn) == NOTE
2723 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
2724 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2725 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
2726 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
2727 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
2728 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP))
2729 {
2730 if (insn == start)
2731 start = next;
2732 else
2733 {
2734 rtx prev = PREV_INSN (insn);
2735 PREV_INSN (insn) = PREV_INSN (start);
2736 NEXT_INSN (insn) = start;
2737 NEXT_INSN (PREV_INSN (insn)) = insn;
2738 PREV_INSN (NEXT_INSN (insn)) = insn;
2739 NEXT_INSN (prev) = next;
2740 PREV_INSN (next) = prev;
2741 }
2742 }
2743 }
2744
2745 return start;
2746 }
2747 \f
2748 /* Compare the instructions before insn E1 with those before E2
2749 to find an opportunity for cross jumping.
2750 (This means detecting identical sequences of insns followed by
2751 jumps to the same place, or followed by a label and a jump
2752 to that label, and replacing one with a jump to the other.)
2753
2754 Assume E1 is a jump that jumps to label E2
2755 (that is not always true but it might as well be).
2756 Find the longest possible equivalent sequences
2757 and store the first insns of those sequences into *F1 and *F2.
2758 Store zero there if no equivalent preceding instructions are found.
2759
2760 We give up if we find a label in stream 1.
2761 Actually we could transfer that label into stream 2. */
2762
2763 static void
2764 find_cross_jump (e1, e2, minimum, f1, f2)
2765 rtx e1, e2;
2766 int minimum;
2767 rtx *f1, *f2;
2768 {
2769 register rtx i1 = e1, i2 = e2;
2770 register rtx p1, p2;
2771 int lose = 0;
2772
2773 rtx last1 = 0, last2 = 0;
2774 rtx afterlast1 = 0, afterlast2 = 0;
2775
2776 *f1 = 0;
2777 *f2 = 0;
2778
2779 while (1)
2780 {
2781 i1 = prev_nonnote_insn (i1);
2782
2783 i2 = PREV_INSN (i2);
2784 while (i2 && (GET_CODE (i2) == NOTE || GET_CODE (i2) == CODE_LABEL))
2785 i2 = PREV_INSN (i2);
2786
2787 if (i1 == 0)
2788 break;
2789
2790 /* Don't allow the range of insns preceding E1 or E2
2791 to include the other (E2 or E1). */
2792 if (i2 == e1 || i1 == e2)
2793 break;
2794
2795 /* If we will get to this code by jumping, those jumps will be
2796 tensioned to go directly to the new label (before I2),
2797 so this cross-jumping won't cost extra. So reduce the minimum. */
2798 if (GET_CODE (i1) == CODE_LABEL)
2799 {
2800 --minimum;
2801 break;
2802 }
2803
2804 if (i2 == 0 || GET_CODE (i1) != GET_CODE (i2))
2805 break;
2806
2807 /* Avoid moving insns across EH regions if either of the insns
2808 can throw. */
2809 if (flag_exceptions
2810 && (asynchronous_exceptions || GET_CODE (i1) == CALL_INSN)
2811 && !in_same_eh_region (i1, i2))
2812 break;
2813
2814 p1 = PATTERN (i1);
2815 p2 = PATTERN (i2);
2816
2817 /* If this is a CALL_INSN, compare register usage information.
2818 If we don't check this on stack register machines, the two
2819 CALL_INSNs might be merged leaving reg-stack.c with mismatching
2820 numbers of stack registers in the same basic block.
2821 If we don't check this on machines with delay slots, a delay slot may
2822 be filled that clobbers a parameter expected by the subroutine.
2823
2824 ??? We take the simple route for now and assume that if they're
2825 equal, they were constructed identically. */
2826
2827 if (GET_CODE (i1) == CALL_INSN
2828 && ! rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
2829 CALL_INSN_FUNCTION_USAGE (i2)))
2830 lose = 1;
2831
2832 #ifdef STACK_REGS
2833 /* If cross_jump_death_matters is not 0, the insn's mode
2834 indicates whether or not the insn contains any stack-like
2835 regs. */
2836
2837 if (!lose && cross_jump_death_matters && GET_MODE (i1) == QImode)
2838 {
2839 /* If register stack conversion has already been done, then
2840 death notes must also be compared before it is certain that
2841 the two instruction streams match. */
2842
2843 rtx note;
2844 HARD_REG_SET i1_regset, i2_regset;
2845
2846 CLEAR_HARD_REG_SET (i1_regset);
2847 CLEAR_HARD_REG_SET (i2_regset);
2848
2849 for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
2850 if (REG_NOTE_KIND (note) == REG_DEAD
2851 && STACK_REG_P (XEXP (note, 0)))
2852 SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
2853
2854 for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
2855 if (REG_NOTE_KIND (note) == REG_DEAD
2856 && STACK_REG_P (XEXP (note, 0)))
2857 SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
2858
2859 GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
2860
2861 lose = 1;
2862
2863 done:
2864 ;
2865 }
2866 #endif
2867
2868 /* Don't allow old-style asm or volatile extended asms to be accepted
2869 for cross jumping purposes. It is conceptually correct to allow
2870 them, since cross-jumping preserves the dynamic instruction order
2871 even though it is changing the static instruction order. However,
2872 if an asm is being used to emit an assembler pseudo-op, such as
2873 the MIPS `.set reorder' pseudo-op, then the static instruction order
2874 matters and it must be preserved. */
2875 if (GET_CODE (p1) == ASM_INPUT || GET_CODE (p2) == ASM_INPUT
2876 || (GET_CODE (p1) == ASM_OPERANDS && MEM_VOLATILE_P (p1))
2877 || (GET_CODE (p2) == ASM_OPERANDS && MEM_VOLATILE_P (p2)))
2878 lose = 1;
2879
2880 if (lose || GET_CODE (p1) != GET_CODE (p2)
2881 || ! rtx_renumbered_equal_p (p1, p2))
2882 {
2883 /* The following code helps take care of G++ cleanups. */
2884 rtx equiv1;
2885 rtx equiv2;
2886
2887 if (!lose && GET_CODE (p1) == GET_CODE (p2)
2888 && ((equiv1 = find_reg_note (i1, REG_EQUAL, NULL_RTX)) != 0
2889 || (equiv1 = find_reg_note (i1, REG_EQUIV, NULL_RTX)) != 0)
2890 && ((equiv2 = find_reg_note (i2, REG_EQUAL, NULL_RTX)) != 0
2891 || (equiv2 = find_reg_note (i2, REG_EQUIV, NULL_RTX)) != 0)
2892 /* If the equivalences are not to a constant, they may
2893 reference pseudos that no longer exist, so we can't
2894 use them. */
2895 && CONSTANT_P (XEXP (equiv1, 0))
2896 && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
2897 {
2898 rtx s1 = single_set (i1);
2899 rtx s2 = single_set (i2);
2900 if (s1 != 0 && s2 != 0
2901 && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
2902 {
2903 validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
2904 validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
2905 if (! rtx_renumbered_equal_p (p1, p2))
2906 cancel_changes (0);
2907 else if (apply_change_group ())
2908 goto win;
2909 }
2910 }
2911
2912 /* Insns fail to match; cross jumping is limited to the following
2913 insns. */
2914
2915 #ifdef HAVE_cc0
2916 /* Don't allow the insn after a compare to be shared by
2917 cross-jumping unless the compare is also shared.
2918 Here, if either of these non-matching insns is a compare,
2919 exclude the following insn from possible cross-jumping. */
2920 if (sets_cc0_p (p1) || sets_cc0_p (p2))
2921 last1 = afterlast1, last2 = afterlast2, ++minimum;
2922 #endif
2923
2924 /* If cross-jumping here will feed a jump-around-jump
2925 optimization, this jump won't cost extra, so reduce
2926 the minimum. */
2927 if (GET_CODE (i1) == JUMP_INSN
2928 && JUMP_LABEL (i1)
2929 && prev_real_insn (JUMP_LABEL (i1)) == e1)
2930 --minimum;
2931 break;
2932 }
2933
2934 win:
2935 if (GET_CODE (p1) != USE && GET_CODE (p1) != CLOBBER)
2936 {
2937 /* Ok, this insn is potentially includable in a cross-jump here. */
2938 afterlast1 = last1, afterlast2 = last2;
2939 last1 = i1, last2 = i2, --minimum;
2940 }
2941 }
2942
2943 if (minimum <= 0 && last1 != 0 && last1 != e1)
2944 *f1 = last1, *f2 = last2;
2945 }
2946
2947 static void
2948 do_cross_jump (insn, newjpos, newlpos)
2949 rtx insn, newjpos, newlpos;
2950 {
2951 /* Find an existing label at this point
2952 or make a new one if there is none. */
2953 register rtx label = get_label_before (newlpos);
2954
2955 /* Make the same jump insn jump to the new point. */
2956 if (GET_CODE (PATTERN (insn)) == RETURN)
2957 {
2958 /* Remove from jump chain of returns. */
2959 delete_from_jump_chain (insn);
2960 /* Change the insn. */
2961 PATTERN (insn) = gen_jump (label);
2962 INSN_CODE (insn) = -1;
2963 JUMP_LABEL (insn) = label;
2964 LABEL_NUSES (label)++;
2965 /* Add to new the jump chain. */
2966 if (INSN_UID (label) < max_jump_chain
2967 && INSN_UID (insn) < max_jump_chain)
2968 {
2969 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (label)];
2970 jump_chain[INSN_UID (label)] = insn;
2971 }
2972 }
2973 else
2974 redirect_jump (insn, label);
2975
2976 /* Delete the matching insns before the jump. Also, remove any REG_EQUAL
2977 or REG_EQUIV note in the NEWLPOS stream that isn't also present in
2978 the NEWJPOS stream. */
2979
2980 while (newjpos != insn)
2981 {
2982 rtx lnote;
2983
2984 for (lnote = REG_NOTES (newlpos); lnote; lnote = XEXP (lnote, 1))
2985 if ((REG_NOTE_KIND (lnote) == REG_EQUAL
2986 || REG_NOTE_KIND (lnote) == REG_EQUIV)
2987 && ! find_reg_note (newjpos, REG_EQUAL, XEXP (lnote, 0))
2988 && ! find_reg_note (newjpos, REG_EQUIV, XEXP (lnote, 0)))
2989 remove_note (newlpos, lnote);
2990
2991 delete_insn (newjpos);
2992 newjpos = next_real_insn (newjpos);
2993 newlpos = next_real_insn (newlpos);
2994 }
2995 }
2996 \f
2997 /* Return the label before INSN, or put a new label there. */
2998
2999 rtx
3000 get_label_before (insn)
3001 rtx insn;
3002 {
3003 rtx label;
3004
3005 /* Find an existing label at this point
3006 or make a new one if there is none. */
3007 label = prev_nonnote_insn (insn);
3008
3009 if (label == 0 || GET_CODE (label) != CODE_LABEL)
3010 {
3011 rtx prev = PREV_INSN (insn);
3012
3013 label = gen_label_rtx ();
3014 emit_label_after (label, prev);
3015 LABEL_NUSES (label) = 0;
3016 }
3017 return label;
3018 }
3019
3020 /* Return the label after INSN, or put a new label there. */
3021
3022 rtx
3023 get_label_after (insn)
3024 rtx insn;
3025 {
3026 rtx label;
3027
3028 /* Find an existing label at this point
3029 or make a new one if there is none. */
3030 label = next_nonnote_insn (insn);
3031
3032 if (label == 0 || GET_CODE (label) != CODE_LABEL)
3033 {
3034 label = gen_label_rtx ();
3035 emit_label_after (label, insn);
3036 LABEL_NUSES (label) = 0;
3037 }
3038 return label;
3039 }
3040 \f
3041 /* Return 1 if INSN is a jump that jumps to right after TARGET
3042 only on the condition that TARGET itself would drop through.
3043 Assumes that TARGET is a conditional jump. */
3044
3045 static int
3046 jump_back_p (insn, target)
3047 rtx insn, target;
3048 {
3049 rtx cinsn, ctarget;
3050 enum rtx_code codei, codet;
3051
3052 if (simplejump_p (insn) || ! condjump_p (insn)
3053 || simplejump_p (target)
3054 || target != prev_real_insn (JUMP_LABEL (insn)))
3055 return 0;
3056
3057 cinsn = XEXP (SET_SRC (PATTERN (insn)), 0);
3058 ctarget = XEXP (SET_SRC (PATTERN (target)), 0);
3059
3060 codei = GET_CODE (cinsn);
3061 codet = GET_CODE (ctarget);
3062
3063 if (XEXP (SET_SRC (PATTERN (insn)), 1) == pc_rtx)
3064 {
3065 if (! can_reverse_comparison_p (cinsn, insn))
3066 return 0;
3067 codei = reverse_condition (codei);
3068 }
3069
3070 if (XEXP (SET_SRC (PATTERN (target)), 2) == pc_rtx)
3071 {
3072 if (! can_reverse_comparison_p (ctarget, target))
3073 return 0;
3074 codet = reverse_condition (codet);
3075 }
3076
3077 return (codei == codet
3078 && rtx_renumbered_equal_p (XEXP (cinsn, 0), XEXP (ctarget, 0))
3079 && rtx_renumbered_equal_p (XEXP (cinsn, 1), XEXP (ctarget, 1)));
3080 }
3081 \f
3082 /* Given a comparison, COMPARISON, inside a conditional jump insn, INSN,
3083 return non-zero if it is safe to reverse this comparison. It is if our
3084 floating-point is not IEEE, if this is an NE or EQ comparison, or if
3085 this is known to be an integer comparison. */
3086
3087 int
3088 can_reverse_comparison_p (comparison, insn)
3089 rtx comparison;
3090 rtx insn;
3091 {
3092 rtx arg0;
3093
3094 /* If this is not actually a comparison, we can't reverse it. */
3095 if (GET_RTX_CLASS (GET_CODE (comparison)) != '<')
3096 return 0;
3097
3098 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3099 /* If this is an NE comparison, it is safe to reverse it to an EQ
3100 comparison and vice versa, even for floating point. If no operands
3101 are NaNs, the reversal is valid. If some operand is a NaN, EQ is
3102 always false and NE is always true, so the reversal is also valid. */
3103 || flag_fast_math
3104 || GET_CODE (comparison) == NE
3105 || GET_CODE (comparison) == EQ)
3106 return 1;
3107
3108 arg0 = XEXP (comparison, 0);
3109
3110 /* Make sure ARG0 is one of the actual objects being compared. If we
3111 can't do this, we can't be sure the comparison can be reversed.
3112
3113 Handle cc0 and a MODE_CC register. */
3114 if ((GET_CODE (arg0) == REG && GET_MODE_CLASS (GET_MODE (arg0)) == MODE_CC)
3115 #ifdef HAVE_cc0
3116 || arg0 == cc0_rtx
3117 #endif
3118 )
3119 {
3120 rtx prev = prev_nonnote_insn (insn);
3121 rtx set = single_set (prev);
3122
3123 if (set == 0 || SET_DEST (set) != arg0)
3124 return 0;
3125
3126 arg0 = SET_SRC (set);
3127
3128 if (GET_CODE (arg0) == COMPARE)
3129 arg0 = XEXP (arg0, 0);
3130 }
3131
3132 /* We can reverse this if ARG0 is a CONST_INT or if its mode is
3133 not VOIDmode and neither a MODE_CC nor MODE_FLOAT type. */
3134 return (GET_CODE (arg0) == CONST_INT
3135 || (GET_MODE (arg0) != VOIDmode
3136 && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_CC
3137 && GET_MODE_CLASS (GET_MODE (arg0)) != MODE_FLOAT));
3138 }
3139
3140 /* Given an rtx-code for a comparison, return the code
3141 for the negated comparison.
3142 WATCH OUT! reverse_condition is not safe to use on a jump
3143 that might be acting on the results of an IEEE floating point comparison,
3144 because of the special treatment of non-signaling nans in comparisons.
3145 Use can_reverse_comparison_p to be sure. */
3146
3147 enum rtx_code
3148 reverse_condition (code)
3149 enum rtx_code code;
3150 {
3151 switch (code)
3152 {
3153 case EQ:
3154 return NE;
3155
3156 case NE:
3157 return EQ;
3158
3159 case GT:
3160 return LE;
3161
3162 case GE:
3163 return LT;
3164
3165 case LT:
3166 return GE;
3167
3168 case LE:
3169 return GT;
3170
3171 case GTU:
3172 return LEU;
3173
3174 case GEU:
3175 return LTU;
3176
3177 case LTU:
3178 return GEU;
3179
3180 case LEU:
3181 return GTU;
3182
3183 default:
3184 abort ();
3185 return UNKNOWN;
3186 }
3187 }
3188
3189 /* Similar, but return the code when two operands of a comparison are swapped.
3190 This IS safe for IEEE floating-point. */
3191
3192 enum rtx_code
3193 swap_condition (code)
3194 enum rtx_code code;
3195 {
3196 switch (code)
3197 {
3198 case EQ:
3199 case NE:
3200 return code;
3201
3202 case GT:
3203 return LT;
3204
3205 case GE:
3206 return LE;
3207
3208 case LT:
3209 return GT;
3210
3211 case LE:
3212 return GE;
3213
3214 case GTU:
3215 return LTU;
3216
3217 case GEU:
3218 return LEU;
3219
3220 case LTU:
3221 return GTU;
3222
3223 case LEU:
3224 return GEU;
3225
3226 default:
3227 abort ();
3228 return UNKNOWN;
3229 }
3230 }
3231
3232 /* Given a comparison CODE, return the corresponding unsigned comparison.
3233 If CODE is an equality comparison or already an unsigned comparison,
3234 CODE is returned. */
3235
3236 enum rtx_code
3237 unsigned_condition (code)
3238 enum rtx_code code;
3239 {
3240 switch (code)
3241 {
3242 case EQ:
3243 case NE:
3244 case GTU:
3245 case GEU:
3246 case LTU:
3247 case LEU:
3248 return code;
3249
3250 case GT:
3251 return GTU;
3252
3253 case GE:
3254 return GEU;
3255
3256 case LT:
3257 return LTU;
3258
3259 case LE:
3260 return LEU;
3261
3262 default:
3263 abort ();
3264 }
3265 }
3266
3267 /* Similarly, return the signed version of a comparison. */
3268
3269 enum rtx_code
3270 signed_condition (code)
3271 enum rtx_code code;
3272 {
3273 switch (code)
3274 {
3275 case EQ:
3276 case NE:
3277 case GT:
3278 case GE:
3279 case LT:
3280 case LE:
3281 return code;
3282
3283 case GTU:
3284 return GT;
3285
3286 case GEU:
3287 return GE;
3288
3289 case LTU:
3290 return LT;
3291
3292 case LEU:
3293 return LE;
3294
3295 default:
3296 abort ();
3297 }
3298 }
3299 \f
3300 /* Return non-zero if CODE1 is more strict than CODE2, i.e., if the
3301 truth of CODE1 implies the truth of CODE2. */
3302
3303 int
3304 comparison_dominates_p (code1, code2)
3305 enum rtx_code code1, code2;
3306 {
3307 if (code1 == code2)
3308 return 1;
3309
3310 switch (code1)
3311 {
3312 case EQ:
3313 if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU)
3314 return 1;
3315 break;
3316
3317 case LT:
3318 if (code2 == LE || code2 == NE)
3319 return 1;
3320 break;
3321
3322 case GT:
3323 if (code2 == GE || code2 == NE)
3324 return 1;
3325 break;
3326
3327 case LTU:
3328 if (code2 == LEU || code2 == NE)
3329 return 1;
3330 break;
3331
3332 case GTU:
3333 if (code2 == GEU || code2 == NE)
3334 return 1;
3335 break;
3336
3337 default:
3338 break;
3339 }
3340
3341 return 0;
3342 }
3343 \f
3344 /* Return 1 if INSN is an unconditional jump and nothing else. */
3345
3346 int
3347 simplejump_p (insn)
3348 rtx insn;
3349 {
3350 return (GET_CODE (insn) == JUMP_INSN
3351 && GET_CODE (PATTERN (insn)) == SET
3352 && GET_CODE (SET_DEST (PATTERN (insn))) == PC
3353 && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
3354 }
3355
3356 /* Return nonzero if INSN is a (possibly) conditional jump
3357 and nothing more. */
3358
3359 int
3360 condjump_p (insn)
3361 rtx insn;
3362 {
3363 register rtx x = PATTERN (insn);
3364 if (GET_CODE (x) != SET)
3365 return 0;
3366 if (GET_CODE (SET_DEST (x)) != PC)
3367 return 0;
3368 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
3369 return 1;
3370 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
3371 return 0;
3372 if (XEXP (SET_SRC (x), 2) == pc_rtx
3373 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
3374 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
3375 return 1;
3376 if (XEXP (SET_SRC (x), 1) == pc_rtx
3377 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
3378 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
3379 return 1;
3380 return 0;
3381 }
3382
3383 /* Return nonzero if INSN is a (possibly) conditional jump
3384 and nothing more. */
3385
3386 int
3387 condjump_in_parallel_p (insn)
3388 rtx insn;
3389 {
3390 register rtx x = PATTERN (insn);
3391
3392 if (GET_CODE (x) != PARALLEL)
3393 return 0;
3394 else
3395 x = XVECEXP (x, 0, 0);
3396
3397 if (GET_CODE (x) != SET)
3398 return 0;
3399 if (GET_CODE (SET_DEST (x)) != PC)
3400 return 0;
3401 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
3402 return 1;
3403 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
3404 return 0;
3405 if (XEXP (SET_SRC (x), 2) == pc_rtx
3406 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
3407 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
3408 return 1;
3409 if (XEXP (SET_SRC (x), 1) == pc_rtx
3410 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
3411 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
3412 return 1;
3413 return 0;
3414 }
3415
3416 /* Return the label of a conditional jump. */
3417
3418 rtx
3419 condjump_label (insn)
3420 rtx insn;
3421 {
3422 register rtx x = PATTERN (insn);
3423
3424 if (GET_CODE (x) == PARALLEL)
3425 x = XVECEXP (x, 0, 0);
3426 if (GET_CODE (x) != SET)
3427 return NULL_RTX;
3428 if (GET_CODE (SET_DEST (x)) != PC)
3429 return NULL_RTX;
3430 x = SET_SRC (x);
3431 if (GET_CODE (x) == LABEL_REF)
3432 return x;
3433 if (GET_CODE (x) != IF_THEN_ELSE)
3434 return NULL_RTX;
3435 if (XEXP (x, 2) == pc_rtx && GET_CODE (XEXP (x, 1)) == LABEL_REF)
3436 return XEXP (x, 1);
3437 if (XEXP (x, 1) == pc_rtx && GET_CODE (XEXP (x, 2)) == LABEL_REF)
3438 return XEXP (x, 2);
3439 return NULL_RTX;
3440 }
3441
3442 /* Return true if INSN is a (possibly conditional) return insn. */
3443
3444 static int
3445 returnjump_p_1 (loc, data)
3446 rtx *loc;
3447 void *data ATTRIBUTE_UNUSED;
3448 {
3449 rtx x = *loc;
3450 return GET_CODE (x) == RETURN;
3451 }
3452
3453 int
3454 returnjump_p (insn)
3455 rtx insn;
3456 {
3457 return for_each_rtx (&PATTERN (insn), returnjump_p_1, NULL);
3458 }
3459
3460 #ifdef HAVE_cc0
3461
3462 /* Return 1 if X is an RTX that does nothing but set the condition codes
3463 and CLOBBER or USE registers.
3464 Return -1 if X does explicitly set the condition codes,
3465 but also does other things. */
3466
3467 int
3468 sets_cc0_p (x)
3469 rtx x ATTRIBUTE_UNUSED;
3470 {
3471 if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
3472 return 1;
3473 if (GET_CODE (x) == PARALLEL)
3474 {
3475 int i;
3476 int sets_cc0 = 0;
3477 int other_things = 0;
3478 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
3479 {
3480 if (GET_CODE (XVECEXP (x, 0, i)) == SET
3481 && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
3482 sets_cc0 = 1;
3483 else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
3484 other_things = 1;
3485 }
3486 return ! sets_cc0 ? 0 : other_things ? -1 : 1;
3487 }
3488 return 0;
3489 }
3490 #endif
3491 \f
3492 /* Follow any unconditional jump at LABEL;
3493 return the ultimate label reached by any such chain of jumps.
3494 If LABEL is not followed by a jump, return LABEL.
3495 If the chain loops or we can't find end, return LABEL,
3496 since that tells caller to avoid changing the insn.
3497
3498 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
3499 a USE or CLOBBER. */
3500
3501 rtx
3502 follow_jumps (label)
3503 rtx label;
3504 {
3505 register rtx insn;
3506 register rtx next;
3507 register rtx value = label;
3508 register int depth;
3509
3510 for (depth = 0;
3511 (depth < 10
3512 && (insn = next_active_insn (value)) != 0
3513 && GET_CODE (insn) == JUMP_INSN
3514 && ((JUMP_LABEL (insn) != 0 && simplejump_p (insn))
3515 || GET_CODE (PATTERN (insn)) == RETURN)
3516 && (next = NEXT_INSN (insn))
3517 && GET_CODE (next) == BARRIER);
3518 depth++)
3519 {
3520 /* Don't chain through the insn that jumps into a loop
3521 from outside the loop,
3522 since that would create multiple loop entry jumps
3523 and prevent loop optimization. */
3524 rtx tem;
3525 if (!reload_completed)
3526 for (tem = value; tem != insn; tem = NEXT_INSN (tem))
3527 if (GET_CODE (tem) == NOTE
3528 && (NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG
3529 /* ??? Optional. Disables some optimizations, but makes
3530 gcov output more accurate with -O. */
3531 || (flag_test_coverage && NOTE_LINE_NUMBER (tem) > 0)))
3532 return value;
3533
3534 /* If we have found a cycle, make the insn jump to itself. */
3535 if (JUMP_LABEL (insn) == label)
3536 return label;
3537
3538 tem = next_active_insn (JUMP_LABEL (insn));
3539 if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
3540 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
3541 break;
3542
3543 value = JUMP_LABEL (insn);
3544 }
3545 if (depth == 10)
3546 return label;
3547 return value;
3548 }
3549
3550 /* Assuming that field IDX of X is a vector of label_refs,
3551 replace each of them by the ultimate label reached by it.
3552 Return nonzero if a change is made.
3553 If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG. */
3554
3555 static int
3556 tension_vector_labels (x, idx)
3557 register rtx x;
3558 register int idx;
3559 {
3560 int changed = 0;
3561 register int i;
3562 for (i = XVECLEN (x, idx) - 1; i >= 0; i--)
3563 {
3564 register rtx olabel = XEXP (XVECEXP (x, idx, i), 0);
3565 register rtx nlabel = follow_jumps (olabel);
3566 if (nlabel && nlabel != olabel)
3567 {
3568 XEXP (XVECEXP (x, idx, i), 0) = nlabel;
3569 ++LABEL_NUSES (nlabel);
3570 if (--LABEL_NUSES (olabel) == 0)
3571 delete_insn (olabel);
3572 changed = 1;
3573 }
3574 }
3575 return changed;
3576 }
3577 \f
3578 /* Find all CODE_LABELs referred to in X, and increment their use counts.
3579 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
3580 in INSN, then store one of them in JUMP_LABEL (INSN).
3581 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
3582 referenced in INSN, add a REG_LABEL note containing that label to INSN.
3583 Also, when there are consecutive labels, canonicalize on the last of them.
3584
3585 Note that two labels separated by a loop-beginning note
3586 must be kept distinct if we have not yet done loop-optimization,
3587 because the gap between them is where loop-optimize
3588 will want to move invariant code to. CROSS_JUMP tells us
3589 that loop-optimization is done with.
3590
3591 Once reload has completed (CROSS_JUMP non-zero), we need not consider
3592 two labels distinct if they are separated by only USE or CLOBBER insns. */
3593
3594 static void
3595 mark_jump_label (x, insn, cross_jump)
3596 register rtx x;
3597 rtx insn;
3598 int cross_jump;
3599 {
3600 register RTX_CODE code = GET_CODE (x);
3601 register int i;
3602 register char *fmt;
3603
3604 switch (code)
3605 {
3606 case PC:
3607 case CC0:
3608 case REG:
3609 case SUBREG:
3610 case CONST_INT:
3611 case SYMBOL_REF:
3612 case CONST_DOUBLE:
3613 case CLOBBER:
3614 case CALL:
3615 return;
3616
3617 case MEM:
3618 /* If this is a constant-pool reference, see if it is a label. */
3619 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
3620 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
3621 mark_jump_label (get_pool_constant (XEXP (x, 0)), insn, cross_jump);
3622 break;
3623
3624 case LABEL_REF:
3625 {
3626 rtx label = XEXP (x, 0);
3627 rtx olabel = label;
3628 rtx note;
3629 rtx next;
3630
3631 if (GET_CODE (label) != CODE_LABEL)
3632 abort ();
3633
3634 /* Ignore references to labels of containing functions. */
3635 if (LABEL_REF_NONLOCAL_P (x))
3636 break;
3637
3638 /* If there are other labels following this one,
3639 replace it with the last of the consecutive labels. */
3640 for (next = NEXT_INSN (label); next; next = NEXT_INSN (next))
3641 {
3642 if (GET_CODE (next) == CODE_LABEL)
3643 label = next;
3644 else if (cross_jump && GET_CODE (next) == INSN
3645 && (GET_CODE (PATTERN (next)) == USE
3646 || GET_CODE (PATTERN (next)) == CLOBBER))
3647 continue;
3648 else if (GET_CODE (next) != NOTE)
3649 break;
3650 else if (! cross_jump
3651 && (NOTE_LINE_NUMBER (next) == NOTE_INSN_LOOP_BEG
3652 || NOTE_LINE_NUMBER (next) == NOTE_INSN_FUNCTION_END
3653 /* ??? Optional. Disables some optimizations, but
3654 makes gcov output more accurate with -O. */
3655 || (flag_test_coverage && NOTE_LINE_NUMBER (next) > 0)))
3656 break;
3657 }
3658
3659 XEXP (x, 0) = label;
3660 if (! insn || ! INSN_DELETED_P (insn))
3661 ++LABEL_NUSES (label);
3662
3663 if (insn)
3664 {
3665 if (GET_CODE (insn) == JUMP_INSN)
3666 JUMP_LABEL (insn) = label;
3667
3668 /* If we've changed OLABEL and we had a REG_LABEL note
3669 for it, update it as well. */
3670 else if (label != olabel
3671 && (note = find_reg_note (insn, REG_LABEL, olabel)) != 0)
3672 XEXP (note, 0) = label;
3673
3674 /* Otherwise, add a REG_LABEL note for LABEL unless there already
3675 is one. */
3676 else if (! find_reg_note (insn, REG_LABEL, label))
3677 {
3678 /* This code used to ignore labels which refered to dispatch
3679 tables to avoid flow.c generating worse code.
3680
3681 However, in the presense of global optimizations like
3682 gcse which call find_basic_blocks without calling
3683 life_analysis, not recording such labels will lead
3684 to compiler aborts because of inconsistencies in the
3685 flow graph. So we go ahead and record the label.
3686
3687 It may also be the case that the optimization argument
3688 is no longer valid because of the more accurate cfg
3689 we build in find_basic_blocks -- it no longer pessimizes
3690 code when it finds a REG_LABEL note. */
3691 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL, label,
3692 REG_NOTES (insn));
3693 }
3694 }
3695 return;
3696 }
3697
3698 /* Do walk the labels in a vector, but not the first operand of an
3699 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
3700 case ADDR_VEC:
3701 case ADDR_DIFF_VEC:
3702 if (! INSN_DELETED_P (insn))
3703 {
3704 int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
3705
3706 for (i = 0; i < XVECLEN (x, eltnum); i++)
3707 mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, cross_jump);
3708 }
3709 return;
3710
3711 default:
3712 break;
3713 }
3714
3715 fmt = GET_RTX_FORMAT (code);
3716 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3717 {
3718 if (fmt[i] == 'e')
3719 mark_jump_label (XEXP (x, i), insn, cross_jump);
3720 else if (fmt[i] == 'E')
3721 {
3722 register int j;
3723 for (j = 0; j < XVECLEN (x, i); j++)
3724 mark_jump_label (XVECEXP (x, i, j), insn, cross_jump);
3725 }
3726 }
3727 }
3728
3729 /* If all INSN does is set the pc, delete it,
3730 and delete the insn that set the condition codes for it
3731 if that's what the previous thing was. */
3732
3733 void
3734 delete_jump (insn)
3735 rtx insn;
3736 {
3737 register rtx set = single_set (insn);
3738
3739 if (set && GET_CODE (SET_DEST (set)) == PC)
3740 delete_computation (insn);
3741 }
3742
3743 /* Delete INSN and recursively delete insns that compute values used only
3744 by INSN. This uses the REG_DEAD notes computed during flow analysis.
3745 If we are running before flow.c, we need do nothing since flow.c will
3746 delete dead code. We also can't know if the registers being used are
3747 dead or not at this point.
3748
3749 Otherwise, look at all our REG_DEAD notes. If a previous insn does
3750 nothing other than set a register that dies in this insn, we can delete
3751 that insn as well.
3752
3753 On machines with CC0, if CC0 is used in this insn, we may be able to
3754 delete the insn that set it. */
3755
3756 static void
3757 delete_computation (insn)
3758 rtx insn;
3759 {
3760 rtx note, next;
3761
3762 #ifdef HAVE_cc0
3763 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
3764 {
3765 rtx prev = prev_nonnote_insn (insn);
3766 /* We assume that at this stage
3767 CC's are always set explicitly
3768 and always immediately before the jump that
3769 will use them. So if the previous insn
3770 exists to set the CC's, delete it
3771 (unless it performs auto-increments, etc.). */
3772 if (prev && GET_CODE (prev) == INSN
3773 && sets_cc0_p (PATTERN (prev)))
3774 {
3775 if (sets_cc0_p (PATTERN (prev)) > 0
3776 && !FIND_REG_INC_NOTE (prev, NULL_RTX))
3777 delete_computation (prev);
3778 else
3779 /* Otherwise, show that cc0 won't be used. */
3780 REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_UNUSED,
3781 cc0_rtx, REG_NOTES (prev));
3782 }
3783 }
3784 #endif
3785
3786 #ifdef INSN_SCHEDULING
3787 /* ?!? The schedulers do not keep REG_DEAD notes accurate after
3788 reload has completed. The schedulers need to be fixed. Until
3789 they are, we must not rely on the death notes here. */
3790 if (reload_completed && flag_schedule_insns_after_reload)
3791 {
3792 delete_insn (insn);
3793 return;
3794 }
3795 #endif
3796
3797 for (note = REG_NOTES (insn); note; note = next)
3798 {
3799 rtx our_prev;
3800
3801 next = XEXP (note, 1);
3802
3803 if (REG_NOTE_KIND (note) != REG_DEAD
3804 /* Verify that the REG_NOTE is legitimate. */
3805 || GET_CODE (XEXP (note, 0)) != REG)
3806 continue;
3807
3808 for (our_prev = prev_nonnote_insn (insn);
3809 our_prev && GET_CODE (our_prev) == INSN;
3810 our_prev = prev_nonnote_insn (our_prev))
3811 {
3812 /* If we reach a SEQUENCE, it is too complex to try to
3813 do anything with it, so give up. */
3814 if (GET_CODE (PATTERN (our_prev)) == SEQUENCE)
3815 break;
3816
3817 if (GET_CODE (PATTERN (our_prev)) == USE
3818 && GET_CODE (XEXP (PATTERN (our_prev), 0)) == INSN)
3819 /* reorg creates USEs that look like this. We leave them
3820 alone because reorg needs them for its own purposes. */
3821 break;
3822
3823 if (reg_set_p (XEXP (note, 0), PATTERN (our_prev)))
3824 {
3825 if (FIND_REG_INC_NOTE (our_prev, NULL_RTX))
3826 break;
3827
3828 if (GET_CODE (PATTERN (our_prev)) == PARALLEL)
3829 {
3830 /* If we find a SET of something else, we can't
3831 delete the insn. */
3832
3833 int i;
3834
3835 for (i = 0; i < XVECLEN (PATTERN (our_prev), 0); i++)
3836 {
3837 rtx part = XVECEXP (PATTERN (our_prev), 0, i);
3838
3839 if (GET_CODE (part) == SET
3840 && SET_DEST (part) != XEXP (note, 0))
3841 break;
3842 }
3843
3844 if (i == XVECLEN (PATTERN (our_prev), 0))
3845 delete_computation (our_prev);
3846 }
3847 else if (GET_CODE (PATTERN (our_prev)) == SET
3848 && SET_DEST (PATTERN (our_prev)) == XEXP (note, 0))
3849 delete_computation (our_prev);
3850
3851 break;
3852 }
3853
3854 /* If OUR_PREV references the register that dies here, it is an
3855 additional use. Hence any prior SET isn't dead. However, this
3856 insn becomes the new place for the REG_DEAD note. */
3857 if (reg_overlap_mentioned_p (XEXP (note, 0),
3858 PATTERN (our_prev)))
3859 {
3860 XEXP (note, 1) = REG_NOTES (our_prev);
3861 REG_NOTES (our_prev) = note;
3862 break;
3863 }
3864 }
3865 }
3866
3867 delete_insn (insn);
3868 }
3869 \f
3870 /* Delete insn INSN from the chain of insns and update label ref counts.
3871 May delete some following insns as a consequence; may even delete
3872 a label elsewhere and insns that follow it.
3873
3874 Returns the first insn after INSN that was not deleted. */
3875
3876 rtx
3877 delete_insn (insn)
3878 register rtx insn;
3879 {
3880 register rtx next = NEXT_INSN (insn);
3881 register rtx prev = PREV_INSN (insn);
3882 register int was_code_label = (GET_CODE (insn) == CODE_LABEL);
3883 register int dont_really_delete = 0;
3884
3885 while (next && INSN_DELETED_P (next))
3886 next = NEXT_INSN (next);
3887
3888 /* This insn is already deleted => return first following nondeleted. */
3889 if (INSN_DELETED_P (insn))
3890 return next;
3891
3892 /* Don't delete user-declared labels. Convert them to special NOTEs
3893 instead. */
3894 if (was_code_label && LABEL_NAME (insn) != 0
3895 && optimize && ! dont_really_delete)
3896 {
3897 PUT_CODE (insn, NOTE);
3898 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
3899 NOTE_SOURCE_FILE (insn) = 0;
3900 dont_really_delete = 1;
3901 }
3902 else
3903 /* Mark this insn as deleted. */
3904 INSN_DELETED_P (insn) = 1;
3905
3906 /* If this is an unconditional jump, delete it from the jump chain. */
3907 if (simplejump_p (insn))
3908 delete_from_jump_chain (insn);
3909
3910 /* If instruction is followed by a barrier,
3911 delete the barrier too. */
3912
3913 if (next != 0 && GET_CODE (next) == BARRIER)
3914 {
3915 INSN_DELETED_P (next) = 1;
3916 next = NEXT_INSN (next);
3917 }
3918
3919 /* Patch out INSN (and the barrier if any) */
3920
3921 if (optimize && ! dont_really_delete)
3922 {
3923 if (prev)
3924 {
3925 NEXT_INSN (prev) = next;
3926 if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
3927 NEXT_INSN (XVECEXP (PATTERN (prev), 0,
3928 XVECLEN (PATTERN (prev), 0) - 1)) = next;
3929 }
3930
3931 if (next)
3932 {
3933 PREV_INSN (next) = prev;
3934 if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
3935 PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
3936 }
3937
3938 if (prev && NEXT_INSN (prev) == 0)
3939 set_last_insn (prev);
3940 }
3941
3942 /* If deleting a jump, decrement the count of the label,
3943 and delete the label if it is now unused. */
3944
3945 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
3946 if (--LABEL_NUSES (JUMP_LABEL (insn)) == 0)
3947 {
3948 /* This can delete NEXT or PREV,
3949 either directly if NEXT is JUMP_LABEL (INSN),
3950 or indirectly through more levels of jumps. */
3951 delete_insn (JUMP_LABEL (insn));
3952 /* I feel a little doubtful about this loop,
3953 but I see no clean and sure alternative way
3954 to find the first insn after INSN that is not now deleted.
3955 I hope this works. */
3956 while (next && INSN_DELETED_P (next))
3957 next = NEXT_INSN (next);
3958 return next;
3959 }
3960
3961 /* Likewise if we're deleting a dispatch table. */
3962
3963 if (GET_CODE (insn) == JUMP_INSN
3964 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
3965 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
3966 {
3967 rtx pat = PATTERN (insn);
3968 int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
3969 int len = XVECLEN (pat, diff_vec_p);
3970
3971 for (i = 0; i < len; i++)
3972 if (--LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
3973 delete_insn (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
3974 while (next && INSN_DELETED_P (next))
3975 next = NEXT_INSN (next);
3976 return next;
3977 }
3978
3979 while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
3980 prev = PREV_INSN (prev);
3981
3982 /* If INSN was a label and a dispatch table follows it,
3983 delete the dispatch table. The tablejump must have gone already.
3984 It isn't useful to fall through into a table. */
3985
3986 if (was_code_label
3987 && NEXT_INSN (insn) != 0
3988 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
3989 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
3990 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
3991 next = delete_insn (NEXT_INSN (insn));
3992
3993 /* If INSN was a label, delete insns following it if now unreachable. */
3994
3995 if (was_code_label && prev && GET_CODE (prev) == BARRIER)
3996 {
3997 register RTX_CODE code;
3998 while (next != 0
3999 && (GET_RTX_CLASS (code = GET_CODE (next)) == 'i'
4000 || code == NOTE || code == BARRIER
4001 || (code == CODE_LABEL && INSN_DELETED_P (next))))
4002 {
4003 if (code == NOTE
4004 && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
4005 next = NEXT_INSN (next);
4006 /* Keep going past other deleted labels to delete what follows. */
4007 else if (code == CODE_LABEL && INSN_DELETED_P (next))
4008 next = NEXT_INSN (next);
4009 else
4010 /* Note: if this deletes a jump, it can cause more
4011 deletion of unreachable code, after a different label.
4012 As long as the value from this recursive call is correct,
4013 this invocation functions correctly. */
4014 next = delete_insn (next);
4015 }
4016 }
4017
4018 return next;
4019 }
4020
4021 /* Advance from INSN till reaching something not deleted
4022 then return that. May return INSN itself. */
4023
4024 rtx
4025 next_nondeleted_insn (insn)
4026 rtx insn;
4027 {
4028 while (INSN_DELETED_P (insn))
4029 insn = NEXT_INSN (insn);
4030 return insn;
4031 }
4032 \f
4033 /* Delete a range of insns from FROM to TO, inclusive.
4034 This is for the sake of peephole optimization, so assume
4035 that whatever these insns do will still be done by a new
4036 peephole insn that will replace them. */
4037
4038 void
4039 delete_for_peephole (from, to)
4040 register rtx from, to;
4041 {
4042 register rtx insn = from;
4043
4044 while (1)
4045 {
4046 register rtx next = NEXT_INSN (insn);
4047 register rtx prev = PREV_INSN (insn);
4048
4049 if (GET_CODE (insn) != NOTE)
4050 {
4051 INSN_DELETED_P (insn) = 1;
4052
4053 /* Patch this insn out of the chain. */
4054 /* We don't do this all at once, because we
4055 must preserve all NOTEs. */
4056 if (prev)
4057 NEXT_INSN (prev) = next;
4058
4059 if (next)
4060 PREV_INSN (next) = prev;
4061 }
4062
4063 if (insn == to)
4064 break;
4065 insn = next;
4066 }
4067
4068 /* Note that if TO is an unconditional jump
4069 we *do not* delete the BARRIER that follows,
4070 since the peephole that replaces this sequence
4071 is also an unconditional jump in that case. */
4072 }
4073 \f
4074 /* Invert the condition of the jump JUMP, and make it jump
4075 to label NLABEL instead of where it jumps now. */
4076
4077 int
4078 invert_jump (jump, nlabel)
4079 rtx jump, nlabel;
4080 {
4081 /* We have to either invert the condition and change the label or
4082 do neither. Either operation could fail. We first try to invert
4083 the jump. If that succeeds, we try changing the label. If that fails,
4084 we invert the jump back to what it was. */
4085
4086 if (! invert_exp (PATTERN (jump), jump))
4087 return 0;
4088
4089 if (redirect_jump (jump, nlabel))
4090 {
4091 if (flag_branch_probabilities)
4092 {
4093 rtx note = find_reg_note (jump, REG_BR_PROB, 0);
4094
4095 /* An inverted jump means that a probability taken becomes a
4096 probability not taken. Subtract the branch probability from the
4097 probability base to convert it back to a taken probability.
4098 (We don't flip the probability on a branch that's never taken. */
4099 if (note && XINT (XEXP (note, 0), 0) >= 0)
4100 XINT (XEXP (note, 0), 0) = REG_BR_PROB_BASE - XINT (XEXP (note, 0), 0);
4101 }
4102
4103 return 1;
4104 }
4105
4106 if (! invert_exp (PATTERN (jump), jump))
4107 /* This should just be putting it back the way it was. */
4108 abort ();
4109
4110 return 0;
4111 }
4112
4113 /* Invert the jump condition of rtx X contained in jump insn, INSN.
4114
4115 Return 1 if we can do so, 0 if we cannot find a way to do so that
4116 matches a pattern. */
4117
4118 int
4119 invert_exp (x, insn)
4120 rtx x;
4121 rtx insn;
4122 {
4123 register RTX_CODE code;
4124 register int i;
4125 register char *fmt;
4126
4127 code = GET_CODE (x);
4128
4129 if (code == IF_THEN_ELSE)
4130 {
4131 register rtx comp = XEXP (x, 0);
4132 register rtx tem;
4133
4134 /* We can do this in two ways: The preferable way, which can only
4135 be done if this is not an integer comparison, is to reverse
4136 the comparison code. Otherwise, swap the THEN-part and ELSE-part
4137 of the IF_THEN_ELSE. If we can't do either, fail. */
4138
4139 if (can_reverse_comparison_p (comp, insn)
4140 && validate_change (insn, &XEXP (x, 0),
4141 gen_rtx_fmt_ee (reverse_condition (GET_CODE (comp)),
4142 GET_MODE (comp), XEXP (comp, 0),
4143 XEXP (comp, 1)), 0))
4144 return 1;
4145
4146 tem = XEXP (x, 1);
4147 validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
4148 validate_change (insn, &XEXP (x, 2), tem, 1);
4149 return apply_change_group ();
4150 }
4151
4152 fmt = GET_RTX_FORMAT (code);
4153 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4154 {
4155 if (fmt[i] == 'e')
4156 if (! invert_exp (XEXP (x, i), insn))
4157 return 0;
4158 if (fmt[i] == 'E')
4159 {
4160 register int j;
4161 for (j = 0; j < XVECLEN (x, i); j++)
4162 if (!invert_exp (XVECEXP (x, i, j), insn))
4163 return 0;
4164 }
4165 }
4166
4167 return 1;
4168 }
4169 \f
4170 /* Make jump JUMP jump to label NLABEL instead of where it jumps now.
4171 If the old jump target label is unused as a result,
4172 it and the code following it may be deleted.
4173
4174 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
4175 RETURN insn.
4176
4177 The return value will be 1 if the change was made, 0 if it wasn't (this
4178 can only occur for NLABEL == 0). */
4179
4180 int
4181 redirect_jump (jump, nlabel)
4182 rtx jump, nlabel;
4183 {
4184 register rtx olabel = JUMP_LABEL (jump);
4185
4186 if (nlabel == olabel)
4187 return 1;
4188
4189 if (! redirect_exp (&PATTERN (jump), olabel, nlabel, jump))
4190 return 0;
4191
4192 /* If this is an unconditional branch, delete it from the jump_chain of
4193 OLABEL and add it to the jump_chain of NLABEL (assuming both labels
4194 have UID's in range and JUMP_CHAIN is valid). */
4195 if (jump_chain && (simplejump_p (jump)
4196 || GET_CODE (PATTERN (jump)) == RETURN))
4197 {
4198 int label_index = nlabel ? INSN_UID (nlabel) : 0;
4199
4200 delete_from_jump_chain (jump);
4201 if (label_index < max_jump_chain
4202 && INSN_UID (jump) < max_jump_chain)
4203 {
4204 jump_chain[INSN_UID (jump)] = jump_chain[label_index];
4205 jump_chain[label_index] = jump;
4206 }
4207 }
4208
4209 JUMP_LABEL (jump) = nlabel;
4210 if (nlabel)
4211 ++LABEL_NUSES (nlabel);
4212
4213 if (olabel && --LABEL_NUSES (olabel) == 0)
4214 delete_insn (olabel);
4215
4216 return 1;
4217 }
4218
4219 /* Delete the instruction JUMP from any jump chain it might be on. */
4220
4221 static void
4222 delete_from_jump_chain (jump)
4223 rtx jump;
4224 {
4225 int index;
4226 rtx olabel = JUMP_LABEL (jump);
4227
4228 /* Handle unconditional jumps. */
4229 if (jump_chain && olabel != 0
4230 && INSN_UID (olabel) < max_jump_chain
4231 && simplejump_p (jump))
4232 index = INSN_UID (olabel);
4233 /* Handle return insns. */
4234 else if (jump_chain && GET_CODE (PATTERN (jump)) == RETURN)
4235 index = 0;
4236 else return;
4237
4238 if (jump_chain[index] == jump)
4239 jump_chain[index] = jump_chain[INSN_UID (jump)];
4240 else
4241 {
4242 rtx insn;
4243
4244 for (insn = jump_chain[index];
4245 insn != 0;
4246 insn = jump_chain[INSN_UID (insn)])
4247 if (jump_chain[INSN_UID (insn)] == jump)
4248 {
4249 jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (jump)];
4250 break;
4251 }
4252 }
4253 }
4254
4255 /* If NLABEL is nonzero, throughout the rtx at LOC,
4256 alter (LABEL_REF OLABEL) to (LABEL_REF NLABEL). If OLABEL is
4257 zero, alter (RETURN) to (LABEL_REF NLABEL).
4258
4259 If NLABEL is zero, alter (LABEL_REF OLABEL) to (RETURN) and check
4260 validity with validate_change. Convert (set (pc) (label_ref olabel))
4261 to (return).
4262
4263 Return 0 if we found a change we would like to make but it is invalid.
4264 Otherwise, return 1. */
4265
4266 int
4267 redirect_exp (loc, olabel, nlabel, insn)
4268 rtx *loc;
4269 rtx olabel, nlabel;
4270 rtx insn;
4271 {
4272 register rtx x = *loc;
4273 register RTX_CODE code = GET_CODE (x);
4274 register int i;
4275 register char *fmt;
4276
4277 if (code == LABEL_REF)
4278 {
4279 if (XEXP (x, 0) == olabel)
4280 {
4281 if (nlabel)
4282 XEXP (x, 0) = nlabel;
4283 else
4284 return validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 0);
4285 return 1;
4286 }
4287 }
4288 else if (code == RETURN && olabel == 0)
4289 {
4290 x = gen_rtx_LABEL_REF (VOIDmode, nlabel);
4291 if (loc == &PATTERN (insn))
4292 x = gen_rtx_SET (VOIDmode, pc_rtx, x);
4293 return validate_change (insn, loc, x, 0);
4294 }
4295
4296 if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
4297 && GET_CODE (SET_SRC (x)) == LABEL_REF
4298 && XEXP (SET_SRC (x), 0) == olabel)
4299 return validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 0);
4300
4301 fmt = GET_RTX_FORMAT (code);
4302 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4303 {
4304 if (fmt[i] == 'e')
4305 if (! redirect_exp (&XEXP (x, i), olabel, nlabel, insn))
4306 return 0;
4307 if (fmt[i] == 'E')
4308 {
4309 register int j;
4310 for (j = 0; j < XVECLEN (x, i); j++)
4311 if (! redirect_exp (&XVECEXP (x, i, j), olabel, nlabel, insn))
4312 return 0;
4313 }
4314 }
4315
4316 return 1;
4317 }
4318 \f
4319 /* Make jump JUMP jump to label NLABEL, assuming it used to be a tablejump.
4320
4321 If the old jump target label (before the dispatch table) becomes unused,
4322 it and the dispatch table may be deleted. In that case, find the insn
4323 before the jump references that label and delete it and logical successors
4324 too. */
4325
4326 static void
4327 redirect_tablejump (jump, nlabel)
4328 rtx jump, nlabel;
4329 {
4330 register rtx olabel = JUMP_LABEL (jump);
4331
4332 /* Add this jump to the jump_chain of NLABEL. */
4333 if (jump_chain && INSN_UID (nlabel) < max_jump_chain
4334 && INSN_UID (jump) < max_jump_chain)
4335 {
4336 jump_chain[INSN_UID (jump)] = jump_chain[INSN_UID (nlabel)];
4337 jump_chain[INSN_UID (nlabel)] = jump;
4338 }
4339
4340 PATTERN (jump) = gen_jump (nlabel);
4341 JUMP_LABEL (jump) = nlabel;
4342 ++LABEL_NUSES (nlabel);
4343 INSN_CODE (jump) = -1;
4344
4345 if (--LABEL_NUSES (olabel) == 0)
4346 {
4347 delete_labelref_insn (jump, olabel, 0);
4348 delete_insn (olabel);
4349 }
4350 }
4351
4352 /* Find the insn referencing LABEL that is a logical predecessor of INSN.
4353 If we found one, delete it and then delete this insn if DELETE_THIS is
4354 non-zero. Return non-zero if INSN or a predecessor references LABEL. */
4355
4356 static int
4357 delete_labelref_insn (insn, label, delete_this)
4358 rtx insn, label;
4359 int delete_this;
4360 {
4361 int deleted = 0;
4362 rtx link;
4363
4364 if (GET_CODE (insn) != NOTE
4365 && reg_mentioned_p (label, PATTERN (insn)))
4366 {
4367 if (delete_this)
4368 {
4369 delete_insn (insn);
4370 deleted = 1;
4371 }
4372 else
4373 return 1;
4374 }
4375
4376 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
4377 if (delete_labelref_insn (XEXP (link, 0), label, 1))
4378 {
4379 if (delete_this)
4380 {
4381 delete_insn (insn);
4382 deleted = 1;
4383 }
4384 else
4385 return 1;
4386 }
4387
4388 return deleted;
4389 }
4390 \f
4391 /* Like rtx_equal_p except that it considers two REGs as equal
4392 if they renumber to the same value and considers two commutative
4393 operations to be the same if the order of the operands has been
4394 reversed.
4395
4396 ??? Addition is not commutative on the PA due to the weird implicit
4397 space register selection rules for memory addresses. Therefore, we
4398 don't consider a + b == b + a.
4399
4400 We could/should make this test a little tighter. Possibly only
4401 disabling it on the PA via some backend macro or only disabling this
4402 case when the PLUS is inside a MEM. */
4403
4404 int
4405 rtx_renumbered_equal_p (x, y)
4406 rtx x, y;
4407 {
4408 register int i;
4409 register RTX_CODE code = GET_CODE (x);
4410 register char *fmt;
4411
4412 if (x == y)
4413 return 1;
4414
4415 if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
4416 && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
4417 && GET_CODE (SUBREG_REG (y)) == REG)))
4418 {
4419 int reg_x = -1, reg_y = -1;
4420 int word_x = 0, word_y = 0;
4421
4422 if (GET_MODE (x) != GET_MODE (y))
4423 return 0;
4424
4425 /* If we haven't done any renumbering, don't
4426 make any assumptions. */
4427 if (reg_renumber == 0)
4428 return rtx_equal_p (x, y);
4429
4430 if (code == SUBREG)
4431 {
4432 reg_x = REGNO (SUBREG_REG (x));
4433 word_x = SUBREG_WORD (x);
4434
4435 if (reg_renumber[reg_x] >= 0)
4436 {
4437 reg_x = reg_renumber[reg_x] + word_x;
4438 word_x = 0;
4439 }
4440 }
4441
4442 else
4443 {
4444 reg_x = REGNO (x);
4445 if (reg_renumber[reg_x] >= 0)
4446 reg_x = reg_renumber[reg_x];
4447 }
4448
4449 if (GET_CODE (y) == SUBREG)
4450 {
4451 reg_y = REGNO (SUBREG_REG (y));
4452 word_y = SUBREG_WORD (y);
4453
4454 if (reg_renumber[reg_y] >= 0)
4455 {
4456 reg_y = reg_renumber[reg_y];
4457 word_y = 0;
4458 }
4459 }
4460
4461 else
4462 {
4463 reg_y = REGNO (y);
4464 if (reg_renumber[reg_y] >= 0)
4465 reg_y = reg_renumber[reg_y];
4466 }
4467
4468 return reg_x >= 0 && reg_x == reg_y && word_x == word_y;
4469 }
4470
4471 /* Now we have disposed of all the cases
4472 in which different rtx codes can match. */
4473 if (code != GET_CODE (y))
4474 return 0;
4475
4476 switch (code)
4477 {
4478 case PC:
4479 case CC0:
4480 case ADDR_VEC:
4481 case ADDR_DIFF_VEC:
4482 return 0;
4483
4484 case CONST_INT:
4485 return INTVAL (x) == INTVAL (y);
4486
4487 case LABEL_REF:
4488 /* We can't assume nonlocal labels have their following insns yet. */
4489 if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
4490 return XEXP (x, 0) == XEXP (y, 0);
4491
4492 /* Two label-refs are equivalent if they point at labels
4493 in the same position in the instruction stream. */
4494 return (next_real_insn (XEXP (x, 0))
4495 == next_real_insn (XEXP (y, 0)));
4496
4497 case SYMBOL_REF:
4498 return XSTR (x, 0) == XSTR (y, 0);
4499
4500 case CODE_LABEL:
4501 /* If we didn't match EQ equality above, they aren't the same. */
4502 return 0;
4503
4504 default:
4505 break;
4506 }
4507
4508 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
4509
4510 if (GET_MODE (x) != GET_MODE (y))
4511 return 0;
4512
4513 /* For commutative operations, the RTX match if the operand match in any
4514 order. Also handle the simple binary and unary cases without a loop.
4515
4516 ??? Don't consider PLUS a commutative operator; see comments above. */
4517 if ((code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
4518 && code != PLUS)
4519 return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
4520 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
4521 || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
4522 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
4523 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
4524 return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
4525 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
4526 else if (GET_RTX_CLASS (code) == '1')
4527 return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
4528
4529 /* Compare the elements. If any pair of corresponding elements
4530 fail to match, return 0 for the whole things. */
4531
4532 fmt = GET_RTX_FORMAT (code);
4533 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4534 {
4535 register int j;
4536 switch (fmt[i])
4537 {
4538 case 'w':
4539 if (XWINT (x, i) != XWINT (y, i))
4540 return 0;
4541 break;
4542
4543 case 'i':
4544 if (XINT (x, i) != XINT (y, i))
4545 return 0;
4546 break;
4547
4548 case 's':
4549 if (strcmp (XSTR (x, i), XSTR (y, i)))
4550 return 0;
4551 break;
4552
4553 case 'e':
4554 if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
4555 return 0;
4556 break;
4557
4558 case 'u':
4559 if (XEXP (x, i) != XEXP (y, i))
4560 return 0;
4561 /* fall through. */
4562 case '0':
4563 break;
4564
4565 case 'E':
4566 if (XVECLEN (x, i) != XVECLEN (y, i))
4567 return 0;
4568 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4569 if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
4570 return 0;
4571 break;
4572
4573 default:
4574 abort ();
4575 }
4576 }
4577 return 1;
4578 }
4579 \f
4580 /* If X is a hard register or equivalent to one or a subregister of one,
4581 return the hard register number. If X is a pseudo register that was not
4582 assigned a hard register, return the pseudo register number. Otherwise,
4583 return -1. Any rtx is valid for X. */
4584
4585 int
4586 true_regnum (x)
4587 rtx x;
4588 {
4589 if (GET_CODE (x) == REG)
4590 {
4591 if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
4592 return reg_renumber[REGNO (x)];
4593 return REGNO (x);
4594 }
4595 if (GET_CODE (x) == SUBREG)
4596 {
4597 int base = true_regnum (SUBREG_REG (x));
4598 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
4599 return SUBREG_WORD (x) + base;
4600 }
4601 return -1;
4602 }
4603 \f
4604 /* Optimize code of the form:
4605
4606 for (x = a[i]; x; ...)
4607 ...
4608 for (x = a[i]; x; ...)
4609 ...
4610 foo:
4611
4612 Loop optimize will change the above code into
4613
4614 if (x = a[i])
4615 for (;;)
4616 { ...; if (! (x = ...)) break; }
4617 if (x = a[i])
4618 for (;;)
4619 { ...; if (! (x = ...)) break; }
4620 foo:
4621
4622 In general, if the first test fails, the program can branch
4623 directly to `foo' and skip the second try which is doomed to fail.
4624 We run this after loop optimization and before flow analysis. */
4625
4626 /* When comparing the insn patterns, we track the fact that different
4627 pseudo-register numbers may have been used in each computation.
4628 The following array stores an equivalence -- same_regs[I] == J means
4629 that pseudo register I was used in the first set of tests in a context
4630 where J was used in the second set. We also count the number of such
4631 pending equivalences. If nonzero, the expressions really aren't the
4632 same. */
4633
4634 static int *same_regs;
4635
4636 static int num_same_regs;
4637
4638 /* Track any registers modified between the target of the first jump and
4639 the second jump. They never compare equal. */
4640
4641 static char *modified_regs;
4642
4643 /* Record if memory was modified. */
4644
4645 static int modified_mem;
4646
4647 /* Called via note_stores on each insn between the target of the first
4648 branch and the second branch. It marks any changed registers. */
4649
4650 static void
4651 mark_modified_reg (dest, x)
4652 rtx dest;
4653 rtx x ATTRIBUTE_UNUSED;
4654 {
4655 int regno, i;
4656
4657 if (GET_CODE (dest) == SUBREG)
4658 dest = SUBREG_REG (dest);
4659
4660 if (GET_CODE (dest) == MEM)
4661 modified_mem = 1;
4662
4663 if (GET_CODE (dest) != REG)
4664 return;
4665
4666 regno = REGNO (dest);
4667 if (regno >= FIRST_PSEUDO_REGISTER)
4668 modified_regs[regno] = 1;
4669 else
4670 for (i = 0; i < HARD_REGNO_NREGS (regno, GET_MODE (dest)); i++)
4671 modified_regs[regno + i] = 1;
4672 }
4673
4674 /* F is the first insn in the chain of insns. */
4675
4676 void
4677 thread_jumps (f, max_reg, flag_before_loop)
4678 rtx f;
4679 int max_reg;
4680 int flag_before_loop;
4681 {
4682 /* Basic algorithm is to find a conditional branch,
4683 the label it may branch to, and the branch after
4684 that label. If the two branches test the same condition,
4685 walk back from both branch paths until the insn patterns
4686 differ, or code labels are hit. If we make it back to
4687 the target of the first branch, then we know that the first branch
4688 will either always succeed or always fail depending on the relative
4689 senses of the two branches. So adjust the first branch accordingly
4690 in this case. */
4691
4692 rtx label, b1, b2, t1, t2;
4693 enum rtx_code code1, code2;
4694 rtx b1op0, b1op1, b2op0, b2op1;
4695 int changed = 1;
4696 int i;
4697 int *all_reset;
4698
4699 /* Allocate register tables and quick-reset table. */
4700 modified_regs = (char *) alloca (max_reg * sizeof (char));
4701 same_regs = (int *) alloca (max_reg * sizeof (int));
4702 all_reset = (int *) alloca (max_reg * sizeof (int));
4703 for (i = 0; i < max_reg; i++)
4704 all_reset[i] = -1;
4705
4706 while (changed)
4707 {
4708 changed = 0;
4709
4710 for (b1 = f; b1; b1 = NEXT_INSN (b1))
4711 {
4712 /* Get to a candidate branch insn. */
4713 if (GET_CODE (b1) != JUMP_INSN
4714 || ! condjump_p (b1) || simplejump_p (b1)
4715 || JUMP_LABEL (b1) == 0)
4716 continue;
4717
4718 bzero (modified_regs, max_reg * sizeof (char));
4719 modified_mem = 0;
4720
4721 bcopy ((char *) all_reset, (char *) same_regs,
4722 max_reg * sizeof (int));
4723 num_same_regs = 0;
4724
4725 label = JUMP_LABEL (b1);
4726
4727 /* Look for a branch after the target. Record any registers and
4728 memory modified between the target and the branch. Stop when we
4729 get to a label since we can't know what was changed there. */
4730 for (b2 = NEXT_INSN (label); b2; b2 = NEXT_INSN (b2))
4731 {
4732 if (GET_CODE (b2) == CODE_LABEL)
4733 break;
4734
4735 else if (GET_CODE (b2) == JUMP_INSN)
4736 {
4737 /* If this is an unconditional jump and is the only use of
4738 its target label, we can follow it. */
4739 if (simplejump_p (b2)
4740 && JUMP_LABEL (b2) != 0
4741 && LABEL_NUSES (JUMP_LABEL (b2)) == 1)
4742 {
4743 b2 = JUMP_LABEL (b2);
4744 continue;
4745 }
4746 else
4747 break;
4748 }
4749
4750 if (GET_CODE (b2) != CALL_INSN && GET_CODE (b2) != INSN)
4751 continue;
4752
4753 if (GET_CODE (b2) == CALL_INSN)
4754 {
4755 modified_mem = 1;
4756 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4757 if (call_used_regs[i] && ! fixed_regs[i]
4758 && i != STACK_POINTER_REGNUM
4759 && i != FRAME_POINTER_REGNUM
4760 && i != HARD_FRAME_POINTER_REGNUM
4761 && i != ARG_POINTER_REGNUM)
4762 modified_regs[i] = 1;
4763 }
4764
4765 note_stores (PATTERN (b2), mark_modified_reg);
4766 }
4767
4768 /* Check the next candidate branch insn from the label
4769 of the first. */
4770 if (b2 == 0
4771 || GET_CODE (b2) != JUMP_INSN
4772 || b2 == b1
4773 || ! condjump_p (b2)
4774 || simplejump_p (b2))
4775 continue;
4776
4777 /* Get the comparison codes and operands, reversing the
4778 codes if appropriate. If we don't have comparison codes,
4779 we can't do anything. */
4780 b1op0 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 0);
4781 b1op1 = XEXP (XEXP (SET_SRC (PATTERN (b1)), 0), 1);
4782 code1 = GET_CODE (XEXP (SET_SRC (PATTERN (b1)), 0));
4783 if (XEXP (SET_SRC (PATTERN (b1)), 1) == pc_rtx)
4784 code1 = reverse_condition (code1);
4785
4786 b2op0 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 0);
4787 b2op1 = XEXP (XEXP (SET_SRC (PATTERN (b2)), 0), 1);
4788 code2 = GET_CODE (XEXP (SET_SRC (PATTERN (b2)), 0));
4789 if (XEXP (SET_SRC (PATTERN (b2)), 1) == pc_rtx)
4790 code2 = reverse_condition (code2);
4791
4792 /* If they test the same things and knowing that B1 branches
4793 tells us whether or not B2 branches, check if we
4794 can thread the branch. */
4795 if (rtx_equal_for_thread_p (b1op0, b2op0, b2)
4796 && rtx_equal_for_thread_p (b1op1, b2op1, b2)
4797 && (comparison_dominates_p (code1, code2)
4798 || (comparison_dominates_p (code1, reverse_condition (code2))
4799 && can_reverse_comparison_p (XEXP (SET_SRC (PATTERN (b1)),
4800 0),
4801 b1))))
4802 {
4803 t1 = prev_nonnote_insn (b1);
4804 t2 = prev_nonnote_insn (b2);
4805
4806 while (t1 != 0 && t2 != 0)
4807 {
4808 if (t2 == label)
4809 {
4810 /* We have reached the target of the first branch.
4811 If there are no pending register equivalents,
4812 we know that this branch will either always
4813 succeed (if the senses of the two branches are
4814 the same) or always fail (if not). */
4815 rtx new_label;
4816
4817 if (num_same_regs != 0)
4818 break;
4819
4820 if (comparison_dominates_p (code1, code2))
4821 new_label = JUMP_LABEL (b2);
4822 else
4823 new_label = get_label_after (b2);
4824
4825 if (JUMP_LABEL (b1) != new_label)
4826 {
4827 rtx prev = PREV_INSN (new_label);
4828
4829 if (flag_before_loop
4830 && GET_CODE (prev) == NOTE
4831 && NOTE_LINE_NUMBER (prev) == NOTE_INSN_LOOP_BEG)
4832 {
4833 /* Don't thread to the loop label. If a loop
4834 label is reused, loop optimization will
4835 be disabled for that loop. */
4836 new_label = gen_label_rtx ();
4837 emit_label_after (new_label, PREV_INSN (prev));
4838 }
4839 changed |= redirect_jump (b1, new_label);
4840 }
4841 break;
4842 }
4843
4844 /* If either of these is not a normal insn (it might be
4845 a JUMP_INSN, CALL_INSN, or CODE_LABEL) we fail. (NOTEs
4846 have already been skipped above.) Similarly, fail
4847 if the insns are different. */
4848 if (GET_CODE (t1) != INSN || GET_CODE (t2) != INSN
4849 || recog_memoized (t1) != recog_memoized (t2)
4850 || ! rtx_equal_for_thread_p (PATTERN (t1),
4851 PATTERN (t2), t2))
4852 break;
4853
4854 t1 = prev_nonnote_insn (t1);
4855 t2 = prev_nonnote_insn (t2);
4856 }
4857 }
4858 }
4859 }
4860 }
4861 \f
4862 /* This is like RTX_EQUAL_P except that it knows about our handling of
4863 possibly equivalent registers and knows to consider volatile and
4864 modified objects as not equal.
4865
4866 YINSN is the insn containing Y. */
4867
4868 int
4869 rtx_equal_for_thread_p (x, y, yinsn)
4870 rtx x, y;
4871 rtx yinsn;
4872 {
4873 register int i;
4874 register int j;
4875 register enum rtx_code code;
4876 register char *fmt;
4877
4878 code = GET_CODE (x);
4879 /* Rtx's of different codes cannot be equal. */
4880 if (code != GET_CODE (y))
4881 return 0;
4882
4883 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
4884 (REG:SI x) and (REG:HI x) are NOT equivalent. */
4885
4886 if (GET_MODE (x) != GET_MODE (y))
4887 return 0;
4888
4889 /* For floating-point, consider everything unequal. This is a bit
4890 pessimistic, but this pass would only rarely do anything for FP
4891 anyway. */
4892 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
4893 && FLOAT_MODE_P (GET_MODE (x)) && ! flag_fast_math)
4894 return 0;
4895
4896 /* For commutative operations, the RTX match if the operand match in any
4897 order. Also handle the simple binary and unary cases without a loop. */
4898 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
4899 return ((rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4900 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn))
4901 || (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 1), yinsn)
4902 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 0), yinsn)));
4903 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
4904 return (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4905 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn));
4906 else if (GET_RTX_CLASS (code) == '1')
4907 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4908
4909 /* Handle special-cases first. */
4910 switch (code)
4911 {
4912 case REG:
4913 if (REGNO (x) == REGNO (y) && ! modified_regs[REGNO (x)])
4914 return 1;
4915
4916 /* If neither is user variable or hard register, check for possible
4917 equivalence. */
4918 if (REG_USERVAR_P (x) || REG_USERVAR_P (y)
4919 || REGNO (x) < FIRST_PSEUDO_REGISTER
4920 || REGNO (y) < FIRST_PSEUDO_REGISTER)
4921 return 0;
4922
4923 if (same_regs[REGNO (x)] == -1)
4924 {
4925 same_regs[REGNO (x)] = REGNO (y);
4926 num_same_regs++;
4927
4928 /* If this is the first time we are seeing a register on the `Y'
4929 side, see if it is the last use. If not, we can't thread the
4930 jump, so mark it as not equivalent. */
4931 if (REGNO_LAST_UID (REGNO (y)) != INSN_UID (yinsn))
4932 return 0;
4933
4934 return 1;
4935 }
4936 else
4937 return (same_regs[REGNO (x)] == REGNO (y));
4938
4939 break;
4940
4941 case MEM:
4942 /* If memory modified or either volatile, not equivalent.
4943 Else, check address. */
4944 if (modified_mem || MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4945 return 0;
4946
4947 return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4948
4949 case ASM_INPUT:
4950 if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4951 return 0;
4952
4953 break;
4954
4955 case SET:
4956 /* Cancel a pending `same_regs' if setting equivalenced registers.
4957 Then process source. */
4958 if (GET_CODE (SET_DEST (x)) == REG
4959 && GET_CODE (SET_DEST (y)) == REG)
4960 {
4961 if (same_regs[REGNO (SET_DEST (x))] == REGNO (SET_DEST (y)))
4962 {
4963 same_regs[REGNO (SET_DEST (x))] = -1;
4964 num_same_regs--;
4965 }
4966 else if (REGNO (SET_DEST (x)) != REGNO (SET_DEST (y)))
4967 return 0;
4968 }
4969 else
4970 if (rtx_equal_for_thread_p (SET_DEST (x), SET_DEST (y), yinsn) == 0)
4971 return 0;
4972
4973 return rtx_equal_for_thread_p (SET_SRC (x), SET_SRC (y), yinsn);
4974
4975 case LABEL_REF:
4976 return XEXP (x, 0) == XEXP (y, 0);
4977
4978 case SYMBOL_REF:
4979 return XSTR (x, 0) == XSTR (y, 0);
4980
4981 default:
4982 break;
4983 }
4984
4985 if (x == y)
4986 return 1;
4987
4988 fmt = GET_RTX_FORMAT (code);
4989 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4990 {
4991 switch (fmt[i])
4992 {
4993 case 'w':
4994 if (XWINT (x, i) != XWINT (y, i))
4995 return 0;
4996 break;
4997
4998 case 'n':
4999 case 'i':
5000 if (XINT (x, i) != XINT (y, i))
5001 return 0;
5002 break;
5003
5004 case 'V':
5005 case 'E':
5006 /* Two vectors must have the same length. */
5007 if (XVECLEN (x, i) != XVECLEN (y, i))
5008 return 0;
5009
5010 /* And the corresponding elements must match. */
5011 for (j = 0; j < XVECLEN (x, i); j++)
5012 if (rtx_equal_for_thread_p (XVECEXP (x, i, j),
5013 XVECEXP (y, i, j), yinsn) == 0)
5014 return 0;
5015 break;
5016
5017 case 'e':
5018 if (rtx_equal_for_thread_p (XEXP (x, i), XEXP (y, i), yinsn) == 0)
5019 return 0;
5020 break;
5021
5022 case 'S':
5023 case 's':
5024 if (strcmp (XSTR (x, i), XSTR (y, i)))
5025 return 0;
5026 break;
5027
5028 case 'u':
5029 /* These are just backpointers, so they don't matter. */
5030 break;
5031
5032 case '0':
5033 break;
5034
5035 /* It is believed that rtx's at this level will never
5036 contain anything but integers and other rtx's,
5037 except for within LABEL_REFs and SYMBOL_REFs. */
5038 default:
5039 abort ();
5040 }
5041 }
5042 return 1;
5043 }
5044 \f
5045
5046 #ifndef HAVE_cc0
5047 /* Return the insn that NEW can be safely inserted in front of starting at
5048 the jump insn INSN. Return 0 if it is not safe to do this jump
5049 optimization. Note that NEW must contain a single set. */
5050
5051 static rtx
5052 find_insert_position (insn, new)
5053 rtx insn;
5054 rtx new;
5055 {
5056 int i;
5057 rtx prev;
5058
5059 /* If NEW does not clobber, it is safe to insert NEW before INSN. */
5060 if (GET_CODE (PATTERN (new)) != PARALLEL)
5061 return insn;
5062
5063 for (i = XVECLEN (PATTERN (new), 0) - 1; i >= 0; i--)
5064 if (GET_CODE (XVECEXP (PATTERN (new), 0, i)) == CLOBBER
5065 && reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (new), 0, i), 0),
5066 insn))
5067 break;
5068
5069 if (i < 0)
5070 return insn;
5071
5072 /* There is a good chance that the previous insn PREV sets the thing
5073 being clobbered (often the CC in a hard reg). If PREV does not
5074 use what NEW sets, we can insert NEW before PREV. */
5075
5076 prev = prev_active_insn (insn);
5077 for (i = XVECLEN (PATTERN (new), 0) - 1; i >= 0; i--)
5078 if (GET_CODE (XVECEXP (PATTERN (new), 0, i)) == CLOBBER
5079 && reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (new), 0, i), 0),
5080 insn)
5081 && ! modified_in_p (XEXP (XVECEXP (PATTERN (new), 0, i), 0),
5082 prev))
5083 return 0;
5084
5085 return reg_mentioned_p (SET_DEST (single_set (new)), prev) ? 0 : prev;
5086 }
5087 #endif /* !HAVE_cc0 */