jump.c (jump_optimize_1): Use reversed_comparison_code instead of can_reverse_compari...
[platform/upstream/gcc.git] / gcc / jump.c
1 /* Optimize jump instructions, for GNU compiler.
2    Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3    1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
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 "tm_p.h"
58 #include "flags.h"
59 #include "hard-reg-set.h"
60 #include "regs.h"
61 #include "insn-config.h"
62 #include "insn-flags.h"
63 #include "insn-attr.h"
64 #include "recog.h"
65 #include "function.h"
66 #include "expr.h"
67 #include "real.h"
68 #include "except.h"
69 #include "toplev.h"
70
71 /* ??? Eventually must record somehow the labels used by jumps
72    from nested functions.  */
73 /* Pre-record the next or previous real insn for each label?
74    No, this pass is very fast anyway.  */
75 /* Condense consecutive labels?
76    This would make life analysis faster, maybe.  */
77 /* Optimize jump y; x: ... y: jumpif... x?
78    Don't know if it is worth bothering with.  */
79 /* Optimize two cases of conditional jump to conditional jump?
80    This can never delete any instruction or make anything dead,
81    or even change what is live at any point.
82    So perhaps let combiner do it.  */
83
84 /* Vector indexed by uid.
85    For each CODE_LABEL, index by its uid to get first unconditional jump
86    that jumps to the label.
87    For each JUMP_INSN, index by its uid to get the next unconditional jump
88    that jumps to the same label.
89    Element 0 is the start of a chain of all return insns.
90    (It is safe to use element 0 because insn uid 0 is not used.  */
91
92 static rtx *jump_chain;
93
94 /* Maximum index in jump_chain.  */
95
96 static int max_jump_chain;
97
98 /* Indicates whether death notes are significant in cross jump analysis.
99    Normally they are not significant, because of A and B jump to C,
100    and R dies in A, it must die in B.  But this might not be true after
101    stack register conversion, and we must compare death notes in that
102    case.  */
103
104 static int cross_jump_death_matters = 0;
105
106 static int init_label_info              PARAMS ((rtx));
107 static void delete_barrier_successors   PARAMS ((rtx));
108 static void mark_all_labels             PARAMS ((rtx, int));
109 static rtx delete_unreferenced_labels   PARAMS ((rtx));
110 static void delete_noop_moves           PARAMS ((rtx));
111 static int duplicate_loop_exit_test     PARAMS ((rtx));
112 static void find_cross_jump             PARAMS ((rtx, rtx, int, rtx *, rtx *));
113 static void do_cross_jump               PARAMS ((rtx, rtx, rtx));
114 static int jump_back_p                  PARAMS ((rtx, rtx));
115 static int tension_vector_labels        PARAMS ((rtx, int));
116 static void delete_computation          PARAMS ((rtx));
117 static void redirect_exp_1              PARAMS ((rtx *, rtx, rtx, rtx));
118 static int redirect_exp                 PARAMS ((rtx, rtx, rtx));
119 static void invert_exp_1                PARAMS ((rtx));
120 static int invert_exp                   PARAMS ((rtx));
121 static void delete_from_jump_chain      PARAMS ((rtx));
122 static int delete_labelref_insn         PARAMS ((rtx, rtx, int));
123 static void mark_modified_reg           PARAMS ((rtx, rtx, void *));
124 static void redirect_tablejump          PARAMS ((rtx, rtx));
125 static void jump_optimize_1             PARAMS ((rtx, int, int, int, int, int));
126 static int returnjump_p_1               PARAMS ((rtx *, void *));
127 static void delete_prior_computation    PARAMS ((rtx, rtx));
128 \f
129 /* Main external entry point into the jump optimizer.  See comments before
130    jump_optimize_1 for descriptions of the arguments.  */
131 void
132 jump_optimize (f, cross_jump, noop_moves, after_regscan)
133      rtx f;
134      int cross_jump;
135      int noop_moves;
136      int after_regscan;
137 {
138   jump_optimize_1 (f, cross_jump, noop_moves, after_regscan, 0, 0);
139 }
140
141 /* Alternate entry into the jump optimizer.  This entry point only rebuilds
142    the JUMP_LABEL field in jumping insns and REG_LABEL notes in non-jumping
143    instructions.  */
144 void
145 rebuild_jump_labels (f)
146      rtx f;
147 {
148   jump_optimize_1 (f, 0, 0, 0, 1, 0);
149 }
150
151 /* Alternate entry into the jump optimizer.  Do only trivial optimizations.  */
152
153 void
154 jump_optimize_minimal (f)
155      rtx f;
156 {
157   jump_optimize_1 (f, 0, 0, 0, 0, 1);
158 }
159 \f
160 /* Delete no-op jumps and optimize jumps to jumps
161    and jumps around jumps.
162    Delete unused labels and unreachable code.
163
164    If CROSS_JUMP is 1, detect matching code
165    before a jump and its destination and unify them.
166    If CROSS_JUMP is 2, do cross-jumping, but pay attention to death notes.
167
168    If NOOP_MOVES is nonzero, delete no-op move insns.
169
170    If AFTER_REGSCAN is nonzero, then this jump pass is being run immediately
171    after regscan, and it is safe to use regno_first_uid and regno_last_uid.
172
173    If MARK_LABELS_ONLY is nonzero, then we only rebuild the jump chain
174    and JUMP_LABEL field for jumping insns.
175
176    If `optimize' is zero, don't change any code,
177    just determine whether control drops off the end of the function.
178    This case occurs when we have -W and not -O.
179    It works because `delete_insn' checks the value of `optimize'
180    and refrains from actually deleting when that is 0.
181
182    If MINIMAL is nonzero, then we only perform trivial optimizations:
183
184      * Removal of unreachable code after BARRIERs.
185      * Removal of unreferenced CODE_LABELs.
186      * Removal of a jump to the next instruction.
187      * Removal of a conditional jump followed by an unconditional jump
188        to the same target as the conditional jump.
189      * Simplify a conditional jump around an unconditional jump.
190      * Simplify a jump to a jump.
191      * Delete extraneous line number notes.
192   */
193
194 static void
195 jump_optimize_1 (f, cross_jump, noop_moves, after_regscan,
196                  mark_labels_only, minimal)
197      rtx f;
198      int cross_jump;
199      int noop_moves;
200      int after_regscan;
201      int mark_labels_only;
202      int minimal;
203 {
204   register rtx insn, next;
205   int changed;
206   int old_max_reg;
207   int first = 1;
208   int max_uid = 0;
209   rtx last_insn;
210   enum rtx_code reversed_code;
211
212   cross_jump_death_matters = (cross_jump == 2);
213   max_uid = init_label_info (f) + 1;
214
215   /* If we are performing cross jump optimizations, then initialize
216      tables mapping UIDs to EH regions to avoid incorrect movement
217      of insns from one EH region to another.  */
218   if (flag_exceptions && cross_jump)
219     init_insn_eh_region (f, max_uid);
220
221   if (! mark_labels_only)
222     delete_barrier_successors (f);
223
224   /* Leave some extra room for labels and duplicate exit test insns
225      we make.  */
226   max_jump_chain = max_uid * 14 / 10;
227   jump_chain = (rtx *) xcalloc (max_jump_chain, sizeof (rtx));
228
229   mark_all_labels (f, cross_jump);
230
231   /* Keep track of labels used from static data; we don't track them
232      closely enough to delete them here, so make sure their reference
233      count doesn't drop to zero.  */
234
235   for (insn = forced_labels; insn; insn = XEXP (insn, 1))
236     if (GET_CODE (XEXP (insn, 0)) == CODE_LABEL)
237       LABEL_NUSES (XEXP (insn, 0))++;
238
239   check_exception_handler_labels ();
240
241   /* Keep track of labels used for marking handlers for exception
242      regions; they cannot usually be deleted.  */
243
244   for (insn = exception_handler_labels; insn; insn = XEXP (insn, 1))
245     if (GET_CODE (XEXP (insn, 0)) == CODE_LABEL)
246       LABEL_NUSES (XEXP (insn, 0))++;
247
248   /* Quit now if we just wanted to rebuild the JUMP_LABEL and REG_LABEL
249      notes and recompute LABEL_NUSES.  */
250   if (mark_labels_only)
251     goto end;
252
253   if (! minimal)
254     exception_optimize ();
255
256   last_insn = delete_unreferenced_labels (f);
257
258   if (noop_moves)
259     delete_noop_moves (f);
260
261   /* If we haven't yet gotten to reload and we have just run regscan,
262      delete any insn that sets a register that isn't used elsewhere.
263      This helps some of the optimizations below by having less insns
264      being jumped around.  */
265
266   if (optimize && ! reload_completed && after_regscan)
267     for (insn = f; insn; insn = next)
268       {
269         rtx set = single_set (insn);
270
271         next = NEXT_INSN (insn);
272
273         if (set && GET_CODE (SET_DEST (set)) == REG
274             && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
275             && REGNO_FIRST_UID (REGNO (SET_DEST (set))) == INSN_UID (insn)
276             /* We use regno_last_note_uid so as not to delete the setting
277                of a reg that's used in notes.  A subsequent optimization
278                might arrange to use that reg for real.  */
279             && REGNO_LAST_NOTE_UID (REGNO (SET_DEST (set))) == INSN_UID (insn)
280             && ! side_effects_p (SET_SRC (set))
281             && ! find_reg_note (insn, REG_RETVAL, 0)
282             /* An ADDRESSOF expression can turn into a use of the internal arg
283                pointer, so do not delete the initialization of the internal
284                arg pointer yet.  If it is truly dead, flow will delete the
285                initializing insn.  */
286             && SET_DEST (set) != current_function_internal_arg_pointer)
287           delete_insn (insn);
288       }
289
290   /* Now iterate optimizing jumps until nothing changes over one pass.  */
291   changed = 1;
292   old_max_reg = max_reg_num ();
293   while (changed)
294     {
295       changed = 0;
296
297       for (insn = f; insn; insn = next)
298         {
299           rtx reallabelprev;
300           rtx temp, temp1, temp2 = NULL_RTX;
301           rtx temp4 ATTRIBUTE_UNUSED;
302           rtx nlabel;
303           int this_is_any_uncondjump;
304           int this_is_any_condjump;
305           int this_is_onlyjump;
306
307           next = NEXT_INSN (insn);
308
309           /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
310              jump.  Try to optimize by duplicating the loop exit test if so.
311              This is only safe immediately after regscan, because it uses
312              the values of regno_first_uid and regno_last_uid.  */
313           if (after_regscan && GET_CODE (insn) == NOTE
314               && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
315               && (temp1 = next_nonnote_insn (insn)) != 0
316               && any_uncondjump_p (temp1)
317               && onlyjump_p (temp1))
318             {
319               temp = PREV_INSN (insn);
320               if (duplicate_loop_exit_test (insn))
321                 {
322                   changed = 1;
323                   next = NEXT_INSN (temp);
324                   continue;
325                 }
326             }
327
328           if (GET_CODE (insn) != JUMP_INSN)
329             continue;
330
331           this_is_any_condjump = any_condjump_p (insn);
332           this_is_any_uncondjump = any_uncondjump_p (insn);
333           this_is_onlyjump = onlyjump_p (insn);
334
335           /* Tension the labels in dispatch tables.  */
336
337           if (GET_CODE (PATTERN (insn)) == ADDR_VEC)
338             changed |= tension_vector_labels (PATTERN (insn), 0);
339           if (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
340             changed |= tension_vector_labels (PATTERN (insn), 1);
341
342           /* See if this jump goes to another jump and redirect if so.  */
343           nlabel = follow_jumps (JUMP_LABEL (insn));
344           if (nlabel != JUMP_LABEL (insn))
345             changed |= redirect_jump (insn, nlabel, 1);
346
347           if (! optimize || minimal)
348             continue;
349
350           /* If a dispatch table always goes to the same place,
351              get rid of it and replace the insn that uses it.  */
352
353           if (GET_CODE (PATTERN (insn)) == ADDR_VEC
354               || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
355             {
356               int i;
357               rtx pat = PATTERN (insn);
358               int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
359               int len = XVECLEN (pat, diff_vec_p);
360               rtx dispatch = prev_real_insn (insn);
361               rtx set;
362
363               for (i = 0; i < len; i++)
364                 if (XEXP (XVECEXP (pat, diff_vec_p, i), 0)
365                     != XEXP (XVECEXP (pat, diff_vec_p, 0), 0))
366                   break;
367
368               if (i == len
369                   && dispatch != 0
370                   && GET_CODE (dispatch) == JUMP_INSN
371                   && JUMP_LABEL (dispatch) != 0
372                   /* Don't mess with a casesi insn.
373                      XXX according to the comment before computed_jump_p(),
374                      all casesi insns should be a parallel of the jump
375                      and a USE of a LABEL_REF.  */
376                   && ! ((set = single_set (dispatch)) != NULL
377                         && (GET_CODE (SET_SRC (set)) == IF_THEN_ELSE))
378                   && next_real_insn (JUMP_LABEL (dispatch)) == insn)
379                 {
380                   redirect_tablejump (dispatch,
381                                       XEXP (XVECEXP (pat, diff_vec_p, 0), 0));
382                   changed = 1;
383                 }
384             }
385
386           reallabelprev = prev_active_insn (JUMP_LABEL (insn));
387
388           /* Detect jump to following insn.  */
389           if (reallabelprev == insn
390               && (this_is_any_condjump || this_is_any_uncondjump)
391               && this_is_onlyjump)
392             {
393               next = next_real_insn (JUMP_LABEL (insn));
394               delete_jump (insn);
395
396               /* Remove the "inactive" but "real" insns (i.e. uses and
397                  clobbers) in between here and there.  */
398               temp = insn;
399               while ((temp = next_real_insn (temp)) != next)
400                 delete_insn (temp);
401
402               changed = 1;
403               continue;
404             }
405
406           /* Detect a conditional jump going to the same place
407              as an immediately following unconditional jump.  */
408           else if (this_is_any_condjump && this_is_onlyjump
409                    && (temp = next_active_insn (insn)) != 0
410                    && simplejump_p (temp)
411                    && (next_active_insn (JUMP_LABEL (insn))
412                        == next_active_insn (JUMP_LABEL (temp))))
413             {
414               /* Don't mess up test coverage analysis.  */
415               temp2 = temp;
416               if (flag_test_coverage && !reload_completed)
417                 for (temp2 = insn; temp2 != temp; temp2 = NEXT_INSN (temp2))
418                   if (GET_CODE (temp2) == NOTE && NOTE_LINE_NUMBER (temp2) > 0)
419                     break;
420
421               if (temp2 == temp)
422                 {
423                   delete_jump (insn);
424                   changed = 1;
425                   continue;
426                 }
427             }
428
429           /* Detect a conditional jump jumping over an unconditional jump.  */
430
431           else if (this_is_any_condjump
432                    && reallabelprev != 0
433                    && GET_CODE (reallabelprev) == JUMP_INSN
434                    && prev_active_insn (reallabelprev) == insn
435                    && no_labels_between_p (insn, reallabelprev)
436                    && any_uncondjump_p (reallabelprev)
437                    && onlyjump_p (reallabelprev))
438             {
439               /* When we invert the unconditional jump, we will be
440                  decrementing the usage count of its old label.
441                  Make sure that we don't delete it now because that
442                  might cause the following code to be deleted.  */
443               rtx prev_uses = prev_nonnote_insn (reallabelprev);
444               rtx prev_label = JUMP_LABEL (insn);
445
446               if (prev_label)
447                 ++LABEL_NUSES (prev_label);
448
449               if (invert_jump (insn, JUMP_LABEL (reallabelprev), 1))
450                 {
451                   /* It is very likely that if there are USE insns before
452                      this jump, they hold REG_DEAD notes.  These REG_DEAD
453                      notes are no longer valid due to this optimization,
454                      and will cause the life-analysis that following passes
455                      (notably delayed-branch scheduling) to think that
456                      these registers are dead when they are not.
457
458                      To prevent this trouble, we just remove the USE insns
459                      from the insn chain.  */
460
461                   while (prev_uses && GET_CODE (prev_uses) == INSN
462                          && GET_CODE (PATTERN (prev_uses)) == USE)
463                     {
464                       rtx useless = prev_uses;
465                       prev_uses = prev_nonnote_insn (prev_uses);
466                       delete_insn (useless);
467                     }
468
469                   delete_insn (reallabelprev);
470                   changed = 1;
471                 }
472
473               /* We can now safely delete the label if it is unreferenced
474                  since the delete_insn above has deleted the BARRIER.  */
475               if (prev_label && --LABEL_NUSES (prev_label) == 0)
476                 delete_insn (prev_label);
477
478               next = NEXT_INSN (insn);
479             }
480
481           /* If we have an unconditional jump preceded by a USE, try to put
482              the USE before the target and jump there.  This simplifies many
483              of the optimizations below since we don't have to worry about
484              dealing with these USE insns.  We only do this if the label
485              being branch to already has the identical USE or if code
486              never falls through to that label.  */
487
488           else if (this_is_any_uncondjump
489                    && (temp = prev_nonnote_insn (insn)) != 0
490                    && GET_CODE (temp) == INSN
491                    && GET_CODE (PATTERN (temp)) == USE
492                    && (temp1 = prev_nonnote_insn (JUMP_LABEL (insn))) != 0
493                    && (GET_CODE (temp1) == BARRIER
494                        || (GET_CODE (temp1) == INSN
495                            && rtx_equal_p (PATTERN (temp), PATTERN (temp1))))
496                    /* Don't do this optimization if we have a loop containing
497                       only the USE instruction, and the loop start label has
498                       a usage count of 1.  This is because we will redo this
499                       optimization everytime through the outer loop, and jump
500                       opt will never exit.  */
501                    && ! ((temp2 = prev_nonnote_insn (temp)) != 0
502                          && temp2 == JUMP_LABEL (insn)
503                          && LABEL_NUSES (temp2) == 1))
504             {
505               if (GET_CODE (temp1) == BARRIER)
506                 {
507                   emit_insn_after (PATTERN (temp), temp1);
508                   temp1 = NEXT_INSN (temp1);
509                 }
510
511               delete_insn (temp);
512               redirect_jump (insn, get_label_before (temp1), 1);
513               reallabelprev = prev_real_insn (temp1);
514               changed = 1;
515               next = NEXT_INSN (insn);
516             }
517
518 #ifdef HAVE_trap
519           /* Detect a conditional jump jumping over an unconditional trap.  */
520           if (HAVE_trap
521               && this_is_any_condjump && this_is_onlyjump
522               && reallabelprev != 0
523               && GET_CODE (reallabelprev) == INSN
524               && GET_CODE (PATTERN (reallabelprev)) == TRAP_IF
525               && TRAP_CONDITION (PATTERN (reallabelprev)) == const_true_rtx
526               && prev_active_insn (reallabelprev) == insn
527               && no_labels_between_p (insn, reallabelprev)
528               && (temp2 = get_condition (insn, &temp4))
529               && ((reversed_code = reversed_comparison_code (temp2, insn))
530                   != UNKNOWN))
531             {
532               rtx new = gen_cond_trap (reversed_code,
533                                        XEXP (temp2, 0), XEXP (temp2, 1),
534                                        TRAP_CODE (PATTERN (reallabelprev)));
535
536               if (new)
537                 {
538                   emit_insn_before (new, temp4);
539                   delete_insn (reallabelprev);
540                   delete_jump (insn);
541                   changed = 1;
542                   continue;
543                 }
544             }
545           /* Detect a jump jumping to an unconditional trap.  */
546           else if (HAVE_trap && this_is_onlyjump
547                    && (temp = next_active_insn (JUMP_LABEL (insn)))
548                    && GET_CODE (temp) == INSN
549                    && GET_CODE (PATTERN (temp)) == TRAP_IF
550                    && (this_is_any_uncondjump
551                        || (this_is_any_condjump
552                            && (temp2 = get_condition (insn, &temp4)))))
553             {
554               rtx tc = TRAP_CONDITION (PATTERN (temp));
555
556               if (tc == const_true_rtx
557                   || (! this_is_any_uncondjump && rtx_equal_p (temp2, tc)))
558                 {
559                   rtx new;
560                   /* Replace an unconditional jump to a trap with a trap.  */
561                   if (this_is_any_uncondjump)
562                     {
563                       emit_barrier_after (emit_insn_before (gen_trap (), insn));
564                       delete_jump (insn);
565                       changed = 1;
566                       continue;
567                     }
568                   new = gen_cond_trap (GET_CODE (temp2), XEXP (temp2, 0),
569                                        XEXP (temp2, 1),
570                                        TRAP_CODE (PATTERN (temp)));
571                   if (new)
572                     {
573                       emit_insn_before (new, temp4);
574                       delete_jump (insn);
575                       changed = 1;
576                       continue;
577                     }
578                 }
579               /* If the trap condition and jump condition are mutually
580                  exclusive, redirect the jump to the following insn.  */
581               else if (GET_RTX_CLASS (GET_CODE (tc)) == '<'
582                        && this_is_any_condjump
583                        && swap_condition (GET_CODE (temp2)) == GET_CODE (tc)
584                        && rtx_equal_p (XEXP (tc, 0), XEXP (temp2, 0))
585                        && rtx_equal_p (XEXP (tc, 1), XEXP (temp2, 1))
586                        && redirect_jump (insn, get_label_after (temp), 1))
587                 {
588                   changed = 1;
589                   continue;
590                 }
591             }
592 #endif
593           else
594             {
595               /* Now that the jump has been tensioned,
596                  try cross jumping: check for identical code
597                  before the jump and before its target label.  */
598
599               /* First, cross jumping of conditional jumps:  */
600
601               if (cross_jump && condjump_p (insn))
602                 {
603                   rtx newjpos, newlpos;
604                   rtx x = prev_real_insn (JUMP_LABEL (insn));
605
606                   /* A conditional jump may be crossjumped
607                      only if the place it jumps to follows
608                      an opposing jump that comes back here.  */
609
610                   if (x != 0 && ! jump_back_p (x, insn))
611                     /* We have no opposing jump;
612                        cannot cross jump this insn.  */
613                     x = 0;
614
615                   newjpos = 0;
616                   /* TARGET is nonzero if it is ok to cross jump
617                      to code before TARGET.  If so, see if matches.  */
618                   if (x != 0)
619                     find_cross_jump (insn, x, 2,
620                                      &newjpos, &newlpos);
621
622                   if (newjpos != 0)
623                     {
624                       do_cross_jump (insn, newjpos, newlpos);
625                       /* Make the old conditional jump
626                          into an unconditional one.  */
627                       SET_SRC (PATTERN (insn))
628                         = gen_rtx_LABEL_REF (VOIDmode, JUMP_LABEL (insn));
629                       INSN_CODE (insn) = -1;
630                       emit_barrier_after (insn);
631                       /* Add to jump_chain unless this is a new label
632                          whose UID is too large.  */
633                       if (INSN_UID (JUMP_LABEL (insn)) < max_jump_chain)
634                         {
635                           jump_chain[INSN_UID (insn)]
636                             = jump_chain[INSN_UID (JUMP_LABEL (insn))];
637                           jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
638                         }
639                       changed = 1;
640                       next = insn;
641                     }
642                 }
643
644               /* Cross jumping of unconditional jumps:
645                  a few differences.  */
646
647               if (cross_jump && simplejump_p (insn))
648                 {
649                   rtx newjpos, newlpos;
650                   rtx target;
651
652                   newjpos = 0;
653
654                   /* TARGET is nonzero if it is ok to cross jump
655                      to code before TARGET.  If so, see if matches.  */
656                   find_cross_jump (insn, JUMP_LABEL (insn), 1,
657                                    &newjpos, &newlpos);
658
659                   /* If cannot cross jump to code before the label,
660                      see if we can cross jump to another jump to
661                      the same label.  */
662                   /* Try each other jump to this label.  */
663                   if (INSN_UID (JUMP_LABEL (insn)) < max_uid)
664                     for (target = jump_chain[INSN_UID (JUMP_LABEL (insn))];
665                          target != 0 && newjpos == 0;
666                          target = jump_chain[INSN_UID (target)])
667                       if (target != insn
668                           && JUMP_LABEL (target) == JUMP_LABEL (insn)
669                           /* Ignore TARGET if it's deleted.  */
670                           && ! INSN_DELETED_P (target))
671                         find_cross_jump (insn, target, 2,
672                                          &newjpos, &newlpos);
673
674                   if (newjpos != 0)
675                     {
676                       do_cross_jump (insn, newjpos, newlpos);
677                       changed = 1;
678                       next = insn;
679                     }
680                 }
681
682               /* This code was dead in the previous jump.c!  */
683               if (cross_jump && GET_CODE (PATTERN (insn)) == RETURN)
684                 {
685                   /* Return insns all "jump to the same place"
686                      so we can cross-jump between any two of them.  */
687
688                   rtx newjpos, newlpos, target;
689
690                   newjpos = 0;
691
692                   /* If cannot cross jump to code before the label,
693                      see if we can cross jump to another jump to
694                      the same label.  */
695                   /* Try each other jump to this label.  */
696                   for (target = jump_chain[0];
697                        target != 0 && newjpos == 0;
698                        target = jump_chain[INSN_UID (target)])
699                     if (target != insn
700                         && ! INSN_DELETED_P (target)
701                         && GET_CODE (PATTERN (target)) == RETURN)
702                       find_cross_jump (insn, target, 2,
703                                        &newjpos, &newlpos);
704
705                   if (newjpos != 0)
706                     {
707                       do_cross_jump (insn, newjpos, newlpos);
708                       changed = 1;
709                       next = insn;
710                     }
711                 }
712             }
713         }
714
715       first = 0;
716     }
717
718   /* Delete extraneous line number notes.
719      Note that two consecutive notes for different lines are not really
720      extraneous.  There should be some indication where that line belonged,
721      even if it became empty.  */
722
723   {
724     rtx last_note = 0;
725
726     for (insn = f; insn; insn = NEXT_INSN (insn))
727       if (GET_CODE (insn) == NOTE)
728         {
729           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
730             /* Any previous line note was for the prologue; gdb wants a new
731                note after the prologue even if it is for the same line.  */
732             last_note = NULL_RTX;
733           else if (NOTE_LINE_NUMBER (insn) >= 0)
734             {
735               /* Delete this note if it is identical to previous note.  */
736               if (last_note
737                   && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
738                   && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note))
739                 {
740                   delete_insn (insn);
741                   continue;
742                 }
743
744               last_note = insn;
745             }
746         }
747   }
748
749 end:
750   /* Clean up.  */
751   free (jump_chain);
752   jump_chain = 0;
753 }
754 \f
755 /* Initialize LABEL_NUSES and JUMP_LABEL fields.  Delete any REG_LABEL
756    notes whose labels don't occur in the insn any more.  Returns the
757    largest INSN_UID found.  */
758 static int
759 init_label_info (f)
760      rtx f;
761 {
762   int largest_uid = 0;
763   rtx insn;
764
765   for (insn = f; insn; insn = NEXT_INSN (insn))
766     {
767       if (GET_CODE (insn) == CODE_LABEL)
768         LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
769       else if (GET_CODE (insn) == JUMP_INSN)
770         JUMP_LABEL (insn) = 0;
771       else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
772         {
773           rtx note, next;
774
775           for (note = REG_NOTES (insn); note; note = next)
776             {
777               next = XEXP (note, 1);
778               if (REG_NOTE_KIND (note) == REG_LABEL
779                   && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
780                 remove_note (insn, note);
781             }
782         }
783       if (INSN_UID (insn) > largest_uid)
784         largest_uid = INSN_UID (insn);
785     }
786
787   return largest_uid;
788 }
789
790 /* Delete insns following barriers, up to next label.
791
792    Also delete no-op jumps created by gcse.  */
793
794 static void
795 delete_barrier_successors (f)
796      rtx f;
797 {
798   rtx insn;
799   rtx set;
800
801   for (insn = f; insn;)
802     {
803       if (GET_CODE (insn) == BARRIER)
804         {
805           insn = NEXT_INSN (insn);
806
807           never_reached_warning (insn);
808
809           while (insn != 0 && GET_CODE (insn) != CODE_LABEL)
810             {
811               if (GET_CODE (insn) == NOTE
812                   && NOTE_LINE_NUMBER (insn) != NOTE_INSN_FUNCTION_END)
813                 insn = NEXT_INSN (insn);
814               else
815                 insn = delete_insn (insn);
816             }
817           /* INSN is now the code_label.  */
818         }
819
820       /* Also remove (set (pc) (pc)) insns which can be created by
821          gcse.  We eliminate such insns now to avoid having them
822          cause problems later.  */
823       else if (GET_CODE (insn) == JUMP_INSN
824                && (set = pc_set (insn)) != NULL
825                && SET_SRC (set) == pc_rtx
826                && SET_DEST (set) == pc_rtx
827                && onlyjump_p (insn))
828         insn = delete_insn (insn);
829
830       else
831         insn = NEXT_INSN (insn);
832     }
833 }
834
835 /* Mark the label each jump jumps to.
836    Combine consecutive labels, and count uses of labels.
837
838    For each label, make a chain (using `jump_chain')
839    of all the *unconditional* jumps that jump to it;
840    also make a chain of all returns.
841
842    CROSS_JUMP indicates whether we are doing cross jumping
843    and if we are whether we will be paying attention to
844    death notes or not.  */
845
846 static void
847 mark_all_labels (f, cross_jump)
848      rtx f;
849      int cross_jump;
850 {
851   rtx insn;
852
853   for (insn = f; insn; insn = NEXT_INSN (insn))
854     if (INSN_P (insn))
855       {
856         if (GET_CODE (insn) == CALL_INSN
857             && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
858           {
859             mark_all_labels (XEXP (PATTERN (insn), 0), cross_jump);
860             mark_all_labels (XEXP (PATTERN (insn), 1), cross_jump);
861             mark_all_labels (XEXP (PATTERN (insn), 2), cross_jump);
862             continue;
863           }
864
865         mark_jump_label (PATTERN (insn), insn, cross_jump, 0);
866         if (! INSN_DELETED_P (insn) && GET_CODE (insn) == JUMP_INSN)
867           {
868             /* When we know the LABEL_REF contained in a REG used in
869                an indirect jump, we'll have a REG_LABEL note so that
870                flow can tell where it's going.  */
871             if (JUMP_LABEL (insn) == 0)
872               {
873                 rtx label_note = find_reg_note (insn, REG_LABEL, NULL_RTX);
874                 if (label_note)
875                   {
876                     /* But a LABEL_REF around the REG_LABEL note, so
877                        that we can canonicalize it.  */
878                     rtx label_ref = gen_rtx_LABEL_REF (VOIDmode,
879                                                        XEXP (label_note, 0));
880
881                     mark_jump_label (label_ref, insn, cross_jump, 0);
882                     XEXP (label_note, 0) = XEXP (label_ref, 0);
883                     JUMP_LABEL (insn) = XEXP (label_note, 0);
884                   }
885               }
886             if (JUMP_LABEL (insn) != 0 && simplejump_p (insn))
887               {
888                 jump_chain[INSN_UID (insn)]
889                   = jump_chain[INSN_UID (JUMP_LABEL (insn))];
890                 jump_chain[INSN_UID (JUMP_LABEL (insn))] = insn;
891               }
892             if (GET_CODE (PATTERN (insn)) == RETURN)
893               {
894                 jump_chain[INSN_UID (insn)] = jump_chain[0];
895                 jump_chain[0] = insn;
896               }
897           }
898       }
899 }
900
901 /* Delete all labels already not referenced.
902    Also find and return the last insn.  */
903
904 static rtx
905 delete_unreferenced_labels (f)
906      rtx f;
907 {
908   rtx final = NULL_RTX;
909   rtx insn;
910
911   for (insn = f; insn;)
912     {
913       if (GET_CODE (insn) == CODE_LABEL
914           && LABEL_NUSES (insn) == 0
915           && LABEL_ALTERNATE_NAME (insn) == NULL)
916         insn = delete_insn (insn);
917       else
918         {
919           final = insn;
920           insn = NEXT_INSN (insn);
921         }
922     }
923
924   return final;
925 }
926
927 /* Delete various simple forms of moves which have no necessary
928    side effect.  */
929
930 static void
931 delete_noop_moves (f)
932      rtx f;
933 {
934   rtx insn, next;
935
936   for (insn = f; insn;)
937     {
938       next = NEXT_INSN (insn);
939
940       if (GET_CODE (insn) == INSN)
941         {
942           register rtx body = PATTERN (insn);
943
944           /* Detect and delete no-op move instructions
945              resulting from not allocating a parameter in a register.  */
946
947           if (GET_CODE (body) == SET
948               && (SET_DEST (body) == SET_SRC (body)
949                   || (GET_CODE (SET_DEST (body)) == MEM
950                       && GET_CODE (SET_SRC (body)) == MEM
951                       && rtx_equal_p (SET_SRC (body), SET_DEST (body))))
952               && ! (GET_CODE (SET_DEST (body)) == MEM
953                     && MEM_VOLATILE_P (SET_DEST (body)))
954               && ! (GET_CODE (SET_SRC (body)) == MEM
955                     && MEM_VOLATILE_P (SET_SRC (body))))
956             delete_computation (insn);
957
958           /* Detect and ignore no-op move instructions
959              resulting from smart or fortuitous register allocation.  */
960
961           else if (GET_CODE (body) == SET)
962             {
963               int sreg = true_regnum (SET_SRC (body));
964               int dreg = true_regnum (SET_DEST (body));
965
966               if (sreg == dreg && sreg >= 0)
967                 delete_insn (insn);
968               else if (sreg >= 0 && dreg >= 0)
969                 {
970                   rtx trial;
971                   rtx tem = find_equiv_reg (NULL_RTX, insn, 0,
972                                             sreg, NULL_PTR, dreg,
973                                             GET_MODE (SET_SRC (body)));
974
975                   if (tem != 0
976                       && GET_MODE (tem) == GET_MODE (SET_DEST (body)))
977                     {
978                       /* DREG may have been the target of a REG_DEAD note in
979                          the insn which makes INSN redundant.  If so, reorg
980                          would still think it is dead.  So search for such a
981                          note and delete it if we find it.  */
982                       if (! find_regno_note (insn, REG_UNUSED, dreg))
983                         for (trial = prev_nonnote_insn (insn);
984                              trial && GET_CODE (trial) != CODE_LABEL;
985                              trial = prev_nonnote_insn (trial))
986                           if (find_regno_note (trial, REG_DEAD, dreg))
987                             {
988                               remove_death (dreg, trial);
989                               break;
990                             }
991
992                       /* Deleting insn could lose a death-note for SREG.  */
993                       if ((trial = find_regno_note (insn, REG_DEAD, sreg)))
994                         {
995                           /* Change this into a USE so that we won't emit
996                              code for it, but still can keep the note.  */
997                           PATTERN (insn)
998                             = gen_rtx_USE (VOIDmode, XEXP (trial, 0));
999                           INSN_CODE (insn) = -1;
1000                           /* Remove all reg notes but the REG_DEAD one.  */
1001                           REG_NOTES (insn) = trial;
1002                           XEXP (trial, 1) = NULL_RTX;
1003                         }
1004                       else
1005                         delete_insn (insn);
1006                     }
1007                 }
1008               else if (dreg >= 0 && CONSTANT_P (SET_SRC (body))
1009                        && find_equiv_reg (SET_SRC (body), insn, 0, dreg,
1010                                           NULL_PTR, 0,
1011                                           GET_MODE (SET_DEST (body))))
1012                 {
1013                   /* This handles the case where we have two consecutive
1014                      assignments of the same constant to pseudos that didn't
1015                      get a hard reg.  Each SET from the constant will be
1016                      converted into a SET of the spill register and an
1017                      output reload will be made following it.  This produces
1018                      two loads of the same constant into the same spill
1019                      register.  */
1020
1021                   rtx in_insn = insn;
1022
1023                   /* Look back for a death note for the first reg.
1024                      If there is one, it is no longer accurate.  */
1025                   while (in_insn && GET_CODE (in_insn) != CODE_LABEL)
1026                     {
1027                       if ((GET_CODE (in_insn) == INSN
1028                            || GET_CODE (in_insn) == JUMP_INSN)
1029                           && find_regno_note (in_insn, REG_DEAD, dreg))
1030                         {
1031                           remove_death (dreg, in_insn);
1032                           break;
1033                         }
1034                       in_insn = PREV_INSN (in_insn);
1035                     }
1036
1037                   /* Delete the second load of the value.  */
1038                   delete_insn (insn);
1039                 }
1040             }
1041           else if (GET_CODE (body) == PARALLEL)
1042             {
1043               /* If each part is a set between two identical registers or
1044                  a USE or CLOBBER, delete the insn.  */
1045               int i, sreg, dreg;
1046               rtx tem;
1047
1048               for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1049                 {
1050                   tem = XVECEXP (body, 0, i);
1051                   if (GET_CODE (tem) == USE || GET_CODE (tem) == CLOBBER)
1052                     continue;
1053
1054                   if (GET_CODE (tem) != SET
1055                       || (sreg = true_regnum (SET_SRC (tem))) < 0
1056                       || (dreg = true_regnum (SET_DEST (tem))) < 0
1057                       || dreg != sreg)
1058                     break;
1059                 }
1060
1061               if (i < 0)
1062                 delete_insn (insn);
1063             }
1064           /* Also delete insns to store bit fields if they are no-ops.  */
1065           /* Not worth the hair to detect this in the big-endian case.  */
1066           else if (! BYTES_BIG_ENDIAN
1067                    && GET_CODE (body) == SET
1068                    && GET_CODE (SET_DEST (body)) == ZERO_EXTRACT
1069                    && XEXP (SET_DEST (body), 2) == const0_rtx
1070                    && XEXP (SET_DEST (body), 0) == SET_SRC (body)
1071                    && ! (GET_CODE (SET_SRC (body)) == MEM
1072                          && MEM_VOLATILE_P (SET_SRC (body))))
1073             delete_insn (insn);
1074         }
1075       insn = next;
1076     }
1077 }
1078
1079 /* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
1080    jump.  Assume that this unconditional jump is to the exit test code.  If
1081    the code is sufficiently simple, make a copy of it before INSN,
1082    followed by a jump to the exit of the loop.  Then delete the unconditional
1083    jump after INSN.
1084
1085    Return 1 if we made the change, else 0.
1086
1087    This is only safe immediately after a regscan pass because it uses the
1088    values of regno_first_uid and regno_last_uid.  */
1089
1090 static int
1091 duplicate_loop_exit_test (loop_start)
1092      rtx loop_start;
1093 {
1094   rtx insn, set, reg, p, link;
1095   rtx copy = 0, first_copy = 0;
1096   int num_insns = 0;
1097   rtx exitcode = NEXT_INSN (JUMP_LABEL (next_nonnote_insn (loop_start)));
1098   rtx lastexit;
1099   int max_reg = max_reg_num ();
1100   rtx *reg_map = 0;
1101
1102   /* Scan the exit code.  We do not perform this optimization if any insn:
1103
1104          is a CALL_INSN
1105          is a CODE_LABEL
1106          has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
1107          is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
1108          is a NOTE_INSN_BLOCK_{BEG,END} because duplicating these notes
1109               is not valid.
1110
1111      We also do not do this if we find an insn with ASM_OPERANDS.  While
1112      this restriction should not be necessary, copying an insn with
1113      ASM_OPERANDS can confuse asm_noperands in some cases.
1114
1115      Also, don't do this if the exit code is more than 20 insns.  */
1116
1117   for (insn = exitcode;
1118        insn
1119        && ! (GET_CODE (insn) == NOTE
1120              && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
1121        insn = NEXT_INSN (insn))
1122     {
1123       switch (GET_CODE (insn))
1124         {
1125         case CODE_LABEL:
1126         case CALL_INSN:
1127           return 0;
1128         case NOTE:
1129           /* We could be in front of the wrong NOTE_INSN_LOOP_END if there is
1130              a jump immediately after the loop start that branches outside
1131              the loop but within an outer loop, near the exit test.
1132              If we copied this exit test and created a phony
1133              NOTE_INSN_LOOP_VTOP, this could make instructions immediately
1134              before the exit test look like these could be safely moved
1135              out of the loop even if they actually may be never executed.
1136              This can be avoided by checking here for NOTE_INSN_LOOP_CONT.  */
1137
1138           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
1139               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
1140             return 0;
1141
1142           if (optimize < 2
1143               && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
1144                   || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
1145             /* If we were to duplicate this code, we would not move
1146                the BLOCK notes, and so debugging the moved code would
1147                be difficult.  Thus, we only move the code with -O2 or
1148                higher.  */
1149             return 0;
1150
1151           break;
1152         case JUMP_INSN:
1153         case INSN:
1154           /* The code below would grossly mishandle REG_WAS_0 notes,
1155              so get rid of them here.  */
1156           while ((p = find_reg_note (insn, REG_WAS_0, NULL_RTX)) != 0)
1157             remove_note (insn, p);
1158           if (++num_insns > 20
1159               || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1160               || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
1161             return 0;
1162           break;
1163         default:
1164           break;
1165         }
1166     }
1167
1168   /* Unless INSN is zero, we can do the optimization.  */
1169   if (insn == 0)
1170     return 0;
1171
1172   lastexit = insn;
1173
1174   /* See if any insn sets a register only used in the loop exit code and
1175      not a user variable.  If so, replace it with a new register.  */
1176   for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
1177     if (GET_CODE (insn) == INSN
1178         && (set = single_set (insn)) != 0
1179         && ((reg = SET_DEST (set), GET_CODE (reg) == REG)
1180             || (GET_CODE (reg) == SUBREG
1181                 && (reg = SUBREG_REG (reg), GET_CODE (reg) == REG)))
1182         && REGNO (reg) >= FIRST_PSEUDO_REGISTER
1183         && REGNO_FIRST_UID (REGNO (reg)) == INSN_UID (insn))
1184       {
1185         for (p = NEXT_INSN (insn); p != lastexit; p = NEXT_INSN (p))
1186           if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (p))
1187             break;
1188
1189         if (p != lastexit)
1190           {
1191             /* We can do the replacement.  Allocate reg_map if this is the
1192                first replacement we found.  */
1193             if (reg_map == 0)
1194               reg_map = (rtx *) xcalloc (max_reg, sizeof (rtx));
1195
1196             REG_LOOP_TEST_P (reg) = 1;
1197
1198             reg_map[REGNO (reg)] = gen_reg_rtx (GET_MODE (reg));
1199           }
1200       }
1201
1202   /* Now copy each insn.  */
1203   for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
1204     {
1205       switch (GET_CODE (insn))
1206         {
1207         case BARRIER:
1208           copy = emit_barrier_before (loop_start);
1209           break;
1210         case NOTE:
1211           /* Only copy line-number notes.  */
1212           if (NOTE_LINE_NUMBER (insn) >= 0)
1213             {
1214               copy = emit_note_before (NOTE_LINE_NUMBER (insn), loop_start);
1215               NOTE_SOURCE_FILE (copy) = NOTE_SOURCE_FILE (insn);
1216             }
1217           break;
1218
1219         case INSN:
1220           copy = emit_insn_before (copy_insn (PATTERN (insn)), loop_start);
1221           if (reg_map)
1222             replace_regs (PATTERN (copy), reg_map, max_reg, 1);
1223
1224           mark_jump_label (PATTERN (copy), copy, 0, 0);
1225
1226           /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
1227              make them.  */
1228           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1229             if (REG_NOTE_KIND (link) != REG_LABEL)
1230               {
1231                 if (GET_CODE (link) == EXPR_LIST)
1232                   REG_NOTES (copy)
1233                     = copy_insn_1 (gen_rtx_EXPR_LIST (REG_NOTE_KIND (link),
1234                                                       XEXP (link, 0),
1235                                                       REG_NOTES (copy)));
1236                 else
1237                   REG_NOTES (copy)
1238                     = copy_insn_1 (gen_rtx_INSN_LIST (REG_NOTE_KIND (link),
1239                                                       XEXP (link, 0),
1240                                                       REG_NOTES (copy)));
1241               }
1242
1243           if (reg_map && REG_NOTES (copy))
1244             replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
1245           break;
1246
1247         case JUMP_INSN:
1248           copy = emit_jump_insn_before (copy_insn (PATTERN (insn)),
1249                                         loop_start);
1250           if (reg_map)
1251             replace_regs (PATTERN (copy), reg_map, max_reg, 1);
1252           mark_jump_label (PATTERN (copy), copy, 0, 0);
1253           if (REG_NOTES (insn))
1254             {
1255               REG_NOTES (copy) = copy_insn_1 (REG_NOTES (insn));
1256               if (reg_map)
1257                 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
1258             }
1259
1260           /* If this is a simple jump, add it to the jump chain.  */
1261
1262           if (INSN_UID (copy) < max_jump_chain && JUMP_LABEL (copy)
1263               && simplejump_p (copy))
1264             {
1265               jump_chain[INSN_UID (copy)]
1266                 = jump_chain[INSN_UID (JUMP_LABEL (copy))];
1267               jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
1268             }
1269           break;
1270
1271         default:
1272           abort ();
1273         }
1274
1275       /* Record the first insn we copied.  We need it so that we can
1276          scan the copied insns for new pseudo registers.  */
1277       if (! first_copy)
1278         first_copy = copy;
1279     }
1280
1281   /* Now clean up by emitting a jump to the end label and deleting the jump
1282      at the start of the loop.  */
1283   if (! copy || GET_CODE (copy) != BARRIER)
1284     {
1285       copy = emit_jump_insn_before (gen_jump (get_label_after (insn)),
1286                                     loop_start);
1287
1288       /* Record the first insn we copied.  We need it so that we can
1289          scan the copied insns for new pseudo registers.   This may not
1290          be strictly necessary since we should have copied at least one
1291          insn above.  But I am going to be safe.  */
1292       if (! first_copy)
1293         first_copy = copy;
1294
1295       mark_jump_label (PATTERN (copy), copy, 0, 0);
1296       if (INSN_UID (copy) < max_jump_chain
1297           && INSN_UID (JUMP_LABEL (copy)) < max_jump_chain)
1298         {
1299           jump_chain[INSN_UID (copy)]
1300             = jump_chain[INSN_UID (JUMP_LABEL (copy))];
1301           jump_chain[INSN_UID (JUMP_LABEL (copy))] = copy;
1302         }
1303       emit_barrier_before (loop_start);
1304     }
1305
1306   /* Now scan from the first insn we copied to the last insn we copied
1307      (copy) for new pseudo registers.  Do this after the code to jump to
1308      the end label since that might create a new pseudo too.  */
1309   reg_scan_update (first_copy, copy, max_reg);
1310
1311   /* Mark the exit code as the virtual top of the converted loop.  */
1312   emit_note_before (NOTE_INSN_LOOP_VTOP, exitcode);
1313
1314   delete_insn (next_nonnote_insn (loop_start));
1315
1316   /* Clean up.  */
1317   if (reg_map)
1318     free (reg_map);
1319
1320   return 1;
1321 }
1322 \f
1323 /* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, loop-end,
1324    eh-beg, eh-end notes between START and END out before START.  Assume that
1325    END is not such a note.  START may be such a note.  Returns the value
1326    of the new starting insn, which may be different if the original start
1327    was such a note.  */
1328
1329 rtx
1330 squeeze_notes (start, end)
1331      rtx start, end;
1332 {
1333   rtx insn;
1334   rtx next;
1335
1336   for (insn = start; insn != end; insn = next)
1337     {
1338       next = NEXT_INSN (insn);
1339       if (GET_CODE (insn) == NOTE
1340           && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
1341               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
1342               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
1343               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
1344               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
1345               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP
1346               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
1347               || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END))
1348         {
1349           if (insn == start)
1350             start = next;
1351           else
1352             {
1353               rtx prev = PREV_INSN (insn);
1354               PREV_INSN (insn) = PREV_INSN (start);
1355               NEXT_INSN (insn) = start;
1356               NEXT_INSN (PREV_INSN (insn)) = insn;
1357               PREV_INSN (NEXT_INSN (insn)) = insn;
1358               NEXT_INSN (prev) = next;
1359               PREV_INSN (next) = prev;
1360             }
1361         }
1362     }
1363
1364   return start;
1365 }
1366 \f
1367 /* Compare the instructions before insn E1 with those before E2
1368    to find an opportunity for cross jumping.
1369    (This means detecting identical sequences of insns followed by
1370    jumps to the same place, or followed by a label and a jump
1371    to that label, and replacing one with a jump to the other.)
1372
1373    Assume E1 is a jump that jumps to label E2
1374    (that is not always true but it might as well be).
1375    Find the longest possible equivalent sequences
1376    and store the first insns of those sequences into *F1 and *F2.
1377    Store zero there if no equivalent preceding instructions are found.
1378
1379    We give up if we find a label in stream 1.
1380    Actually we could transfer that label into stream 2.  */
1381
1382 static void
1383 find_cross_jump (e1, e2, minimum, f1, f2)
1384      rtx e1, e2;
1385      int minimum;
1386      rtx *f1, *f2;
1387 {
1388   register rtx i1 = e1, i2 = e2;
1389   register rtx p1, p2;
1390   int lose = 0;
1391
1392   rtx last1 = 0, last2 = 0;
1393   rtx afterlast1 = 0, afterlast2 = 0;
1394
1395   *f1 = 0;
1396   *f2 = 0;
1397
1398   while (1)
1399     {
1400       i1 = prev_nonnote_insn (i1);
1401
1402       i2 = PREV_INSN (i2);
1403       while (i2 && (GET_CODE (i2) == NOTE || GET_CODE (i2) == CODE_LABEL))
1404         i2 = PREV_INSN (i2);
1405
1406       if (i1 == 0)
1407         break;
1408
1409       /* Don't allow the range of insns preceding E1 or E2
1410          to include the other (E2 or E1).  */
1411       if (i2 == e1 || i1 == e2)
1412         break;
1413
1414       /* If we will get to this code by jumping, those jumps will be
1415          tensioned to go directly to the new label (before I2),
1416          so this cross-jumping won't cost extra.  So reduce the minimum.  */
1417       if (GET_CODE (i1) == CODE_LABEL)
1418         {
1419           --minimum;
1420           break;
1421         }
1422
1423       if (i2 == 0 || GET_CODE (i1) != GET_CODE (i2))
1424         break;
1425
1426       /* Avoid moving insns across EH regions if either of the insns
1427          can throw.  */
1428       if (flag_exceptions
1429           && (asynchronous_exceptions || GET_CODE (i1) == CALL_INSN)
1430           && !in_same_eh_region (i1, i2))
1431         break;
1432
1433       p1 = PATTERN (i1);
1434       p2 = PATTERN (i2);
1435
1436       /* If this is a CALL_INSN, compare register usage information.
1437          If we don't check this on stack register machines, the two
1438          CALL_INSNs might be merged leaving reg-stack.c with mismatching
1439          numbers of stack registers in the same basic block.
1440          If we don't check this on machines with delay slots, a delay slot may
1441          be filled that clobbers a parameter expected by the subroutine.
1442
1443          ??? We take the simple route for now and assume that if they're
1444          equal, they were constructed identically.  */
1445
1446       if (GET_CODE (i1) == CALL_INSN
1447           && ! rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
1448                             CALL_INSN_FUNCTION_USAGE (i2)))
1449         lose = 1;
1450
1451 #ifdef STACK_REGS
1452       /* If cross_jump_death_matters is not 0, the insn's mode
1453          indicates whether or not the insn contains any stack-like
1454          regs.  */
1455
1456       if (!lose && cross_jump_death_matters && stack_regs_mentioned (i1))
1457         {
1458           /* If register stack conversion has already been done, then
1459              death notes must also be compared before it is certain that
1460              the two instruction streams match.  */
1461
1462           rtx note;
1463           HARD_REG_SET i1_regset, i2_regset;
1464
1465           CLEAR_HARD_REG_SET (i1_regset);
1466           CLEAR_HARD_REG_SET (i2_regset);
1467
1468           for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
1469             if (REG_NOTE_KIND (note) == REG_DEAD
1470                 && STACK_REG_P (XEXP (note, 0)))
1471               SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
1472
1473           for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
1474             if (REG_NOTE_KIND (note) == REG_DEAD
1475                 && STACK_REG_P (XEXP (note, 0)))
1476               SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
1477
1478           GO_IF_HARD_REG_EQUAL (i1_regset, i2_regset, done);
1479
1480           lose = 1;
1481
1482         done:
1483           ;
1484         }
1485 #endif
1486
1487       /* Don't allow old-style asm or volatile extended asms to be accepted
1488          for cross jumping purposes.  It is conceptually correct to allow
1489          them, since cross-jumping preserves the dynamic instruction order
1490          even though it is changing the static instruction order.  However,
1491          if an asm is being used to emit an assembler pseudo-op, such as
1492          the MIPS `.set reorder' pseudo-op, then the static instruction order
1493          matters and it must be preserved.  */
1494       if (GET_CODE (p1) == ASM_INPUT || GET_CODE (p2) == ASM_INPUT
1495           || (GET_CODE (p1) == ASM_OPERANDS && MEM_VOLATILE_P (p1))
1496           || (GET_CODE (p2) == ASM_OPERANDS && MEM_VOLATILE_P (p2)))
1497         lose = 1;
1498
1499       if (lose || GET_CODE (p1) != GET_CODE (p2)
1500           || ! rtx_renumbered_equal_p (p1, p2))
1501         {
1502           /* The following code helps take care of G++ cleanups.  */
1503           rtx equiv1;
1504           rtx equiv2;
1505
1506           if (!lose && GET_CODE (p1) == GET_CODE (p2)
1507               && ((equiv1 = find_reg_note (i1, REG_EQUAL, NULL_RTX)) != 0
1508                   || (equiv1 = find_reg_note (i1, REG_EQUIV, NULL_RTX)) != 0)
1509               && ((equiv2 = find_reg_note (i2, REG_EQUAL, NULL_RTX)) != 0
1510                   || (equiv2 = find_reg_note (i2, REG_EQUIV, NULL_RTX)) != 0)
1511               /* If the equivalences are not to a constant, they may
1512                  reference pseudos that no longer exist, so we can't
1513                  use them.  */
1514               && CONSTANT_P (XEXP (equiv1, 0))
1515               && rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
1516             {
1517               rtx s1 = single_set (i1);
1518               rtx s2 = single_set (i2);
1519               if (s1 != 0 && s2 != 0
1520                   && rtx_renumbered_equal_p (SET_DEST (s1), SET_DEST (s2)))
1521                 {
1522                   validate_change (i1, &SET_SRC (s1), XEXP (equiv1, 0), 1);
1523                   validate_change (i2, &SET_SRC (s2), XEXP (equiv2, 0), 1);
1524                   if (! rtx_renumbered_equal_p (p1, p2))
1525                     cancel_changes (0);
1526                   else if (apply_change_group ())
1527                     goto win;
1528                 }
1529             }
1530
1531           /* Insns fail to match; cross jumping is limited to the following
1532              insns.  */
1533
1534 #ifdef HAVE_cc0
1535           /* Don't allow the insn after a compare to be shared by
1536              cross-jumping unless the compare is also shared.
1537              Here, if either of these non-matching insns is a compare,
1538              exclude the following insn from possible cross-jumping.  */
1539           if (sets_cc0_p (p1) || sets_cc0_p (p2))
1540             last1 = afterlast1, last2 = afterlast2, ++minimum;
1541 #endif
1542
1543           /* If cross-jumping here will feed a jump-around-jump
1544              optimization, this jump won't cost extra, so reduce
1545              the minimum.  */
1546           if (GET_CODE (i1) == JUMP_INSN
1547               && JUMP_LABEL (i1)
1548               && prev_real_insn (JUMP_LABEL (i1)) == e1)
1549             --minimum;
1550           break;
1551         }
1552
1553     win:
1554       if (GET_CODE (p1) != USE && GET_CODE (p1) != CLOBBER)
1555         {
1556           /* Ok, this insn is potentially includable in a cross-jump here.  */
1557           afterlast1 = last1, afterlast2 = last2;
1558           last1 = i1, last2 = i2, --minimum;
1559         }
1560     }
1561
1562   if (minimum <= 0 && last1 != 0 && last1 != e1)
1563     *f1 = last1, *f2 = last2;
1564 }
1565
1566 static void
1567 do_cross_jump (insn, newjpos, newlpos)
1568      rtx insn, newjpos, newlpos;
1569 {
1570   /* Find an existing label at this point
1571      or make a new one if there is none.  */
1572   register rtx label = get_label_before (newlpos);
1573
1574   /* Make the same jump insn jump to the new point.  */
1575   if (GET_CODE (PATTERN (insn)) == RETURN)
1576     {
1577       /* Remove from jump chain of returns.  */
1578       delete_from_jump_chain (insn);
1579       /* Change the insn.  */
1580       PATTERN (insn) = gen_jump (label);
1581       INSN_CODE (insn) = -1;
1582       JUMP_LABEL (insn) = label;
1583       LABEL_NUSES (label)++;
1584       /* Add to new the jump chain.  */
1585       if (INSN_UID (label) < max_jump_chain
1586           && INSN_UID (insn) < max_jump_chain)
1587         {
1588           jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (label)];
1589           jump_chain[INSN_UID (label)] = insn;
1590         }
1591     }
1592   else
1593     redirect_jump (insn, label, 1);
1594
1595   /* Delete the matching insns before the jump.  Also, remove any REG_EQUAL
1596      or REG_EQUIV note in the NEWLPOS stream that isn't also present in
1597      the NEWJPOS stream.  */
1598
1599   while (newjpos != insn)
1600     {
1601       rtx lnote;
1602
1603       for (lnote = REG_NOTES (newlpos); lnote; lnote = XEXP (lnote, 1))
1604         if ((REG_NOTE_KIND (lnote) == REG_EQUAL
1605              || REG_NOTE_KIND (lnote) == REG_EQUIV)
1606             && ! find_reg_note (newjpos, REG_EQUAL, XEXP (lnote, 0))
1607             && ! find_reg_note (newjpos, REG_EQUIV, XEXP (lnote, 0)))
1608           remove_note (newlpos, lnote);
1609
1610       delete_insn (newjpos);
1611       newjpos = next_real_insn (newjpos);
1612       newlpos = next_real_insn (newlpos);
1613     }
1614 }
1615 \f
1616 /* Return the label before INSN, or put a new label there.  */
1617
1618 rtx
1619 get_label_before (insn)
1620      rtx insn;
1621 {
1622   rtx label;
1623
1624   /* Find an existing label at this point
1625      or make a new one if there is none.  */
1626   label = prev_nonnote_insn (insn);
1627
1628   if (label == 0 || GET_CODE (label) != CODE_LABEL)
1629     {
1630       rtx prev = PREV_INSN (insn);
1631
1632       label = gen_label_rtx ();
1633       emit_label_after (label, prev);
1634       LABEL_NUSES (label) = 0;
1635     }
1636   return label;
1637 }
1638
1639 /* Return the label after INSN, or put a new label there.  */
1640
1641 rtx
1642 get_label_after (insn)
1643      rtx insn;
1644 {
1645   rtx label;
1646
1647   /* Find an existing label at this point
1648      or make a new one if there is none.  */
1649   label = next_nonnote_insn (insn);
1650
1651   if (label == 0 || GET_CODE (label) != CODE_LABEL)
1652     {
1653       label = gen_label_rtx ();
1654       emit_label_after (label, insn);
1655       LABEL_NUSES (label) = 0;
1656     }
1657   return label;
1658 }
1659 \f
1660 /* Return 1 if INSN is a jump that jumps to right after TARGET
1661    only on the condition that TARGET itself would drop through.
1662    Assumes that TARGET is a conditional jump.  */
1663
1664 static int
1665 jump_back_p (insn, target)
1666      rtx insn, target;
1667 {
1668   rtx cinsn, ctarget;
1669   enum rtx_code codei, codet;
1670   rtx set, tset;
1671
1672   if (! any_condjump_p (insn)
1673       || any_uncondjump_p (target)
1674       || target != prev_real_insn (JUMP_LABEL (insn)))
1675     return 0;
1676   set = pc_set (insn);
1677   tset = pc_set (target);
1678
1679   cinsn = XEXP (SET_SRC (set), 0);
1680   ctarget = XEXP (SET_SRC (tset), 0);
1681
1682   codei = GET_CODE (cinsn);
1683   codet = GET_CODE (ctarget);
1684
1685   if (XEXP (SET_SRC (set), 1) == pc_rtx)
1686     {
1687       codei = reversed_comparison_code (cinsn, insn);
1688       if (codei == UNKNOWN)
1689         return 0;
1690     }
1691
1692   if (XEXP (SET_SRC (tset), 2) == pc_rtx)
1693     {
1694       codet = reversed_comparison_code (ctarget, target);
1695       if (codei == UNKNOWN)
1696         return 0;
1697     }
1698
1699   return (codei == codet
1700           && rtx_renumbered_equal_p (XEXP (cinsn, 0), XEXP (ctarget, 0))
1701           && rtx_renumbered_equal_p (XEXP (cinsn, 1), XEXP (ctarget, 1)));
1702 }
1703 \f
1704 /* Given a comparison (CODE ARG0 ARG1), inside a insn, INSN, return an code
1705    of reversed comparison if it is possible to do so.  Otherwise return UNKNOWN.
1706    UNKNOWN may be returned in case we are having CC_MODE compare and we don't
1707    know whether it's source is floating point or integer comparison.  Machine
1708    description should define REVERSIBLE_CC_MODE and REVERSE_CONDITION macros
1709    to help this function avoid overhead in these cases.  */
1710 enum rtx_code
1711 reversed_comparison_code_parts (code, arg0, arg1, insn)
1712      rtx insn, arg0, arg1;
1713      enum rtx_code code;
1714 {
1715   enum machine_mode mode;
1716
1717   /* If this is not actually a comparison, we can't reverse it.  */
1718   if (GET_RTX_CLASS (code) != '<')
1719     return UNKNOWN;
1720
1721   mode = GET_MODE (arg0);
1722   if (mode == VOIDmode)
1723     mode = GET_MODE (arg1);
1724
1725   /* First see if machine description supply us way to reverse the comparison.
1726      Give it priority over everything else to allow machine description to do
1727      tricks.  */
1728 #ifdef REVERSIBLE_CC_MODE
1729   if (GET_MODE_CLASS (mode) == MODE_CC
1730       && REVERSIBLE_CC_MODE (mode))
1731     {
1732 #ifdef REVERSE_CONDITION
1733            return REVERSE_CONDITION (code, mode);
1734 #endif
1735            return reverse_condition (code);
1736         }
1737 #endif
1738
1739   /* Try few special cases based on the comparison code.  */
1740   switch (code)
1741     {
1742       case GEU:
1743       case GTU:
1744       case LEU:
1745       case LTU:
1746       case NE:
1747       case EQ:
1748         /* It is always safe to reverse EQ and NE, even for the floating
1749            point.  Similary the unsigned comparisons are never used for
1750            floating point so we can reverse them in the default way.  */
1751         return reverse_condition (code);
1752       case ORDERED:
1753       case UNORDERED:
1754       case LTGT:
1755       case UNEQ:
1756         /* In case we already see unordered comparison, we can be sure to
1757            be dealing with floating point so we don't need any more tests.  */
1758         return reverse_condition_maybe_unordered (code);
1759       case UNLT:
1760       case UNLE:
1761       case UNGT:
1762       case UNGE:
1763         /* We don't have safe way to reverse these yet.  */
1764         return UNKNOWN;
1765       default:
1766         break;
1767     }
1768
1769   /* In case we give up IEEE compatibility, all comparisons are reversible.  */
1770   if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1771       || flag_fast_math)
1772     return reverse_condition (code);
1773
1774   if (GET_MODE_CLASS (mode) == MODE_CC
1775 #ifdef HAVE_cc0
1776       || arg0 == cc0_rtx
1777 #endif
1778       )
1779     {
1780       rtx prev;
1781       /* Try to search for the comparison to determine the real mode.
1782          This code is expensive, but with sane machine description it
1783          will be never used, since REVERSIBLE_CC_MODE will return true
1784          in all cases.  */
1785       if (! insn)
1786         return UNKNOWN;
1787
1788       for (prev = prev_nonnote_insn (insn);
1789            prev != 0 && GET_CODE (prev) != CODE_LABEL;
1790            prev = prev_nonnote_insn (prev))
1791         {
1792           rtx set = set_of (arg0, prev);
1793           if (set && GET_CODE (set) == SET
1794               && rtx_equal_p (SET_DEST (set), arg0))
1795             {
1796               rtx src = SET_SRC (set);
1797
1798               if (GET_CODE (src) == COMPARE)
1799                 {
1800                   rtx comparison = src;
1801                   arg0 = XEXP (src, 0);
1802                   mode = GET_MODE (arg0);
1803                   if (mode == VOIDmode)
1804                     mode = GET_MODE (XEXP (comparison, 1));
1805                   break;
1806                 }
1807               /* We can get past reg-reg moves.  This may be usefull for model
1808                  of i387 comparisons that first move flag registers around.  */
1809               if (REG_P (src))
1810                 {
1811                   arg0 = src;
1812                   continue;
1813                 }
1814             }
1815           /* If register is clobbered in some ununderstandable way,
1816              give up.  */
1817           if (set)
1818             return UNKNOWN;
1819         }
1820     }
1821
1822   /* An integer condition.  */
1823   if (GET_CODE (arg0) == CONST_INT
1824       || (GET_MODE (arg0) != VOIDmode
1825           && GET_MODE_CLASS (mode) != MODE_CC
1826           && ! FLOAT_MODE_P (mode)))
1827     return reverse_condition (code);
1828
1829   return UNKNOWN;
1830 }
1831
1832 /* An wrapper around the previous function to take COMPARISON as rtx
1833    expression.  This simplifies many callers.  */
1834 enum rtx_code
1835 reversed_comparison_code (comparison, insn)
1836      rtx comparison, insn;
1837 {
1838   if (GET_RTX_CLASS (GET_CODE (comparison)) != '<')
1839     return UNKNOWN;
1840   return reversed_comparison_code_parts (GET_CODE (comparison),
1841                                          XEXP (comparison, 0),
1842                                          XEXP (comparison, 1), insn);
1843 }
1844 \f
1845 /* Given a comparison, COMPARISON, inside a conditional jump insn, INSN,
1846    return non-zero if it is safe to reverse this comparison.  It is if our
1847    floating-point is not IEEE, if this is an NE or EQ comparison, or if
1848    this is known to be an integer comparison.  
1849  
1850    Use of this function is depreached and you should use
1851    REVERSED_COMPARISON_CODE bits instead.
1852  */
1853
1854 int
1855 can_reverse_comparison_p (comparison, insn)
1856      rtx comparison;
1857      rtx insn;
1858 {
1859   enum rtx_code code;
1860
1861   /* If this is not actually a comparison, we can't reverse it.  */
1862   if (GET_RTX_CLASS (GET_CODE (comparison)) != '<')
1863     return 0;
1864
1865   code = reversed_comparison_code (comparison, insn);
1866   if (code == UNKNOWN)
1867     return 0;
1868
1869   /* The code will follow can_reverse_comparison_p with reverse_condition,
1870      so see if it will get proper result.  */
1871   return (code == reverse_condition (GET_CODE (comparison)));
1872 }
1873
1874 /* Given an rtx-code for a comparison, return the code for the negated
1875    comparison.  If no such code exists, return UNKNOWN.
1876
1877    WATCH OUT!  reverse_condition is not safe to use on a jump that might
1878    be acting on the results of an IEEE floating point comparison, because
1879    of the special treatment of non-signaling nans in comparisons.
1880    Use reversed_comparison_code instead.  */
1881
1882 enum rtx_code
1883 reverse_condition (code)
1884      enum rtx_code code;
1885 {
1886   switch (code)
1887     {
1888     case EQ:
1889       return NE;
1890     case NE:
1891       return EQ;
1892     case GT:
1893       return LE;
1894     case GE:
1895       return LT;
1896     case LT:
1897       return GE;
1898     case LE:
1899       return GT;
1900     case GTU:
1901       return LEU;
1902     case GEU:
1903       return LTU;
1904     case LTU:
1905       return GEU;
1906     case LEU:
1907       return GTU;
1908     case UNORDERED:
1909       return ORDERED;
1910     case ORDERED:
1911       return UNORDERED;
1912
1913     case UNLT:
1914     case UNLE:
1915     case UNGT:
1916     case UNGE:
1917     case UNEQ:
1918     case LTGT:
1919       return UNKNOWN;
1920
1921     default:
1922       abort ();
1923     }
1924 }
1925
1926 /* Similar, but we're allowed to generate unordered comparisons, which
1927    makes it safe for IEEE floating-point.  Of course, we have to recognize
1928    that the target will support them too...  */
1929
1930 enum rtx_code
1931 reverse_condition_maybe_unordered (code)
1932      enum rtx_code code;
1933 {
1934   /* Non-IEEE formats don't have unordered conditions.  */
1935   if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
1936     return reverse_condition (code);
1937
1938   switch (code)
1939     {
1940     case EQ:
1941       return NE;
1942     case NE:
1943       return EQ;
1944     case GT:
1945       return UNLE;
1946     case GE:
1947       return UNLT;
1948     case LT:
1949       return UNGE;
1950     case LE:
1951       return UNGT;
1952     case LTGT:
1953       return UNEQ;
1954     case UNORDERED:
1955       return ORDERED;
1956     case ORDERED:
1957       return UNORDERED;
1958     case UNLT:
1959       return GE;
1960     case UNLE:
1961       return GT;
1962     case UNGT:
1963       return LE;
1964     case UNGE:
1965       return LT;
1966     case UNEQ:
1967       return LTGT;
1968
1969     default:
1970       abort ();
1971     }
1972 }
1973
1974 /* Similar, but return the code when two operands of a comparison are swapped.
1975    This IS safe for IEEE floating-point.  */
1976
1977 enum rtx_code
1978 swap_condition (code)
1979      enum rtx_code code;
1980 {
1981   switch (code)
1982     {
1983     case EQ:
1984     case NE:
1985     case UNORDERED:
1986     case ORDERED:
1987     case UNEQ:
1988     case LTGT:
1989       return code;
1990
1991     case GT:
1992       return LT;
1993     case GE:
1994       return LE;
1995     case LT:
1996       return GT;
1997     case LE:
1998       return GE;
1999     case GTU:
2000       return LTU;
2001     case GEU:
2002       return LEU;
2003     case LTU:
2004       return GTU;
2005     case LEU:
2006       return GEU;
2007     case UNLT:
2008       return UNGT;
2009     case UNLE:
2010       return UNGE;
2011     case UNGT:
2012       return UNLT;
2013     case UNGE:
2014       return UNLE;
2015
2016     default:
2017       abort ();
2018     }
2019 }
2020
2021 /* Given a comparison CODE, return the corresponding unsigned comparison.
2022    If CODE is an equality comparison or already an unsigned comparison,
2023    CODE is returned.  */
2024
2025 enum rtx_code
2026 unsigned_condition (code)
2027      enum rtx_code code;
2028 {
2029   switch (code)
2030     {
2031     case EQ:
2032     case NE:
2033     case GTU:
2034     case GEU:
2035     case LTU:
2036     case LEU:
2037       return code;
2038
2039     case GT:
2040       return GTU;
2041     case GE:
2042       return GEU;
2043     case LT:
2044       return LTU;
2045     case LE:
2046       return LEU;
2047
2048     default:
2049       abort ();
2050     }
2051 }
2052
2053 /* Similarly, return the signed version of a comparison.  */
2054
2055 enum rtx_code
2056 signed_condition (code)
2057      enum rtx_code code;
2058 {
2059   switch (code)
2060     {
2061     case EQ:
2062     case NE:
2063     case GT:
2064     case GE:
2065     case LT:
2066     case LE:
2067       return code;
2068
2069     case GTU:
2070       return GT;
2071     case GEU:
2072       return GE;
2073     case LTU:
2074       return LT;
2075     case LEU:
2076       return LE;
2077
2078     default:
2079       abort ();
2080     }
2081 }
2082 \f
2083 /* Return non-zero if CODE1 is more strict than CODE2, i.e., if the
2084    truth of CODE1 implies the truth of CODE2.  */
2085
2086 int
2087 comparison_dominates_p (code1, code2)
2088      enum rtx_code code1, code2;
2089 {
2090   if (code1 == code2)
2091     return 1;
2092
2093   switch (code1)
2094     {
2095     case UNEQ:
2096       if (code2 == UNLE || code2 == UNGE)
2097         return 1;
2098       break;
2099
2100     case EQ:
2101       if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU
2102           || code2 == ORDERED)
2103         return 1;
2104       break;
2105
2106     case UNLT:
2107       if (code2 == UNLE || code2 == NE)
2108         return 1;
2109       break;
2110
2111     case LT:
2112       if (code2 == LE || code2 == NE || code2 == ORDERED || code2 == LTGT)
2113         return 1;
2114       break;
2115
2116     case UNGT:
2117       if (code2 == UNGE || code2 == NE)
2118         return 1;
2119       break;
2120
2121     case GT:
2122       if (code2 == GE || code2 == NE || code2 == ORDERED || code2 == LTGT)
2123         return 1;
2124       break;
2125
2126     case GE:
2127     case LE:
2128       if (code2 == ORDERED)
2129         return 1;
2130       break;
2131
2132     case LTGT:
2133       if (code2 == NE || code2 == ORDERED)
2134         return 1;
2135       break;
2136
2137     case LTU:
2138       if (code2 == LEU || code2 == NE)
2139         return 1;
2140       break;
2141
2142     case GTU:
2143       if (code2 == GEU || code2 == NE)
2144         return 1;
2145       break;
2146
2147     case UNORDERED:
2148       if (code2 == NE || code2 == UNEQ || code2 == UNLE || code2 == UNLT
2149           || code2 == UNGE || code2 == UNGT)
2150         return 1;
2151       break;
2152
2153     default:
2154       break;
2155     }
2156
2157   return 0;
2158 }
2159 \f
2160 /* Return 1 if INSN is an unconditional jump and nothing else.  */
2161
2162 int
2163 simplejump_p (insn)
2164      rtx insn;
2165 {
2166   rtx set;
2167
2168   if (GET_CODE (insn) != JUMP_INSN)
2169     return 0;
2170
2171   set = PATTERN (insn);
2172   if (GET_CODE (set) != SET)
2173     {
2174       set = single_set_1 (insn);
2175       if (set == NULL_RTX)
2176         return 0;
2177     }
2178
2179   return (GET_CODE (SET_DEST (set)) == PC
2180           && GET_CODE (SET_SRC (set)) == LABEL_REF);
2181 }
2182
2183 /* Return nonzero if INSN is a (possibly) conditional jump
2184    and nothing more.
2185
2186    Use this function is deprecated, since we need to support combined
2187    branch and compare insns.  Use any_condjump_p instead whenever possible.  */
2188
2189 int
2190 condjump_p (insn)
2191      rtx insn;
2192 {
2193   register rtx x = PATTERN (insn);
2194
2195   if (GET_CODE (x) != SET
2196       || GET_CODE (SET_DEST (x)) != PC)
2197     return 0;
2198
2199   x = SET_SRC (x);
2200   if (GET_CODE (x) == LABEL_REF)
2201     return 1;
2202   else
2203     return (GET_CODE (x) == IF_THEN_ELSE
2204             && ((GET_CODE (XEXP (x, 2)) == PC
2205                  && (GET_CODE (XEXP (x, 1)) == LABEL_REF
2206                      || GET_CODE (XEXP (x, 1)) == RETURN))
2207                 || (GET_CODE (XEXP (x, 1)) == PC
2208                     && (GET_CODE (XEXP (x, 2)) == LABEL_REF
2209                         || GET_CODE (XEXP (x, 2)) == RETURN))));
2210
2211   return 0;
2212 }
2213
2214 /* Return nonzero if INSN is a (possibly) conditional jump inside a
2215    PARALLEL.
2216
2217    Use this function is deprecated, since we need to support combined
2218    branch and compare insns.  Use any_condjump_p instead whenever possible.  */
2219
2220 int
2221 condjump_in_parallel_p (insn)
2222      rtx insn;
2223 {
2224   register rtx x = PATTERN (insn);
2225
2226   if (GET_CODE (x) != PARALLEL)
2227     return 0;
2228   else
2229     x = XVECEXP (x, 0, 0);
2230
2231   if (GET_CODE (x) != SET)
2232     return 0;
2233   if (GET_CODE (SET_DEST (x)) != PC)
2234     return 0;
2235   if (GET_CODE (SET_SRC (x)) == LABEL_REF)
2236     return 1;
2237   if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
2238     return 0;
2239   if (XEXP (SET_SRC (x), 2) == pc_rtx
2240       && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
2241           || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
2242     return 1;
2243   if (XEXP (SET_SRC (x), 1) == pc_rtx
2244       && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
2245           || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
2246     return 1;
2247   return 0;
2248 }
2249
2250 /* Return set of PC, otherwise NULL.  */
2251
2252 rtx
2253 pc_set (insn)
2254      rtx insn;
2255 {
2256   rtx pat;
2257   if (GET_CODE (insn) != JUMP_INSN)
2258     return NULL_RTX;
2259   pat = PATTERN (insn);
2260
2261   /* The set is allowed to appear either as the insn pattern or
2262      the first set in a PARALLEL.  */
2263   if (GET_CODE (pat) == PARALLEL)
2264     pat = XVECEXP (pat, 0, 0);
2265   if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == PC)
2266     return pat;
2267
2268   return NULL_RTX;
2269 }
2270
2271 /* Return true when insn is an unconditional direct jump,
2272    possibly bundled inside a PARALLEL.  */
2273
2274 int
2275 any_uncondjump_p (insn)
2276      rtx insn;
2277 {
2278   rtx x = pc_set (insn);
2279   if (!x)
2280     return 0;
2281   if (GET_CODE (SET_SRC (x)) != LABEL_REF)
2282     return 0;
2283   return 1;
2284 }
2285
2286 /* Return true when insn is a conditional jump.  This function works for
2287    instructions containing PC sets in PARALLELs.  The instruction may have
2288    various other effects so before removing the jump you must verify
2289    onlyjump_p.
2290
2291    Note that unlike condjump_p it returns false for unconditional jumps.  */
2292
2293 int
2294 any_condjump_p (insn)
2295      rtx insn;
2296 {
2297   rtx x = pc_set (insn);
2298   enum rtx_code a, b;
2299
2300   if (!x)
2301     return 0;
2302   if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
2303     return 0;
2304
2305   a = GET_CODE (XEXP (SET_SRC (x), 1));
2306   b = GET_CODE (XEXP (SET_SRC (x), 2));
2307
2308   return ((b == PC && (a == LABEL_REF || a == RETURN))
2309           || (a == PC && (b == LABEL_REF || b == RETURN)));
2310 }
2311
2312 /* Return the label of a conditional jump.  */
2313
2314 rtx
2315 condjump_label (insn)
2316      rtx insn;
2317 {
2318   rtx x = pc_set (insn);
2319
2320   if (!x)
2321     return NULL_RTX;
2322   x = SET_SRC (x);
2323   if (GET_CODE (x) == LABEL_REF)
2324     return x;
2325   if (GET_CODE (x) != IF_THEN_ELSE)
2326     return NULL_RTX;
2327   if (XEXP (x, 2) == pc_rtx && GET_CODE (XEXP (x, 1)) == LABEL_REF)
2328     return XEXP (x, 1);
2329   if (XEXP (x, 1) == pc_rtx && GET_CODE (XEXP (x, 2)) == LABEL_REF)
2330     return XEXP (x, 2);
2331   return NULL_RTX;
2332 }
2333
2334 /* Return true if INSN is a (possibly conditional) return insn.  */
2335
2336 static int
2337 returnjump_p_1 (loc, data)
2338      rtx *loc;
2339      void *data ATTRIBUTE_UNUSED;
2340 {
2341   rtx x = *loc;
2342   return x && GET_CODE (x) == RETURN;
2343 }
2344
2345 int
2346 returnjump_p (insn)
2347      rtx insn;
2348 {
2349   if (GET_CODE (insn) != JUMP_INSN)
2350     return 0;
2351   return for_each_rtx (&PATTERN (insn), returnjump_p_1, NULL);
2352 }
2353
2354 /* Return true if INSN is a jump that only transfers control and
2355    nothing more.  */
2356
2357 int
2358 onlyjump_p (insn)
2359      rtx insn;
2360 {
2361   rtx set;
2362
2363   if (GET_CODE (insn) != JUMP_INSN)
2364     return 0;
2365
2366   set = single_set (insn);
2367   if (set == NULL)
2368     return 0;
2369   if (GET_CODE (SET_DEST (set)) != PC)
2370     return 0;
2371   if (side_effects_p (SET_SRC (set)))
2372     return 0;
2373
2374   return 1;
2375 }
2376
2377 #ifdef HAVE_cc0
2378
2379 /* Return 1 if X is an RTX that does nothing but set the condition codes
2380    and CLOBBER or USE registers.
2381    Return -1 if X does explicitly set the condition codes,
2382    but also does other things.  */
2383
2384 int
2385 sets_cc0_p (x)
2386      rtx x ATTRIBUTE_UNUSED;
2387 {
2388   if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
2389     return 1;
2390   if (GET_CODE (x) == PARALLEL)
2391     {
2392       int i;
2393       int sets_cc0 = 0;
2394       int other_things = 0;
2395       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
2396         {
2397           if (GET_CODE (XVECEXP (x, 0, i)) == SET
2398               && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
2399             sets_cc0 = 1;
2400           else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
2401             other_things = 1;
2402         }
2403       return ! sets_cc0 ? 0 : other_things ? -1 : 1;
2404     }
2405   return 0;
2406 }
2407 #endif
2408 \f
2409 /* Follow any unconditional jump at LABEL;
2410    return the ultimate label reached by any such chain of jumps.
2411    If LABEL is not followed by a jump, return LABEL.
2412    If the chain loops or we can't find end, return LABEL,
2413    since that tells caller to avoid changing the insn.
2414
2415    If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
2416    a USE or CLOBBER.  */
2417
2418 rtx
2419 follow_jumps (label)
2420      rtx label;
2421 {
2422   register rtx insn;
2423   register rtx next;
2424   register rtx value = label;
2425   register int depth;
2426
2427   for (depth = 0;
2428        (depth < 10
2429         && (insn = next_active_insn (value)) != 0
2430         && GET_CODE (insn) == JUMP_INSN
2431         && ((JUMP_LABEL (insn) != 0 && any_uncondjump_p (insn)
2432              && onlyjump_p (insn))
2433             || GET_CODE (PATTERN (insn)) == RETURN)
2434         && (next = NEXT_INSN (insn))
2435         && GET_CODE (next) == BARRIER);
2436        depth++)
2437     {
2438       /* Don't chain through the insn that jumps into a loop
2439          from outside the loop,
2440          since that would create multiple loop entry jumps
2441          and prevent loop optimization.  */
2442       rtx tem;
2443       if (!reload_completed)
2444         for (tem = value; tem != insn; tem = NEXT_INSN (tem))
2445           if (GET_CODE (tem) == NOTE
2446               && (NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG
2447                   /* ??? Optional.  Disables some optimizations, but makes
2448                      gcov output more accurate with -O.  */
2449                   || (flag_test_coverage && NOTE_LINE_NUMBER (tem) > 0)))
2450             return value;
2451
2452       /* If we have found a cycle, make the insn jump to itself.  */
2453       if (JUMP_LABEL (insn) == label)
2454         return label;
2455
2456       tem = next_active_insn (JUMP_LABEL (insn));
2457       if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
2458                   || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
2459         break;
2460
2461       value = JUMP_LABEL (insn);
2462     }
2463   if (depth == 10)
2464     return label;
2465   return value;
2466 }
2467
2468 /* Assuming that field IDX of X is a vector of label_refs,
2469    replace each of them by the ultimate label reached by it.
2470    Return nonzero if a change is made.
2471    If IGNORE_LOOPS is 0, we do not chain across a NOTE_INSN_LOOP_BEG.  */
2472
2473 static int
2474 tension_vector_labels (x, idx)
2475      register rtx x;
2476      register int idx;
2477 {
2478   int changed = 0;
2479   register int i;
2480   for (i = XVECLEN (x, idx) - 1; i >= 0; i--)
2481     {
2482       register rtx olabel = XEXP (XVECEXP (x, idx, i), 0);
2483       register rtx nlabel = follow_jumps (olabel);
2484       if (nlabel && nlabel != olabel)
2485         {
2486           XEXP (XVECEXP (x, idx, i), 0) = nlabel;
2487           ++LABEL_NUSES (nlabel);
2488           if (--LABEL_NUSES (olabel) == 0)
2489             delete_insn (olabel);
2490           changed = 1;
2491         }
2492     }
2493   return changed;
2494 }
2495 \f
2496 /* Find all CODE_LABELs referred to in X, and increment their use counts.
2497    If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
2498    in INSN, then store one of them in JUMP_LABEL (INSN).
2499    If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
2500    referenced in INSN, add a REG_LABEL note containing that label to INSN.
2501    Also, when there are consecutive labels, canonicalize on the last of them.
2502
2503    Note that two labels separated by a loop-beginning note
2504    must be kept distinct if we have not yet done loop-optimization,
2505    because the gap between them is where loop-optimize
2506    will want to move invariant code to.  CROSS_JUMP tells us
2507    that loop-optimization is done with.
2508
2509    Once reload has completed (CROSS_JUMP non-zero), we need not consider
2510    two labels distinct if they are separated by only USE or CLOBBER insns.  */
2511
2512 void
2513 mark_jump_label (x, insn, cross_jump, in_mem)
2514      register rtx x;
2515      rtx insn;
2516      int cross_jump;
2517      int in_mem;
2518 {
2519   register RTX_CODE code = GET_CODE (x);
2520   register int i;
2521   register const char *fmt;
2522
2523   switch (code)
2524     {
2525     case PC:
2526     case CC0:
2527     case REG:
2528     case SUBREG:
2529     case CONST_INT:
2530     case CONST_DOUBLE:
2531     case CLOBBER:
2532     case CALL:
2533       return;
2534
2535     case MEM:
2536       in_mem = 1;
2537       break;
2538
2539     case SYMBOL_REF:
2540       if (!in_mem)
2541         return;
2542
2543       /* If this is a constant-pool reference, see if it is a label.  */
2544       if (CONSTANT_POOL_ADDRESS_P (x))
2545         mark_jump_label (get_pool_constant (x), insn, cross_jump, in_mem);
2546       break;
2547
2548     case LABEL_REF:
2549       {
2550         rtx label = XEXP (x, 0);
2551         rtx olabel = label;
2552         rtx note;
2553         rtx next;
2554
2555         /* Ignore remaining references to unreachable labels that
2556            have been deleted.  */
2557         if (GET_CODE (label) == NOTE
2558             && NOTE_LINE_NUMBER (label) == NOTE_INSN_DELETED_LABEL)
2559           break;
2560
2561         if (GET_CODE (label) != CODE_LABEL)
2562           abort ();
2563
2564         /* Ignore references to labels of containing functions.  */
2565         if (LABEL_REF_NONLOCAL_P (x))
2566           break;
2567
2568         /* If there are other labels following this one,
2569            replace it with the last of the consecutive labels.  */
2570         for (next = NEXT_INSN (label); next; next = NEXT_INSN (next))
2571           {
2572             if (GET_CODE (next) == CODE_LABEL)
2573               label = next;
2574             else if (cross_jump && GET_CODE (next) == INSN
2575                      && (GET_CODE (PATTERN (next)) == USE
2576                          || GET_CODE (PATTERN (next)) == CLOBBER))
2577               continue;
2578             else if (GET_CODE (next) != NOTE)
2579               break;
2580             else if (! cross_jump
2581                      && (NOTE_LINE_NUMBER (next) == NOTE_INSN_LOOP_BEG
2582                          || NOTE_LINE_NUMBER (next) == NOTE_INSN_FUNCTION_END
2583                          /* ??? Optional.  Disables some optimizations, but
2584                             makes gcov output more accurate with -O.  */
2585                          || (flag_test_coverage
2586                              && NOTE_LINE_NUMBER (next) > 0)))
2587               break;
2588           }
2589
2590         XEXP (x, 0) = label;
2591         if (! insn || ! INSN_DELETED_P (insn))
2592           ++LABEL_NUSES (label);
2593
2594         if (insn)
2595           {
2596             if (GET_CODE (insn) == JUMP_INSN)
2597               JUMP_LABEL (insn) = label;
2598
2599             /* If we've changed OLABEL and we had a REG_LABEL note
2600                for it, update it as well.  */
2601             else if (label != olabel
2602                      && (note = find_reg_note (insn, REG_LABEL, olabel)) != 0)
2603               XEXP (note, 0) = label;
2604
2605             /* Otherwise, add a REG_LABEL note for LABEL unless there already
2606                is one.  */
2607             else if (! find_reg_note (insn, REG_LABEL, label))
2608               {
2609                 /* This code used to ignore labels which refered to dispatch
2610                    tables to avoid flow.c generating worse code.
2611
2612                    However, in the presense of global optimizations like
2613                    gcse which call find_basic_blocks without calling
2614                    life_analysis, not recording such labels will lead
2615                    to compiler aborts because of inconsistencies in the
2616                    flow graph.  So we go ahead and record the label.
2617
2618                    It may also be the case that the optimization argument
2619                    is no longer valid because of the more accurate cfg
2620                    we build in find_basic_blocks -- it no longer pessimizes
2621                    code when it finds a REG_LABEL note.  */
2622                 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
2623                                                       REG_NOTES (insn));
2624               }
2625           }
2626         return;
2627       }
2628
2629   /* Do walk the labels in a vector, but not the first operand of an
2630      ADDR_DIFF_VEC.  Don't set the JUMP_LABEL of a vector.  */
2631     case ADDR_VEC:
2632     case ADDR_DIFF_VEC:
2633       if (! INSN_DELETED_P (insn))
2634         {
2635           int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
2636
2637           for (i = 0; i < XVECLEN (x, eltnum); i++)
2638             mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX,
2639                              cross_jump, in_mem);
2640         }
2641       return;
2642
2643     default:
2644       break;
2645     }
2646
2647   fmt = GET_RTX_FORMAT (code);
2648   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2649     {
2650       if (fmt[i] == 'e')
2651         mark_jump_label (XEXP (x, i), insn, cross_jump, in_mem);
2652       else if (fmt[i] == 'E')
2653         {
2654           register int j;
2655           for (j = 0; j < XVECLEN (x, i); j++)
2656             mark_jump_label (XVECEXP (x, i, j), insn, cross_jump, in_mem);
2657         }
2658     }
2659 }
2660
2661 /* If all INSN does is set the pc, delete it,
2662    and delete the insn that set the condition codes for it
2663    if that's what the previous thing was.  */
2664
2665 void
2666 delete_jump (insn)
2667      rtx insn;
2668 {
2669   register rtx set = single_set (insn);
2670
2671   if (set && GET_CODE (SET_DEST (set)) == PC)
2672     delete_computation (insn);
2673 }
2674
2675 /* Verify INSN is a BARRIER and delete it.  */
2676
2677 void
2678 delete_barrier (insn)
2679      rtx insn;
2680 {
2681   if (GET_CODE (insn) != BARRIER)
2682     abort ();
2683
2684   delete_insn (insn);
2685 }
2686
2687 /* Recursively delete prior insns that compute the value (used only by INSN
2688    which the caller is deleting) stored in the register mentioned by NOTE
2689    which is a REG_DEAD note associated with INSN.  */
2690
2691 static void
2692 delete_prior_computation (note, insn)
2693      rtx note;
2694      rtx insn;
2695 {
2696   rtx our_prev;
2697   rtx reg = XEXP (note, 0);
2698
2699   for (our_prev = prev_nonnote_insn (insn);
2700        our_prev && (GET_CODE (our_prev) == INSN
2701                     || GET_CODE (our_prev) == CALL_INSN);
2702        our_prev = prev_nonnote_insn (our_prev))
2703     {
2704       rtx pat = PATTERN (our_prev);
2705
2706       /* If we reach a CALL which is not calling a const function
2707          or the callee pops the arguments, then give up.  */
2708       if (GET_CODE (our_prev) == CALL_INSN
2709           && (! CONST_CALL_P (our_prev)
2710               || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
2711         break;
2712
2713       /* If we reach a SEQUENCE, it is too complex to try to
2714          do anything with it, so give up.  */
2715       if (GET_CODE (pat) == SEQUENCE)
2716         break;
2717
2718       if (GET_CODE (pat) == USE
2719           && GET_CODE (XEXP (pat, 0)) == INSN)
2720         /* reorg creates USEs that look like this.  We leave them
2721            alone because reorg needs them for its own purposes.  */
2722         break;
2723
2724       if (reg_set_p (reg, pat))
2725         {
2726           if (side_effects_p (pat) && GET_CODE (our_prev) != CALL_INSN)
2727             break;
2728
2729           if (GET_CODE (pat) == PARALLEL)
2730             {
2731               /* If we find a SET of something else, we can't
2732                  delete the insn.  */
2733
2734               int i;
2735
2736               for (i = 0; i < XVECLEN (pat, 0); i++)
2737                 {
2738                   rtx part = XVECEXP (pat, 0, i);
2739
2740                   if (GET_CODE (part) == SET
2741                       && SET_DEST (part) != reg)
2742                     break;
2743                 }
2744
2745               if (i == XVECLEN (pat, 0))
2746                 delete_computation (our_prev);
2747             }
2748           else if (GET_CODE (pat) == SET
2749                    && GET_CODE (SET_DEST (pat)) == REG)
2750             {
2751               int dest_regno = REGNO (SET_DEST (pat));
2752               int dest_endregno
2753                 = (dest_regno
2754                    + (dest_regno < FIRST_PSEUDO_REGISTER
2755                       ? HARD_REGNO_NREGS (dest_regno,
2756                                           GET_MODE (SET_DEST (pat))) : 1));
2757               int regno = REGNO (reg);
2758               int endregno
2759                 = (regno
2760                    + (regno < FIRST_PSEUDO_REGISTER
2761                       ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1));
2762
2763               if (dest_regno >= regno
2764                   && dest_endregno <= endregno)
2765                 delete_computation (our_prev);
2766
2767               /* We may have a multi-word hard register and some, but not
2768                  all, of the words of the register are needed in subsequent
2769                  insns.  Write REG_UNUSED notes for those parts that were not
2770                  needed.  */
2771               else if (dest_regno <= regno
2772                        && dest_endregno >= endregno)
2773                 {
2774                   int i;
2775
2776                   REG_NOTES (our_prev)
2777                     = gen_rtx_EXPR_LIST (REG_UNUSED, reg,
2778                                          REG_NOTES (our_prev));
2779
2780                   for (i = dest_regno; i < dest_endregno; i++)
2781                     if (! find_regno_note (our_prev, REG_UNUSED, i))
2782                       break;
2783
2784                   if (i == dest_endregno)
2785                     delete_computation (our_prev);
2786                 }
2787             }
2788
2789           break;
2790         }
2791
2792       /* If PAT references the register that dies here, it is an
2793          additional use.  Hence any prior SET isn't dead.  However, this
2794          insn becomes the new place for the REG_DEAD note.  */
2795       if (reg_overlap_mentioned_p (reg, pat))
2796         {
2797           XEXP (note, 1) = REG_NOTES (our_prev);
2798           REG_NOTES (our_prev) = note;
2799           break;
2800         }
2801     }
2802 }
2803
2804 /* Delete INSN and recursively delete insns that compute values used only
2805    by INSN.  This uses the REG_DEAD notes computed during flow analysis.
2806    If we are running before flow.c, we need do nothing since flow.c will
2807    delete dead code.  We also can't know if the registers being used are
2808    dead or not at this point.
2809
2810    Otherwise, look at all our REG_DEAD notes.  If a previous insn does
2811    nothing other than set a register that dies in this insn, we can delete
2812    that insn as well.
2813
2814    On machines with CC0, if CC0 is used in this insn, we may be able to
2815    delete the insn that set it.  */
2816
2817 static void
2818 delete_computation (insn)
2819      rtx insn;
2820 {
2821   rtx note, next;
2822
2823 #ifdef HAVE_cc0
2824   if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
2825     {
2826       rtx prev = prev_nonnote_insn (insn);
2827       /* We assume that at this stage
2828          CC's are always set explicitly
2829          and always immediately before the jump that
2830          will use them.  So if the previous insn
2831          exists to set the CC's, delete it
2832          (unless it performs auto-increments, etc.).  */
2833       if (prev && GET_CODE (prev) == INSN
2834           && sets_cc0_p (PATTERN (prev)))
2835         {
2836           if (sets_cc0_p (PATTERN (prev)) > 0
2837               && ! side_effects_p (PATTERN (prev)))
2838             delete_computation (prev);
2839           else
2840             /* Otherwise, show that cc0 won't be used.  */
2841             REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_UNUSED,
2842                                                   cc0_rtx, REG_NOTES (prev));
2843         }
2844     }
2845 #endif
2846
2847   for (note = REG_NOTES (insn); note; note = next)
2848     {
2849       next = XEXP (note, 1);
2850
2851       if (REG_NOTE_KIND (note) != REG_DEAD
2852           /* Verify that the REG_NOTE is legitimate.  */
2853           || GET_CODE (XEXP (note, 0)) != REG)
2854         continue;
2855
2856       delete_prior_computation (note, insn);
2857     }
2858
2859   delete_insn (insn);
2860 }
2861 \f
2862 /* Delete insn INSN from the chain of insns and update label ref counts.
2863    May delete some following insns as a consequence; may even delete
2864    a label elsewhere and insns that follow it.
2865
2866    Returns the first insn after INSN that was not deleted.  */
2867
2868 rtx
2869 delete_insn (insn)
2870      register rtx insn;
2871 {
2872   register rtx next = NEXT_INSN (insn);
2873   register rtx prev = PREV_INSN (insn);
2874   register int was_code_label = (GET_CODE (insn) == CODE_LABEL);
2875   register int dont_really_delete = 0;
2876   rtx note;
2877
2878   while (next && INSN_DELETED_P (next))
2879     next = NEXT_INSN (next);
2880
2881   /* This insn is already deleted => return first following nondeleted.  */
2882   if (INSN_DELETED_P (insn))
2883     return next;
2884
2885   if (was_code_label)
2886     remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels);
2887
2888   /* Don't delete user-declared labels.  When optimizing, convert them
2889      to special NOTEs instead.  When not optimizing, leave them alone.  */
2890   if (was_code_label && LABEL_NAME (insn) != 0)
2891     {
2892       if (! optimize)
2893         dont_really_delete = 1;
2894       else if (! dont_really_delete)
2895         {
2896           const char *name = LABEL_NAME (insn);
2897           PUT_CODE (insn, NOTE);
2898           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED_LABEL;
2899           NOTE_SOURCE_FILE (insn) = name;
2900           dont_really_delete = 1;
2901         }
2902     }
2903   else
2904     /* Mark this insn as deleted.  */
2905     INSN_DELETED_P (insn) = 1;
2906
2907   /* If this is an unconditional jump, delete it from the jump chain.  */
2908   if (simplejump_p (insn))
2909     delete_from_jump_chain (insn);
2910
2911   /* If instruction is followed by a barrier,
2912      delete the barrier too.  */
2913
2914   if (next != 0 && GET_CODE (next) == BARRIER)
2915     {
2916       INSN_DELETED_P (next) = 1;
2917       next = NEXT_INSN (next);
2918     }
2919
2920   /* Patch out INSN (and the barrier if any) */
2921
2922   if (! dont_really_delete)
2923     {
2924       if (prev)
2925         {
2926           NEXT_INSN (prev) = next;
2927           if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
2928             NEXT_INSN (XVECEXP (PATTERN (prev), 0,
2929                                 XVECLEN (PATTERN (prev), 0) - 1)) = next;
2930         }
2931
2932       if (next)
2933         {
2934           PREV_INSN (next) = prev;
2935           if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
2936             PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
2937         }
2938
2939       if (prev && NEXT_INSN (prev) == 0)
2940         set_last_insn (prev);
2941     }
2942
2943   /* If deleting a jump, decrement the count of the label,
2944      and delete the label if it is now unused.  */
2945
2946   if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
2947     {
2948       rtx lab = JUMP_LABEL (insn), lab_next;
2949
2950       if (--LABEL_NUSES (lab) == 0)
2951         {
2952           /* This can delete NEXT or PREV,
2953              either directly if NEXT is JUMP_LABEL (INSN),
2954              or indirectly through more levels of jumps.  */
2955           delete_insn (lab);
2956
2957           /* I feel a little doubtful about this loop,
2958              but I see no clean and sure alternative way
2959              to find the first insn after INSN that is not now deleted.
2960              I hope this works.  */
2961           while (next && INSN_DELETED_P (next))
2962             next = NEXT_INSN (next);
2963           return next;
2964         }
2965       else if ((lab_next = next_nonnote_insn (lab)) != NULL
2966                && GET_CODE (lab_next) == JUMP_INSN
2967                && (GET_CODE (PATTERN (lab_next)) == ADDR_VEC
2968                    || GET_CODE (PATTERN (lab_next)) == ADDR_DIFF_VEC))
2969         {
2970           /* If we're deleting the tablejump, delete the dispatch table.
2971              We may not be able to kill the label immediately preceeding
2972              just yet, as it might be referenced in code leading up to
2973              the tablejump.  */
2974           delete_insn (lab_next);
2975         }
2976     }
2977
2978   /* Likewise if we're deleting a dispatch table.  */
2979
2980   if (GET_CODE (insn) == JUMP_INSN
2981       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
2982           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
2983     {
2984       rtx pat = PATTERN (insn);
2985       int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
2986       int len = XVECLEN (pat, diff_vec_p);
2987
2988       for (i = 0; i < len; i++)
2989         if (--LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
2990           delete_insn (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
2991       while (next && INSN_DELETED_P (next))
2992         next = NEXT_INSN (next);
2993       return next;
2994     }
2995
2996   /* Likewise for an ordinary INSN / CALL_INSN with a REG_LABEL note.  */
2997   if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2998     for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2999       if (REG_NOTE_KIND (note) == REG_LABEL
3000           /* This could also be a NOTE_INSN_DELETED_LABEL note.  */
3001           && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
3002         if (--LABEL_NUSES (XEXP (note, 0)) == 0)
3003           delete_insn (XEXP (note, 0));
3004
3005   while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
3006     prev = PREV_INSN (prev);
3007
3008   /* If INSN was a label and a dispatch table follows it,
3009      delete the dispatch table.  The tablejump must have gone already.
3010      It isn't useful to fall through into a table.  */
3011
3012   if (was_code_label
3013       && NEXT_INSN (insn) != 0
3014       && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
3015       && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
3016           || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
3017     next = delete_insn (NEXT_INSN (insn));
3018
3019   /* If INSN was a label, delete insns following it if now unreachable.  */
3020
3021   if (was_code_label && prev && GET_CODE (prev) == BARRIER)
3022     {
3023       register RTX_CODE code;
3024       while (next != 0
3025              && (GET_RTX_CLASS (code = GET_CODE (next)) == 'i'
3026                  || code == NOTE || code == BARRIER
3027                  || (code == CODE_LABEL && INSN_DELETED_P (next))))
3028         {
3029           if (code == NOTE
3030               && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
3031             next = NEXT_INSN (next);
3032           /* Keep going past other deleted labels to delete what follows.  */
3033           else if (code == CODE_LABEL && INSN_DELETED_P (next))
3034             next = NEXT_INSN (next);
3035           else
3036             /* Note: if this deletes a jump, it can cause more
3037                deletion of unreachable code, after a different label.
3038                As long as the value from this recursive call is correct,
3039                this invocation functions correctly.  */
3040             next = delete_insn (next);
3041         }
3042     }
3043
3044   return next;
3045 }
3046
3047 /* Advance from INSN till reaching something not deleted
3048    then return that.  May return INSN itself.  */
3049
3050 rtx
3051 next_nondeleted_insn (insn)
3052      rtx insn;
3053 {
3054   while (INSN_DELETED_P (insn))
3055     insn = NEXT_INSN (insn);
3056   return insn;
3057 }
3058 \f
3059 /* Delete a range of insns from FROM to TO, inclusive.
3060    This is for the sake of peephole optimization, so assume
3061    that whatever these insns do will still be done by a new
3062    peephole insn that will replace them.  */
3063
3064 void
3065 delete_for_peephole (from, to)
3066      register rtx from, to;
3067 {
3068   register rtx insn = from;
3069
3070   while (1)
3071     {
3072       register rtx next = NEXT_INSN (insn);
3073       register rtx prev = PREV_INSN (insn);
3074
3075       if (GET_CODE (insn) != NOTE)
3076         {
3077           INSN_DELETED_P (insn) = 1;
3078
3079           /* Patch this insn out of the chain.  */
3080           /* We don't do this all at once, because we
3081              must preserve all NOTEs.  */
3082           if (prev)
3083             NEXT_INSN (prev) = next;
3084
3085           if (next)
3086             PREV_INSN (next) = prev;
3087         }
3088
3089       if (insn == to)
3090         break;
3091       insn = next;
3092     }
3093
3094   /* Note that if TO is an unconditional jump
3095      we *do not* delete the BARRIER that follows,
3096      since the peephole that replaces this sequence
3097      is also an unconditional jump in that case.  */
3098 }
3099 \f
3100 /* We have determined that INSN is never reached, and are about to
3101    delete it.  Print a warning if the user asked for one.
3102
3103    To try to make this warning more useful, this should only be called
3104    once per basic block not reached, and it only warns when the basic
3105    block contains more than one line from the current function, and
3106    contains at least one operation.  CSE and inlining can duplicate insns,
3107    so it's possible to get spurious warnings from this.  */
3108
3109 void
3110 never_reached_warning (avoided_insn)
3111      rtx avoided_insn;
3112 {
3113   rtx insn;
3114   rtx a_line_note = NULL;
3115   int two_avoided_lines = 0;
3116   int contains_insn = 0;
3117
3118   if (! warn_notreached)
3119     return;
3120
3121   /* Scan forwards, looking at LINE_NUMBER notes, until
3122      we hit a LABEL or we run out of insns.  */
3123
3124   for (insn = avoided_insn; insn != NULL; insn = NEXT_INSN (insn))
3125     {
3126       if (GET_CODE (insn) == CODE_LABEL)
3127         break;
3128       else if (GET_CODE (insn) == NOTE          /* A line number note?  */
3129                && NOTE_LINE_NUMBER (insn) >= 0)
3130         {
3131           if (a_line_note == NULL)
3132             a_line_note = insn;
3133           else
3134             two_avoided_lines |= (NOTE_LINE_NUMBER (a_line_note)
3135                                   != NOTE_LINE_NUMBER (insn));
3136         }
3137       else if (INSN_P (insn))
3138         contains_insn = 1;
3139     }
3140   if (two_avoided_lines && contains_insn)
3141     warning_with_file_and_line (NOTE_SOURCE_FILE (a_line_note),
3142                                 NOTE_LINE_NUMBER (a_line_note),
3143                                 "will never be executed");
3144 }
3145 \f
3146 /* Throughout LOC, redirect OLABEL to NLABEL.  Treat null OLABEL or
3147    NLABEL as a return.  Accrue modifications into the change group.  */
3148
3149 static void
3150 redirect_exp_1 (loc, olabel, nlabel, insn)
3151      rtx *loc;
3152      rtx olabel, nlabel;
3153      rtx insn;
3154 {
3155   register rtx x = *loc;
3156   register RTX_CODE code = GET_CODE (x);
3157   register int i;
3158   register const char *fmt;
3159
3160   if (code == LABEL_REF)
3161     {
3162       if (XEXP (x, 0) == olabel)
3163         {
3164           rtx n;
3165           if (nlabel)
3166             n = gen_rtx_LABEL_REF (VOIDmode, nlabel);
3167           else
3168             n = gen_rtx_RETURN (VOIDmode);
3169
3170           validate_change (insn, loc, n, 1);
3171           return;
3172         }
3173     }
3174   else if (code == RETURN && olabel == 0)
3175     {
3176       x = gen_rtx_LABEL_REF (VOIDmode, nlabel);
3177       if (loc == &PATTERN (insn))
3178         x = gen_rtx_SET (VOIDmode, pc_rtx, x);
3179       validate_change (insn, loc, x, 1);
3180       return;
3181     }
3182
3183   if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
3184       && GET_CODE (SET_SRC (x)) == LABEL_REF
3185       && XEXP (SET_SRC (x), 0) == olabel)
3186     {
3187       validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 1);
3188       return;
3189     }
3190
3191   fmt = GET_RTX_FORMAT (code);
3192   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3193     {
3194       if (fmt[i] == 'e')
3195         redirect_exp_1 (&XEXP (x, i), olabel, nlabel, insn);
3196       else if (fmt[i] == 'E')
3197         {
3198           register int j;
3199           for (j = 0; j < XVECLEN (x, i); j++)
3200             redirect_exp_1 (&XVECEXP (x, i, j), olabel, nlabel, insn);
3201         }
3202     }
3203 }
3204
3205 /* Similar, but apply the change group and report success or failure.  */
3206
3207 static int
3208 redirect_exp (olabel, nlabel, insn)
3209      rtx olabel, nlabel;
3210      rtx insn;
3211 {
3212   rtx *loc;
3213
3214   if (GET_CODE (PATTERN (insn)) == PARALLEL)
3215     loc = &XVECEXP (PATTERN (insn), 0, 0);
3216   else
3217     loc = &PATTERN (insn);
3218
3219   redirect_exp_1 (loc, olabel, nlabel, insn);
3220   if (num_validated_changes () == 0)
3221     return 0;
3222
3223   return apply_change_group ();
3224 }
3225
3226 /* Make JUMP go to NLABEL instead of where it jumps now.  Accrue
3227    the modifications into the change group.  Return false if we did
3228    not see how to do that.  */
3229
3230 int
3231 redirect_jump_1 (jump, nlabel)
3232      rtx jump, nlabel;
3233 {
3234   int ochanges = num_validated_changes ();
3235   rtx *loc;
3236
3237   if (GET_CODE (PATTERN (jump)) == PARALLEL)
3238     loc = &XVECEXP (PATTERN (jump), 0, 0);
3239   else
3240     loc = &PATTERN (jump);
3241
3242   redirect_exp_1 (loc, JUMP_LABEL (jump), nlabel, jump);
3243   return num_validated_changes () > ochanges;
3244 }
3245
3246 /* Make JUMP go to NLABEL instead of where it jumps now.  If the old
3247    jump target label is unused as a result, it and the code following
3248    it may be deleted.
3249
3250    If NLABEL is zero, we are to turn the jump into a (possibly conditional)
3251    RETURN insn.
3252
3253    The return value will be 1 if the change was made, 0 if it wasn't
3254    (this can only occur for NLABEL == 0).  */
3255
3256 int
3257 redirect_jump (jump, nlabel, delete_unused)
3258      rtx jump, nlabel;
3259      int delete_unused;
3260 {
3261   register rtx olabel = JUMP_LABEL (jump);
3262
3263   if (nlabel == olabel)
3264     return 1;
3265
3266   if (! redirect_exp (olabel, nlabel, jump))
3267     return 0;
3268
3269   /* If this is an unconditional branch, delete it from the jump_chain of
3270      OLABEL and add it to the jump_chain of NLABEL (assuming both labels
3271      have UID's in range and JUMP_CHAIN is valid).  */
3272   if (jump_chain && (simplejump_p (jump)
3273                      || GET_CODE (PATTERN (jump)) == RETURN))
3274     {
3275       int label_index = nlabel ? INSN_UID (nlabel) : 0;
3276
3277       delete_from_jump_chain (jump);
3278       if (label_index < max_jump_chain
3279           && INSN_UID (jump) < max_jump_chain)
3280         {
3281           jump_chain[INSN_UID (jump)] = jump_chain[label_index];
3282           jump_chain[label_index] = jump;
3283         }
3284     }
3285
3286   JUMP_LABEL (jump) = nlabel;
3287   if (nlabel)
3288     ++LABEL_NUSES (nlabel);
3289
3290   /* If we're eliding the jump over exception cleanups at the end of a
3291      function, move the function end note so that -Wreturn-type works.  */
3292   if (olabel && nlabel
3293       && NEXT_INSN (olabel)
3294       && GET_CODE (NEXT_INSN (olabel)) == NOTE
3295       && NOTE_LINE_NUMBER (NEXT_INSN (olabel)) == NOTE_INSN_FUNCTION_END)
3296     emit_note_after (NOTE_INSN_FUNCTION_END, nlabel);
3297
3298   if (olabel && --LABEL_NUSES (olabel) == 0 && delete_unused)
3299     delete_insn (olabel);
3300
3301   return 1;
3302 }
3303
3304 /* Invert the jump condition of rtx X contained in jump insn, INSN.
3305    Accrue the modifications into the change group.  */
3306
3307 static void
3308 invert_exp_1 (insn)
3309      rtx insn;
3310 {
3311   register RTX_CODE code;
3312   rtx x = pc_set (insn);
3313
3314   if (!x)
3315     abort ();
3316   x = SET_SRC (x);
3317
3318   code = GET_CODE (x);
3319
3320   if (code == IF_THEN_ELSE)
3321     {
3322       register rtx comp = XEXP (x, 0);
3323       register rtx tem;
3324       enum rtx_code reversed_code;
3325
3326       /* We can do this in two ways:  The preferable way, which can only
3327          be done if this is not an integer comparison, is to reverse
3328          the comparison code.  Otherwise, swap the THEN-part and ELSE-part
3329          of the IF_THEN_ELSE.  If we can't do either, fail.  */
3330
3331       reversed_code = reversed_comparison_code (comp, insn);
3332
3333       if (reversed_code != UNKNOWN)
3334         {
3335           validate_change (insn, &XEXP (x, 0),
3336                            gen_rtx_fmt_ee (reversed_code,
3337                                            GET_MODE (comp), XEXP (comp, 0),
3338                                            XEXP (comp, 1)),
3339                            1);
3340           return;
3341         }
3342
3343       tem = XEXP (x, 1);
3344       validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
3345       validate_change (insn, &XEXP (x, 2), tem, 1);
3346     }
3347   else
3348     abort ();
3349 }
3350
3351 /* Invert the jump condition of conditional jump insn, INSN.
3352
3353    Return 1 if we can do so, 0 if we cannot find a way to do so that
3354    matches a pattern.  */
3355
3356 static int
3357 invert_exp (insn)
3358      rtx insn;
3359 {
3360   invert_exp_1 (insn);
3361   if (num_validated_changes () == 0)
3362     return 0;
3363
3364   return apply_change_group ();
3365 }
3366
3367 /* Invert the condition of the jump JUMP, and make it jump to label
3368    NLABEL instead of where it jumps now.  Accrue changes into the
3369    change group.  Return false if we didn't see how to perform the
3370    inversion and redirection.  */
3371
3372 int
3373 invert_jump_1 (jump, nlabel)
3374      rtx jump, nlabel;
3375 {
3376   int ochanges;
3377
3378   ochanges = num_validated_changes ();
3379   invert_exp_1 (jump);
3380   if (num_validated_changes () == ochanges)
3381     return 0;
3382
3383   return redirect_jump_1 (jump, nlabel);
3384 }
3385
3386 /* Invert the condition of the jump JUMP, and make it jump to label
3387    NLABEL instead of where it jumps now.  Return true if successful.  */
3388
3389 int
3390 invert_jump (jump, nlabel, delete_unused)
3391      rtx jump, nlabel;
3392      int delete_unused;
3393 {
3394   /* We have to either invert the condition and change the label or
3395      do neither.  Either operation could fail.  We first try to invert
3396      the jump. If that succeeds, we try changing the label.  If that fails,
3397      we invert the jump back to what it was.  */
3398
3399   if (! invert_exp (jump))
3400     return 0;
3401
3402   if (redirect_jump (jump, nlabel, delete_unused))
3403     {
3404       /* An inverted jump means that a probability taken becomes a
3405          probability not taken.  Subtract the branch probability from the
3406          probability base to convert it back to a taken probability.  */
3407
3408       rtx note = find_reg_note (jump, REG_BR_PROB, NULL_RTX);
3409       if (note)
3410         XEXP (note, 0) = GEN_INT (REG_BR_PROB_BASE - INTVAL (XEXP (note, 0)));
3411
3412       return 1;
3413     }
3414
3415   if (! invert_exp (jump))
3416     /* This should just be putting it back the way it was.  */
3417     abort ();
3418
3419   return 0;
3420 }
3421
3422 /* Delete the instruction JUMP from any jump chain it might be on.  */
3423
3424 static void
3425 delete_from_jump_chain (jump)
3426      rtx jump;
3427 {
3428   int index;
3429   rtx olabel = JUMP_LABEL (jump);
3430
3431   /* Handle unconditional jumps.  */
3432   if (jump_chain && olabel != 0
3433       && INSN_UID (olabel) < max_jump_chain
3434       && simplejump_p (jump))
3435     index = INSN_UID (olabel);
3436   /* Handle return insns.  */
3437   else if (jump_chain && GET_CODE (PATTERN (jump)) == RETURN)
3438     index = 0;
3439   else
3440     return;
3441
3442   if (jump_chain[index] == jump)
3443     jump_chain[index] = jump_chain[INSN_UID (jump)];
3444   else
3445     {
3446       rtx insn;
3447
3448       for (insn = jump_chain[index];
3449            insn != 0;
3450            insn = jump_chain[INSN_UID (insn)])
3451         if (jump_chain[INSN_UID (insn)] == jump)
3452           {
3453             jump_chain[INSN_UID (insn)] = jump_chain[INSN_UID (jump)];
3454             break;
3455           }
3456     }
3457 }
3458 \f
3459 /* Make jump JUMP jump to label NLABEL, assuming it used to be a tablejump.
3460
3461    If the old jump target label (before the dispatch table) becomes unused,
3462    it and the dispatch table may be deleted.  In that case, find the insn
3463    before the jump references that label and delete it and logical successors
3464    too.  */
3465
3466 static void
3467 redirect_tablejump (jump, nlabel)
3468      rtx jump, nlabel;
3469 {
3470   register rtx olabel = JUMP_LABEL (jump);
3471   rtx *notep, note, next;
3472
3473   /* Add this jump to the jump_chain of NLABEL.  */
3474   if (jump_chain && INSN_UID (nlabel) < max_jump_chain
3475       && INSN_UID (jump) < max_jump_chain)
3476     {
3477       jump_chain[INSN_UID (jump)] = jump_chain[INSN_UID (nlabel)];
3478       jump_chain[INSN_UID (nlabel)] = jump;
3479     }
3480
3481   for (notep = &REG_NOTES (jump), note = *notep; note; note = next)
3482     {
3483       next = XEXP (note, 1);
3484
3485       if (REG_NOTE_KIND (note) != REG_DEAD
3486           /* Verify that the REG_NOTE is legitimate.  */
3487           || GET_CODE (XEXP (note, 0)) != REG
3488           || ! reg_mentioned_p (XEXP (note, 0), PATTERN (jump)))
3489         notep = &XEXP (note, 1);
3490       else
3491         {
3492           delete_prior_computation (note, jump);
3493           *notep = next;
3494         }
3495     }
3496
3497   PATTERN (jump) = gen_jump (nlabel);
3498   JUMP_LABEL (jump) = nlabel;
3499   ++LABEL_NUSES (nlabel);
3500   INSN_CODE (jump) = -1;
3501
3502   if (--LABEL_NUSES (olabel) == 0)
3503     {
3504       delete_labelref_insn (jump, olabel, 0);
3505       delete_insn (olabel);
3506     }
3507 }
3508
3509 /* Find the insn referencing LABEL that is a logical predecessor of INSN.
3510    If we found one, delete it and then delete this insn if DELETE_THIS is
3511    non-zero.  Return non-zero if INSN or a predecessor references LABEL.  */
3512
3513 static int
3514 delete_labelref_insn (insn, label, delete_this)
3515      rtx insn, label;
3516      int delete_this;
3517 {
3518   int deleted = 0;
3519   rtx link;
3520
3521   if (GET_CODE (insn) != NOTE
3522       && reg_mentioned_p (label, PATTERN (insn)))
3523     {
3524       if (delete_this)
3525         {
3526           delete_insn (insn);
3527           deleted = 1;
3528         }
3529       else
3530         return 1;
3531     }
3532
3533   for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
3534     if (delete_labelref_insn (XEXP (link, 0), label, 1))
3535       {
3536         if (delete_this)
3537           {
3538             delete_insn (insn);
3539             deleted = 1;
3540           }
3541         else
3542           return 1;
3543       }
3544
3545   return deleted;
3546 }
3547 \f
3548 /* Like rtx_equal_p except that it considers two REGs as equal
3549    if they renumber to the same value and considers two commutative
3550    operations to be the same if the order of the operands has been
3551    reversed.
3552
3553    ??? Addition is not commutative on the PA due to the weird implicit
3554    space register selection rules for memory addresses.  Therefore, we
3555    don't consider a + b == b + a.
3556
3557    We could/should make this test a little tighter.  Possibly only
3558    disabling it on the PA via some backend macro or only disabling this
3559    case when the PLUS is inside a MEM.  */
3560
3561 int
3562 rtx_renumbered_equal_p (x, y)
3563      rtx x, y;
3564 {
3565   register int i;
3566   register RTX_CODE code = GET_CODE (x);
3567   register const char *fmt;
3568
3569   if (x == y)
3570     return 1;
3571
3572   if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
3573       && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
3574                                   && GET_CODE (SUBREG_REG (y)) == REG)))
3575     {
3576       int reg_x = -1, reg_y = -1;
3577       int word_x = 0, word_y = 0;
3578
3579       if (GET_MODE (x) != GET_MODE (y))
3580         return 0;
3581
3582       /* If we haven't done any renumbering, don't
3583          make any assumptions.  */
3584       if (reg_renumber == 0)
3585         return rtx_equal_p (x, y);
3586
3587       if (code == SUBREG)
3588         {
3589           reg_x = REGNO (SUBREG_REG (x));
3590           word_x = SUBREG_WORD (x);
3591
3592           if (reg_renumber[reg_x] >= 0)
3593             {
3594               reg_x = reg_renumber[reg_x] + word_x;
3595               word_x = 0;
3596             }
3597         }
3598
3599       else
3600         {
3601           reg_x = REGNO (x);
3602           if (reg_renumber[reg_x] >= 0)
3603             reg_x = reg_renumber[reg_x];
3604         }
3605
3606       if (GET_CODE (y) == SUBREG)
3607         {
3608           reg_y = REGNO (SUBREG_REG (y));
3609           word_y = SUBREG_WORD (y);
3610
3611           if (reg_renumber[reg_y] >= 0)
3612             {
3613               reg_y = reg_renumber[reg_y];
3614               word_y = 0;
3615             }
3616         }
3617
3618       else
3619         {
3620           reg_y = REGNO (y);
3621           if (reg_renumber[reg_y] >= 0)
3622             reg_y = reg_renumber[reg_y];
3623         }
3624
3625       return reg_x >= 0 && reg_x == reg_y && word_x == word_y;
3626     }
3627
3628   /* Now we have disposed of all the cases
3629      in which different rtx codes can match.  */
3630   if (code != GET_CODE (y))
3631     return 0;
3632
3633   switch (code)
3634     {
3635     case PC:
3636     case CC0:
3637     case ADDR_VEC:
3638     case ADDR_DIFF_VEC:
3639       return 0;
3640
3641     case CONST_INT:
3642       return INTVAL (x) == INTVAL (y);
3643
3644     case LABEL_REF:
3645       /* We can't assume nonlocal labels have their following insns yet.  */
3646       if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
3647         return XEXP (x, 0) == XEXP (y, 0);
3648
3649       /* Two label-refs are equivalent if they point at labels
3650          in the same position in the instruction stream.  */
3651       return (next_real_insn (XEXP (x, 0))
3652               == next_real_insn (XEXP (y, 0)));
3653
3654     case SYMBOL_REF:
3655       return XSTR (x, 0) == XSTR (y, 0);
3656
3657     case CODE_LABEL:
3658       /* If we didn't match EQ equality above, they aren't the same.  */
3659       return 0;
3660
3661     default:
3662       break;
3663     }
3664
3665   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
3666
3667   if (GET_MODE (x) != GET_MODE (y))
3668     return 0;
3669
3670   /* For commutative operations, the RTX match if the operand match in any
3671      order.  Also handle the simple binary and unary cases without a loop.
3672
3673      ??? Don't consider PLUS a commutative operator; see comments above.  */
3674   if ((code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
3675       && code != PLUS)
3676     return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
3677              && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
3678             || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
3679                 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
3680   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
3681     return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
3682             && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
3683   else if (GET_RTX_CLASS (code) == '1')
3684     return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
3685
3686   /* Compare the elements.  If any pair of corresponding elements
3687      fail to match, return 0 for the whole things.  */
3688
3689   fmt = GET_RTX_FORMAT (code);
3690   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3691     {
3692       register int j;
3693       switch (fmt[i])
3694         {
3695         case 'w':
3696           if (XWINT (x, i) != XWINT (y, i))
3697             return 0;
3698           break;
3699
3700         case 'i':
3701           if (XINT (x, i) != XINT (y, i))
3702             return 0;
3703           break;
3704
3705         case 's':
3706           if (strcmp (XSTR (x, i), XSTR (y, i)))
3707             return 0;
3708           break;
3709
3710         case 'e':
3711           if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
3712             return 0;
3713           break;
3714
3715         case 'u':
3716           if (XEXP (x, i) != XEXP (y, i))
3717             return 0;
3718           /* fall through.  */
3719         case '0':
3720           break;
3721
3722         case 'E':
3723           if (XVECLEN (x, i) != XVECLEN (y, i))
3724             return 0;
3725           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3726             if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
3727               return 0;
3728           break;
3729
3730         default:
3731           abort ();
3732         }
3733     }
3734   return 1;
3735 }
3736 \f
3737 /* If X is a hard register or equivalent to one or a subregister of one,
3738    return the hard register number.  If X is a pseudo register that was not
3739    assigned a hard register, return the pseudo register number.  Otherwise,
3740    return -1.  Any rtx is valid for X.  */
3741
3742 int
3743 true_regnum (x)
3744      rtx x;
3745 {
3746   if (GET_CODE (x) == REG)
3747     {
3748       if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
3749         return reg_renumber[REGNO (x)];
3750       return REGNO (x);
3751     }
3752   if (GET_CODE (x) == SUBREG)
3753     {
3754       int base = true_regnum (SUBREG_REG (x));
3755       if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
3756         return SUBREG_WORD (x) + base;
3757     }
3758   return -1;
3759 }
3760 \f
3761 /* Optimize code of the form:
3762
3763         for (x = a[i]; x; ...)
3764           ...
3765         for (x = a[i]; x; ...)
3766           ...
3767       foo:
3768
3769    Loop optimize will change the above code into
3770
3771         if (x = a[i])
3772           for (;;)
3773              { ...; if (! (x = ...)) break; }
3774         if (x = a[i])
3775           for (;;)
3776              { ...; if (! (x = ...)) break; }
3777       foo:
3778
3779    In general, if the first test fails, the program can branch
3780    directly to `foo' and skip the second try which is doomed to fail.
3781    We run this after loop optimization and before flow analysis.  */
3782
3783 /* When comparing the insn patterns, we track the fact that different
3784    pseudo-register numbers may have been used in each computation.
3785    The following array stores an equivalence -- same_regs[I] == J means
3786    that pseudo register I was used in the first set of tests in a context
3787    where J was used in the second set.  We also count the number of such
3788    pending equivalences.  If nonzero, the expressions really aren't the
3789    same.  */
3790
3791 static int *same_regs;
3792
3793 static int num_same_regs;
3794
3795 /* Track any registers modified between the target of the first jump and
3796    the second jump.  They never compare equal.  */
3797
3798 static char *modified_regs;
3799
3800 /* Record if memory was modified.  */
3801
3802 static int modified_mem;
3803
3804 /* Called via note_stores on each insn between the target of the first
3805    branch and the second branch.  It marks any changed registers.  */
3806
3807 static void
3808 mark_modified_reg (dest, x, data)
3809      rtx dest;
3810      rtx x ATTRIBUTE_UNUSED;
3811      void *data ATTRIBUTE_UNUSED;
3812 {
3813   int regno;
3814   unsigned int i;
3815
3816   if (GET_CODE (dest) == SUBREG)
3817     dest = SUBREG_REG (dest);
3818
3819   if (GET_CODE (dest) == MEM)
3820     modified_mem = 1;
3821
3822   if (GET_CODE (dest) != REG)
3823     return;
3824
3825   regno = REGNO (dest);
3826   if (regno >= FIRST_PSEUDO_REGISTER)
3827     modified_regs[regno] = 1;
3828   else
3829     for (i = 0; i < HARD_REGNO_NREGS (regno, GET_MODE (dest)); i++)
3830       modified_regs[regno + i] = 1;
3831 }
3832
3833 /* F is the first insn in the chain of insns.  */
3834
3835 void
3836 thread_jumps (f, max_reg, flag_before_loop)
3837      rtx f;
3838      int max_reg;
3839      int flag_before_loop;
3840 {
3841   /* Basic algorithm is to find a conditional branch,
3842      the label it may branch to, and the branch after
3843      that label.  If the two branches test the same condition,
3844      walk back from both branch paths until the insn patterns
3845      differ, or code labels are hit.  If we make it back to
3846      the target of the first branch, then we know that the first branch
3847      will either always succeed or always fail depending on the relative
3848      senses of the two branches.  So adjust the first branch accordingly
3849      in this case.  */
3850
3851   rtx label, b1, b2, t1, t2;
3852   enum rtx_code code1, code2;
3853   rtx b1op0, b1op1, b2op0, b2op1;
3854   int changed = 1;
3855   int i;
3856   int *all_reset;
3857   enum rtx_code reversed_code1, reversed_code2;
3858
3859   /* Allocate register tables and quick-reset table.  */
3860   modified_regs = (char *) xmalloc (max_reg * sizeof (char));
3861   same_regs = (int *) xmalloc (max_reg * sizeof (int));
3862   all_reset = (int *) xmalloc (max_reg * sizeof (int));
3863   for (i = 0; i < max_reg; i++)
3864     all_reset[i] = -1;
3865
3866   while (changed)
3867     {
3868       changed = 0;
3869
3870       for (b1 = f; b1; b1 = NEXT_INSN (b1))
3871         {
3872           rtx set;
3873           rtx set2;
3874
3875           /* Get to a candidate branch insn.  */
3876           if (GET_CODE (b1) != JUMP_INSN
3877               || ! any_condjump_p (b1) || JUMP_LABEL (b1) == 0)
3878             continue;
3879
3880           memset (modified_regs, 0, max_reg * sizeof (char));
3881           modified_mem = 0;
3882
3883           memcpy (same_regs, all_reset, max_reg * sizeof (int));
3884           num_same_regs = 0;
3885
3886           label = JUMP_LABEL (b1);
3887
3888           /* Look for a branch after the target.  Record any registers and
3889              memory modified between the target and the branch.  Stop when we
3890              get to a label since we can't know what was changed there.  */
3891           for (b2 = NEXT_INSN (label); b2; b2 = NEXT_INSN (b2))
3892             {
3893               if (GET_CODE (b2) == CODE_LABEL)
3894                 break;
3895
3896               else if (GET_CODE (b2) == JUMP_INSN)
3897                 {
3898                   /* If this is an unconditional jump and is the only use of
3899                      its target label, we can follow it.  */
3900                   if (any_uncondjump_p (b2)
3901                       && onlyjump_p (b2)
3902                       && JUMP_LABEL (b2) != 0
3903                       && LABEL_NUSES (JUMP_LABEL (b2)) == 1)
3904                     {
3905                       b2 = JUMP_LABEL (b2);
3906                       continue;
3907                     }
3908                   else
3909                     break;
3910                 }
3911
3912               if (GET_CODE (b2) != CALL_INSN && GET_CODE (b2) != INSN)
3913                 continue;
3914
3915               if (GET_CODE (b2) == CALL_INSN)
3916                 {
3917                   modified_mem = 1;
3918                   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3919                     if (call_used_regs[i] && ! fixed_regs[i]
3920                         && i != STACK_POINTER_REGNUM
3921                         && i != FRAME_POINTER_REGNUM
3922                         && i != HARD_FRAME_POINTER_REGNUM
3923                         && i != ARG_POINTER_REGNUM)
3924                       modified_regs[i] = 1;
3925                 }
3926
3927               note_stores (PATTERN (b2), mark_modified_reg, NULL);
3928             }
3929
3930           /* Check the next candidate branch insn from the label
3931              of the first.  */
3932           if (b2 == 0
3933               || GET_CODE (b2) != JUMP_INSN
3934               || b2 == b1
3935               || !any_condjump_p (b2)
3936               || !onlyjump_p (b2))
3937             continue;
3938           set = pc_set (b1);
3939           set2 = pc_set (b2);
3940
3941           /* Get the comparison codes and operands, reversing the
3942              codes if appropriate.  If we don't have comparison codes,
3943              we can't do anything.  */
3944           b1op0 = XEXP (XEXP (SET_SRC (set), 0), 0);
3945           b1op1 = XEXP (XEXP (SET_SRC (set), 0), 1);
3946           code1 = GET_CODE (XEXP (SET_SRC (set), 0));
3947           reversed_code1 = code1;
3948           if (XEXP (SET_SRC (set), 1) == pc_rtx)
3949             code1 = reversed_comparison_code (XEXP (SET_SRC (set), 0), b1);
3950           else
3951             reversed_code1 = reversed_comparison_code (XEXP (SET_SRC (set), 0), b1);
3952
3953           b2op0 = XEXP (XEXP (SET_SRC (set2), 0), 0);
3954           b2op1 = XEXP (XEXP (SET_SRC (set2), 0), 1);
3955           code2 = GET_CODE (XEXP (SET_SRC (set2), 0));
3956           reversed_code2 = code2;
3957           if (XEXP (SET_SRC (set2), 1) == pc_rtx)
3958             code2 = reversed_comparison_code (XEXP (SET_SRC (set2), 0), b2);
3959           else
3960             reversed_code2 = reversed_comparison_code (XEXP (SET_SRC (set2), 0), b2);
3961
3962           /* If they test the same things and knowing that B1 branches
3963              tells us whether or not B2 branches, check if we
3964              can thread the branch.  */
3965           if (rtx_equal_for_thread_p (b1op0, b2op0, b2)
3966               && rtx_equal_for_thread_p (b1op1, b2op1, b2)
3967               && (comparison_dominates_p (code1, code2)
3968                   || comparison_dominates_p (code1, reversed_code2)))
3969
3970             {
3971               t1 = prev_nonnote_insn (b1);
3972               t2 = prev_nonnote_insn (b2);
3973
3974               while (t1 != 0 && t2 != 0)
3975                 {
3976                   if (t2 == label)
3977                     {
3978                       /* We have reached the target of the first branch.
3979                          If there are no pending register equivalents,
3980                          we know that this branch will either always
3981                          succeed (if the senses of the two branches are
3982                          the same) or always fail (if not).  */
3983                       rtx new_label;
3984
3985                       if (num_same_regs != 0)
3986                         break;
3987
3988                       if (comparison_dominates_p (code1, code2))
3989                         new_label = JUMP_LABEL (b2);
3990                       else
3991                         new_label = get_label_after (b2);
3992
3993                       if (JUMP_LABEL (b1) != new_label)
3994                         {
3995                           rtx prev = PREV_INSN (new_label);
3996
3997                           if (flag_before_loop
3998                               && GET_CODE (prev) == NOTE
3999                               && NOTE_LINE_NUMBER (prev) == NOTE_INSN_LOOP_BEG)
4000                             {
4001                               /* Don't thread to the loop label.  If a loop
4002                                  label is reused, loop optimization will
4003                                  be disabled for that loop.  */
4004                               new_label = gen_label_rtx ();
4005                               emit_label_after (new_label, PREV_INSN (prev));
4006                             }
4007                           changed |= redirect_jump (b1, new_label, 1);
4008                         }
4009                       break;
4010                     }
4011
4012                   /* If either of these is not a normal insn (it might be
4013                      a JUMP_INSN, CALL_INSN, or CODE_LABEL) we fail.  (NOTEs
4014                      have already been skipped above.)  Similarly, fail
4015                      if the insns are different.  */
4016                   if (GET_CODE (t1) != INSN || GET_CODE (t2) != INSN
4017                       || recog_memoized (t1) != recog_memoized (t2)
4018                       || ! rtx_equal_for_thread_p (PATTERN (t1),
4019                                                    PATTERN (t2), t2))
4020                     break;
4021
4022                   t1 = prev_nonnote_insn (t1);
4023                   t2 = prev_nonnote_insn (t2);
4024                 }
4025             }
4026         }
4027     }
4028
4029   /* Clean up.  */
4030   free (modified_regs);
4031   free (same_regs);
4032   free (all_reset);
4033 }
4034 \f
4035 /* This is like RTX_EQUAL_P except that it knows about our handling of
4036    possibly equivalent registers and knows to consider volatile and
4037    modified objects as not equal.
4038
4039    YINSN is the insn containing Y.  */
4040
4041 int
4042 rtx_equal_for_thread_p (x, y, yinsn)
4043      rtx x, y;
4044      rtx yinsn;
4045 {
4046   register int i;
4047   register int j;
4048   register enum rtx_code code;
4049   register const char *fmt;
4050
4051   code = GET_CODE (x);
4052   /* Rtx's of different codes cannot be equal.  */
4053   if (code != GET_CODE (y))
4054     return 0;
4055
4056   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
4057      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
4058
4059   if (GET_MODE (x) != GET_MODE (y))
4060     return 0;
4061
4062   /* For floating-point, consider everything unequal.  This is a bit
4063      pessimistic, but this pass would only rarely do anything for FP
4064      anyway.  */
4065   if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
4066       && FLOAT_MODE_P (GET_MODE (x)) && ! flag_fast_math)
4067     return 0;
4068
4069   /* For commutative operations, the RTX match if the operand match in any
4070      order.  Also handle the simple binary and unary cases without a loop.  */
4071   if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
4072     return ((rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4073              && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn))
4074             || (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 1), yinsn)
4075                 && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 0), yinsn)));
4076   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
4077     return (rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn)
4078             && rtx_equal_for_thread_p (XEXP (x, 1), XEXP (y, 1), yinsn));
4079   else if (GET_RTX_CLASS (code) == '1')
4080     return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4081
4082   /* Handle special-cases first.  */
4083   switch (code)
4084     {
4085     case REG:
4086       if (REGNO (x) == REGNO (y) && ! modified_regs[REGNO (x)])
4087         return 1;
4088
4089       /* If neither is user variable or hard register, check for possible
4090          equivalence.  */
4091       if (REG_USERVAR_P (x) || REG_USERVAR_P (y)
4092           || REGNO (x) < FIRST_PSEUDO_REGISTER
4093           || REGNO (y) < FIRST_PSEUDO_REGISTER)
4094         return 0;
4095
4096       if (same_regs[REGNO (x)] == -1)
4097         {
4098           same_regs[REGNO (x)] = REGNO (y);
4099           num_same_regs++;
4100
4101           /* If this is the first time we are seeing a register on the `Y'
4102              side, see if it is the last use.  If not, we can't thread the
4103              jump, so mark it as not equivalent.  */
4104           if (REGNO_LAST_UID (REGNO (y)) != INSN_UID (yinsn))
4105             return 0;
4106
4107           return 1;
4108         }
4109       else
4110         return (same_regs[REGNO (x)] == (int) REGNO (y));
4111
4112       break;
4113
4114     case MEM:
4115       /* If memory modified or either volatile, not equivalent.
4116          Else, check address.  */
4117       if (modified_mem || MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4118         return 0;
4119
4120       return rtx_equal_for_thread_p (XEXP (x, 0), XEXP (y, 0), yinsn);
4121
4122     case ASM_INPUT:
4123       if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
4124         return 0;
4125
4126       break;
4127
4128     case SET:
4129       /* Cancel a pending `same_regs' if setting equivalenced registers.
4130          Then process source.  */
4131       if (GET_CODE (SET_DEST (x)) == REG
4132           && GET_CODE (SET_DEST (y)) == REG)
4133         {
4134           if (same_regs[REGNO (SET_DEST (x))] == (int) REGNO (SET_DEST (y)))
4135             {
4136               same_regs[REGNO (SET_DEST (x))] = -1;
4137               num_same_regs--;
4138             }
4139           else if (REGNO (SET_DEST (x)) != REGNO (SET_DEST (y)))
4140             return 0;
4141         }
4142       else
4143         {
4144           if (rtx_equal_for_thread_p (SET_DEST (x), SET_DEST (y), yinsn) == 0)
4145             return 0;
4146         }
4147
4148       return rtx_equal_for_thread_p (SET_SRC (x), SET_SRC (y), yinsn);
4149
4150     case LABEL_REF:
4151       return XEXP (x, 0) == XEXP (y, 0);
4152
4153     case SYMBOL_REF:
4154       return XSTR (x, 0) == XSTR (y, 0);
4155
4156     default:
4157       break;
4158     }
4159
4160   if (x == y)
4161     return 1;
4162
4163   fmt = GET_RTX_FORMAT (code);
4164   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4165     {
4166       switch (fmt[i])
4167         {
4168         case 'w':
4169           if (XWINT (x, i) != XWINT (y, i))
4170             return 0;
4171           break;
4172
4173         case 'n':
4174         case 'i':
4175           if (XINT (x, i) != XINT (y, i))
4176             return 0;
4177           break;
4178
4179         case 'V':
4180         case 'E':
4181           /* Two vectors must have the same length.  */
4182           if (XVECLEN (x, i) != XVECLEN (y, i))
4183             return 0;
4184
4185           /* And the corresponding elements must match.  */
4186           for (j = 0; j < XVECLEN (x, i); j++)
4187             if (rtx_equal_for_thread_p (XVECEXP (x, i, j),
4188                                         XVECEXP (y, i, j), yinsn) == 0)
4189               return 0;
4190           break;
4191
4192         case 'e':
4193           if (rtx_equal_for_thread_p (XEXP (x, i), XEXP (y, i), yinsn) == 0)
4194             return 0;
4195           break;
4196
4197         case 'S':
4198         case 's':
4199           if (strcmp (XSTR (x, i), XSTR (y, i)))
4200             return 0;
4201           break;
4202
4203         case 'u':
4204           /* These are just backpointers, so they don't matter.  */
4205           break;
4206
4207         case '0':
4208         case 't':
4209           break;
4210
4211           /* It is believed that rtx's at this level will never
4212              contain anything but integers and other rtx's,
4213              except for within LABEL_REFs and SYMBOL_REFs.  */
4214         default:
4215           abort ();
4216         }
4217     }
4218   return 1;
4219 }