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