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