]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/jump.c
* sh.md (insv): Provide byte offsets for gen_rtx_SUBREG.
[thirdparty/gcc.git] / gcc / jump.c
CommitLineData
5924de0b 1/* Optimize jump instructions, for GNU compiler.
c773fc10 2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5377f687 3 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5924de0b 4
f12b58b3 5This file is part of GCC.
5924de0b 6
f12b58b3 7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
5924de0b 11
f12b58b3 12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
5924de0b 16
17You should have received a copy of the GNU General Public License
f12b58b3 18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
5924de0b 21
fc4eaab7 22/* This is the pathetic reminder of old fame of the jump-optimization pass
23 of the compiler. Now it contains basically set of utility function to
24 operate with jumps.
5924de0b 25
26 Each CODE_LABEL has a count of the times it is used
27 stored in the LABEL_NUSES internal field, and each JUMP_INSN
28 has one label that it refers to stored in the
29 JUMP_LABEL internal field. With this we can detect labels that
30 become unused because of the deletion of all the jumps that
31 formerly used them. The JUMP_LABEL info is sometimes looked
32 at by later passes.
33
5924de0b 34 The subroutines delete_insn, redirect_jump, and invert_jump are used
35 from other passes as well. */
36
37#include "config.h"
405711de 38#include "system.h"
5924de0b 39#include "rtl.h"
7953c610 40#include "tm_p.h"
5924de0b 41#include "flags.h"
42#include "hard-reg-set.h"
43#include "regs.h"
5924de0b 44#include "insn-config.h"
fe3b47be 45#include "insn-attr.h"
0dbd1c74 46#include "recog.h"
0a893c29 47#include "function.h"
fa9157fe 48#include "expr.h"
5924de0b 49#include "real.h"
485aaaaf 50#include "except.h"
ce1fd7fc 51#include "toplev.h"
75eb327c 52#include "reload.h"
13488c51 53#include "predict.h"
5924de0b 54
5924de0b 55/* Optimize jump y; x: ... y: jumpif... x?
56 Don't know if it is worth bothering with. */
57/* Optimize two cases of conditional jump to conditional jump?
58 This can never delete any instruction or make anything dead,
59 or even change what is live at any point.
60 So perhaps let combiner do it. */
61
38b9004f 62static int init_label_info PARAMS ((rtx));
bf73fcf4 63static void mark_all_labels PARAMS ((rtx));
38b9004f 64static int duplicate_loop_exit_test PARAMS ((rtx));
38b9004f 65static void delete_computation PARAMS ((rtx));
a8b5d014 66static void redirect_exp_1 PARAMS ((rtx *, rtx, rtx, rtx));
ba08b7e7 67static int redirect_exp PARAMS ((rtx, rtx, rtx));
68static void invert_exp_1 PARAMS ((rtx));
69static int invert_exp PARAMS ((rtx));
38b9004f 70static int returnjump_p_1 PARAMS ((rtx *, void *));
71static void delete_prior_computation PARAMS ((rtx, rtx));
60ecc450 72\f
8b946ced 73/* Alternate entry into the jump optimizer. This entry point only rebuilds
74 the JUMP_LABEL field in jumping insns and REG_LABEL notes in non-jumping
75 instructions. */
76void
77rebuild_jump_labels (f)
78 rtx f;
79{
19cb6b50 80 rtx insn;
5924de0b 81 int max_uid = 0;
5924de0b 82
e8d75e01 83 max_uid = init_label_info (f) + 1;
5924de0b 84
bf73fcf4 85 mark_all_labels (f);
5924de0b 86
cbd914e1 87 /* Keep track of labels used from static data; we don't track them
88 closely enough to delete them here, so make sure their reference
89 count doesn't drop to zero. */
5924de0b 90
91 for (insn = forced_labels; insn; insn = XEXP (insn, 1))
cbd914e1 92 if (GET_CODE (XEXP (insn, 0)) == CODE_LABEL)
93 LABEL_NUSES (XEXP (insn, 0))++;
5924de0b 94
485aaaaf 95 /* Keep track of labels used for marking handlers for exception
96 regions; they cannot usually be deleted. */
97
98 for (insn = exception_handler_labels; insn; insn = XEXP (insn, 1))
7d27371b 99 if (GET_CODE (XEXP (insn, 0)) == CODE_LABEL)
100 LABEL_NUSES (XEXP (insn, 0))++;
fc4eaab7 101}
102\f
fb3c15bc 103/* Some old code expects exactly one BARRIER as the NEXT_INSN of a
104 non-fallthru insn. This is not generally true, as multiple barriers
105 may have crept in, or the BARRIER may be separated from the last
106 real insn by one or more NOTEs.
107
108 This simple pass moves barriers and removes duplicates so that the
109 old code is happy.
110 */
111void
112cleanup_barriers ()
113{
114 rtx insn, next, prev;
115 for (insn = get_insns (); insn; insn = next)
116 {
117 next = NEXT_INSN (insn);
118 if (GET_CODE (insn) == BARRIER)
119 {
120 prev = prev_nonnote_insn (insn);
121 if (GET_CODE (prev) == BARRIER)
122 delete_barrier (insn);
123 else if (prev != PREV_INSN (insn))
124 reorder_insns (insn, insn, prev);
125 }
126 }
127}
128\f
fc4eaab7 129void
130copy_loop_headers (f)
131 rtx f;
132{
19cb6b50 133 rtx insn, next;
5924de0b 134 /* Now iterate optimizing jumps until nothing changes over one pass. */
fc4eaab7 135 for (insn = f; insn; insn = next)
5924de0b 136 {
fc4eaab7 137 rtx temp, temp1;
5924de0b 138
fc4eaab7 139 next = NEXT_INSN (insn);
7014838c 140
fc4eaab7 141 /* See if this is a NOTE_INSN_LOOP_BEG followed by an unconditional
142 jump. Try to optimize by duplicating the loop exit test if so.
143 This is only safe immediately after regscan, because it uses
144 the values of regno_first_uid and regno_last_uid. */
145 if (GET_CODE (insn) == NOTE
146 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
147 && (temp1 = next_nonnote_insn (insn)) != 0
148 && any_uncondjump_p (temp1) && onlyjump_p (temp1))
149 {
150 temp = PREV_INSN (insn);
151 if (duplicate_loop_exit_test (insn))
5924de0b 152 {
fc4eaab7 153 next = NEXT_INSN (temp);
5924de0b 154 }
5924de0b 155 }
5924de0b 156 }
fc4eaab7 157}
5924de0b 158
fc4eaab7 159void
160purge_line_number_notes (f)
161 rtx f;
162{
163 rtx last_note = 0;
164 rtx insn;
5924de0b 165 /* Delete extraneous line number notes.
166 Note that two consecutive notes for different lines are not really
167 extraneous. There should be some indication where that line belonged,
168 even if it became empty. */
169
fc4eaab7 170 for (insn = f; insn; insn = NEXT_INSN (insn))
171 if (GET_CODE (insn) == NOTE)
172 {
173 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
174 /* Any previous line note was for the prologue; gdb wants a new
175 note after the prologue even if it is for the same line. */
176 last_note = NULL_RTX;
177 else if (NOTE_LINE_NUMBER (insn) >= 0)
178 {
179 /* Delete this note if it is identical to previous note. */
180 if (last_note
181 && NOTE_SOURCE_FILE (insn) == NOTE_SOURCE_FILE (last_note)
182 && NOTE_LINE_NUMBER (insn) == NOTE_LINE_NUMBER (last_note))
183 {
e4bf866d 184 delete_related_insns (insn);
fc4eaab7 185 continue;
186 }
5924de0b 187
fc4eaab7 188 last_note = insn;
189 }
190 }
e8d75e01 191}
192\f
193/* Initialize LABEL_NUSES and JUMP_LABEL fields. Delete any REG_LABEL
194 notes whose labels don't occur in the insn any more. Returns the
195 largest INSN_UID found. */
196static int
197init_label_info (f)
198 rtx f;
199{
200 int largest_uid = 0;
201 rtx insn;
202
203 for (insn = f; insn; insn = NEXT_INSN (insn))
204 {
205 if (GET_CODE (insn) == CODE_LABEL)
206 LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
207 else if (GET_CODE (insn) == JUMP_INSN)
208 JUMP_LABEL (insn) = 0;
209 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
210 {
211 rtx note, next;
212
213 for (note = REG_NOTES (insn); note; note = next)
214 {
215 next = XEXP (note, 1);
216 if (REG_NOTE_KIND (note) == REG_LABEL
217 && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
218 remove_note (insn, note);
219 }
220 }
221 if (INSN_UID (insn) > largest_uid)
222 largest_uid = INSN_UID (insn);
223 }
224
225 return largest_uid;
226}
227
e8d75e01 228/* Mark the label each jump jumps to.
fc4eaab7 229 Combine consecutive labels, and count uses of labels. */
e8d75e01 230
231static void
bf73fcf4 232mark_all_labels (f)
e8d75e01 233 rtx f;
e8d75e01 234{
235 rtx insn;
236
237 for (insn = f; insn; insn = NEXT_INSN (insn))
9204e736 238 if (INSN_P (insn))
e8d75e01 239 {
0e3150ce 240 if (GET_CODE (insn) == CALL_INSN
241 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
242 {
bf73fcf4 243 mark_all_labels (XEXP (PATTERN (insn), 0));
244 mark_all_labels (XEXP (PATTERN (insn), 1));
245 mark_all_labels (XEXP (PATTERN (insn), 2));
af29e037 246
247 /* Canonicalize the tail recursion label attached to the
248 CALL_PLACEHOLDER insn. */
249 if (XEXP (PATTERN (insn), 3))
250 {
251 rtx label_ref = gen_rtx_LABEL_REF (VOIDmode,
252 XEXP (PATTERN (insn), 3));
bf73fcf4 253 mark_jump_label (label_ref, insn, 0);
af29e037 254 XEXP (PATTERN (insn), 3) = XEXP (label_ref, 0);
255 }
256
0e3150ce 257 continue;
258 }
7113a566 259
bf73fcf4 260 mark_jump_label (PATTERN (insn), insn, 0);
e8d75e01 261 if (! INSN_DELETED_P (insn) && GET_CODE (insn) == JUMP_INSN)
262 {
d3ff0f75 263 /* When we know the LABEL_REF contained in a REG used in
264 an indirect jump, we'll have a REG_LABEL note so that
265 flow can tell where it's going. */
266 if (JUMP_LABEL (insn) == 0)
267 {
268 rtx label_note = find_reg_note (insn, REG_LABEL, NULL_RTX);
269 if (label_note)
270 {
271 /* But a LABEL_REF around the REG_LABEL note, so
272 that we can canonicalize it. */
273 rtx label_ref = gen_rtx_LABEL_REF (VOIDmode,
274 XEXP (label_note, 0));
275
bf73fcf4 276 mark_jump_label (label_ref, insn, 0);
d3ff0f75 277 XEXP (label_note, 0) = XEXP (label_ref, 0);
278 JUMP_LABEL (insn) = XEXP (label_note, 0);
279 }
280 }
e8d75e01 281 }
282 }
283}
284
5924de0b 285/* LOOP_START is a NOTE_INSN_LOOP_BEG note that is followed by an unconditional
286 jump. Assume that this unconditional jump is to the exit test code. If
287 the code is sufficiently simple, make a copy of it before INSN,
288 followed by a jump to the exit of the loop. Then delete the unconditional
289 jump after INSN.
290
5924de0b 291 Return 1 if we made the change, else 0.
292
293 This is only safe immediately after a regscan pass because it uses the
294 values of regno_first_uid and regno_last_uid. */
295
296static int
297duplicate_loop_exit_test (loop_start)
298 rtx loop_start;
299{
3a348c93 300 rtx insn, set, reg, p, link;
4e717234 301 rtx copy = 0, first_copy = 0;
5924de0b 302 int num_insns = 0;
303 rtx exitcode = NEXT_INSN (JUMP_LABEL (next_nonnote_insn (loop_start)));
304 rtx lastexit;
305 int max_reg = max_reg_num ();
306 rtx *reg_map = 0;
ea918419 307 rtx loop_pre_header_label;
5924de0b 308
309 /* Scan the exit code. We do not perform this optimization if any insn:
310
311 is a CALL_INSN
312 is a CODE_LABEL
313 has a REG_RETVAL or REG_LIBCALL note (hard to adjust)
314 is a NOTE_INSN_LOOP_BEG because this means we have a nested loop
315 is a NOTE_INSN_BLOCK_{BEG,END} because duplicating these notes
7665c376 316 is not valid.
17b1950c 317
318 We also do not do this if we find an insn with ASM_OPERANDS. While
319 this restriction should not be necessary, copying an insn with
320 ASM_OPERANDS can confuse asm_noperands in some cases.
5924de0b 321
322 Also, don't do this if the exit code is more than 20 insns. */
323
324 for (insn = exitcode;
325 insn
326 && ! (GET_CODE (insn) == NOTE
327 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END);
328 insn = NEXT_INSN (insn))
329 {
330 switch (GET_CODE (insn))
331 {
332 case CODE_LABEL:
333 case CALL_INSN:
334 return 0;
335 case NOTE:
801acb81 336 /* We could be in front of the wrong NOTE_INSN_LOOP_END if there is
337 a jump immediately after the loop start that branches outside
338 the loop but within an outer loop, near the exit test.
339 If we copied this exit test and created a phony
340 NOTE_INSN_LOOP_VTOP, this could make instructions immediately
341 before the exit test look like these could be safely moved
342 out of the loop even if they actually may be never executed.
343 This can be avoided by checking here for NOTE_INSN_LOOP_CONT. */
344
5924de0b 345 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
801acb81 346 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
5924de0b 347 return 0;
ec6be638 348
349 if (optimize < 2
350 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
351 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
352 /* If we were to duplicate this code, we would not move
353 the BLOCK notes, and so debugging the moved code would
354 be difficult. Thus, we only move the code with -O2 or
355 higher. */
356 return 0;
357
5924de0b 358 break;
359 case JUMP_INSN:
360 case INSN:
7665c376 361 /* The code below would grossly mishandle REG_WAS_0 notes,
362 so get rid of them here. */
363 while ((p = find_reg_note (insn, REG_WAS_0, NULL_RTX)) != 0)
364 remove_note (insn, p);
5924de0b 365 if (++num_insns > 20
1bb04728 366 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
928d57e3 367 || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
5924de0b 368 return 0;
369 break;
0dbd1c74 370 default:
371 break;
5924de0b 372 }
373 }
374
375 /* Unless INSN is zero, we can do the optimization. */
376 if (insn == 0)
377 return 0;
378
379 lastexit = insn;
380
381 /* See if any insn sets a register only used in the loop exit code and
382 not a user variable. If so, replace it with a new register. */
383 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
384 if (GET_CODE (insn) == INSN
385 && (set = single_set (insn)) != 0
3a348c93 386 && ((reg = SET_DEST (set), GET_CODE (reg) == REG)
387 || (GET_CODE (reg) == SUBREG
388 && (reg = SUBREG_REG (reg), GET_CODE (reg) == REG)))
389 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
394685a4 390 && REGNO_FIRST_UID (REGNO (reg)) == INSN_UID (insn))
5924de0b 391 {
392 for (p = NEXT_INSN (insn); p != lastexit; p = NEXT_INSN (p))
394685a4 393 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (p))
5924de0b 394 break;
395
396 if (p != lastexit)
397 {
398 /* We can do the replacement. Allocate reg_map if this is the
399 first replacement we found. */
400 if (reg_map == 0)
8b861be4 401 reg_map = (rtx *) xcalloc (max_reg, sizeof (rtx));
5924de0b 402
3a348c93 403 REG_LOOP_TEST_P (reg) = 1;
5924de0b 404
3a348c93 405 reg_map[REGNO (reg)] = gen_reg_rtx (GET_MODE (reg));
5924de0b 406 }
407 }
ea918419 408 loop_pre_header_label = gen_label_rtx ();
5924de0b 409
410 /* Now copy each insn. */
411 for (insn = exitcode; insn != lastexit; insn = NEXT_INSN (insn))
4e717234 412 {
413 switch (GET_CODE (insn))
414 {
415 case BARRIER:
416 copy = emit_barrier_before (loop_start);
417 break;
418 case NOTE:
419 /* Only copy line-number notes. */
420 if (NOTE_LINE_NUMBER (insn) >= 0)
421 {
422 copy = emit_note_before (NOTE_LINE_NUMBER (insn), loop_start);
423 NOTE_SOURCE_FILE (copy) = NOTE_SOURCE_FILE (insn);
424 }
425 break;
7113a566 426
4e717234 427 case INSN:
928d57e3 428 copy = emit_insn_before (copy_insn (PATTERN (insn)), loop_start);
4e717234 429 if (reg_map)
430 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
7113a566 431
bf73fcf4 432 mark_jump_label (PATTERN (copy), copy, 0);
7113a566 433
4e717234 434 /* Copy all REG_NOTES except REG_LABEL since mark_jump_label will
435 make them. */
436 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
437 if (REG_NOTE_KIND (link) != REG_LABEL)
74b0991d 438 {
439 if (GET_CODE (link) == EXPR_LIST)
440 REG_NOTES (copy)
441 = copy_insn_1 (gen_rtx_EXPR_LIST (REG_NOTE_KIND (link),
442 XEXP (link, 0),
443 REG_NOTES (copy)));
444 else
445 REG_NOTES (copy)
446 = copy_insn_1 (gen_rtx_INSN_LIST (REG_NOTE_KIND (link),
447 XEXP (link, 0),
448 REG_NOTES (copy)));
449 }
450
4e717234 451 if (reg_map && REG_NOTES (copy))
452 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
453 break;
7113a566 454
4e717234 455 case JUMP_INSN:
7113a566 456 copy = emit_jump_insn_before (copy_insn (PATTERN (insn)),
457 loop_start);
4e717234 458 if (reg_map)
459 replace_regs (PATTERN (copy), reg_map, max_reg, 1);
bf73fcf4 460 mark_jump_label (PATTERN (copy), copy, 0);
4e717234 461 if (REG_NOTES (insn))
462 {
928d57e3 463 REG_NOTES (copy) = copy_insn_1 (REG_NOTES (insn));
4e717234 464 if (reg_map)
465 replace_regs (REG_NOTES (copy), reg_map, max_reg, 1);
466 }
7113a566 467
13488c51 468 /* Predict conditional jump that do make loop looping as taken.
469 Other jumps are probably exit conditions, so predict
470 them as untaken. */
471 if (any_condjump_p (copy))
472 {
473 rtx label = JUMP_LABEL (copy);
474 if (label)
475 {
4361b500 476 /* The jump_insn after loop_start should be followed
477 by barrier and loopback label. */
478 if (prev_nonnote_insn (label)
ea918419 479 && (prev_nonnote_insn (prev_nonnote_insn (label))
480 == next_nonnote_insn (loop_start)))
481 {
482 predict_insn_def (copy, PRED_LOOP_HEADER, TAKEN);
483 /* To keep pre-header, we need to redirect all loop
484 entrances before the LOOP_BEG note. */
485 redirect_jump (copy, loop_pre_header_label, 0);
486 }
13488c51 487 else
488 predict_insn_def (copy, PRED_LOOP_HEADER, NOT_TAKEN);
489 }
490 }
4e717234 491 break;
7113a566 492
4e717234 493 default:
494 abort ();
495 }
5924de0b 496
4e717234 497 /* Record the first insn we copied. We need it so that we can
498 scan the copied insns for new pseudo registers. */
499 if (! first_copy)
500 first_copy = copy;
501 }
5924de0b 502
503 /* Now clean up by emitting a jump to the end label and deleting the jump
504 at the start of the loop. */
b8778d98 505 if (! copy || GET_CODE (copy) != BARRIER)
5924de0b 506 {
507 copy = emit_jump_insn_before (gen_jump (get_label_after (insn)),
508 loop_start);
4e717234 509
510 /* Record the first insn we copied. We need it so that we can
511 scan the copied insns for new pseudo registers. This may not
512 be strictly necessary since we should have copied at least one
513 insn above. But I am going to be safe. */
514 if (! first_copy)
515 first_copy = copy;
516
bf73fcf4 517 mark_jump_label (PATTERN (copy), copy, 0);
5924de0b 518 emit_barrier_before (loop_start);
519 }
520
ea918419 521 emit_label_before (loop_pre_header_label, loop_start);
522
4e717234 523 /* Now scan from the first insn we copied to the last insn we copied
524 (copy) for new pseudo registers. Do this after the code to jump to
525 the end label since that might create a new pseudo too. */
526 reg_scan_update (first_copy, copy, max_reg);
527
5924de0b 528 /* Mark the exit code as the virtual top of the converted loop. */
529 emit_note_before (NOTE_INSN_LOOP_VTOP, exitcode);
530
e4bf866d 531 delete_related_insns (next_nonnote_insn (loop_start));
7113a566 532
8b861be4 533 /* Clean up. */
534 if (reg_map)
535 free (reg_map);
92d3c3ad 536
5924de0b 537 return 1;
538}
539\f
74b0991d 540/* Move all block-beg, block-end, loop-beg, loop-cont, loop-vtop, loop-end,
3612339f 541 notes between START and END out before START. START and END may be such
542 notes. Returns the values of the new starting and ending insns, which
87dc0300 543 may be different if the original ones were such notes.
544 Return true if there were only such notes and no real instructions. */
5924de0b 545
87dc0300 546bool
3612339f 547squeeze_notes (startp, endp)
548 rtx* startp;
549 rtx* endp;
5924de0b 550{
3612339f 551 rtx start = *startp;
552 rtx end = *endp;
553
5924de0b 554 rtx insn;
555 rtx next;
3612339f 556 rtx last = NULL;
557 rtx past_end = NEXT_INSN (end);
5924de0b 558
3612339f 559 for (insn = start; insn != past_end; insn = next)
5924de0b 560 {
561 next = NEXT_INSN (insn);
562 if (GET_CODE (insn) == NOTE
563 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END
564 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
565 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
566 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END
567 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT
a196d160 568 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_VTOP))
5924de0b 569 {
d2d45541 570 if (insn == start)
571 start = next;
572 else
573 {
574 rtx prev = PREV_INSN (insn);
575 PREV_INSN (insn) = PREV_INSN (start);
576 NEXT_INSN (insn) = start;
577 NEXT_INSN (PREV_INSN (insn)) = insn;
578 PREV_INSN (NEXT_INSN (insn)) = insn;
579 NEXT_INSN (prev) = next;
580 PREV_INSN (next) = prev;
581 }
5924de0b 582 }
3612339f 583 else
584 last = insn;
5924de0b 585 }
d2d45541 586
87dc0300 587 /* There were no real instructions. */
3612339f 588 if (start == past_end)
87dc0300 589 return true;
3612339f 590
591 end = last;
592
593 *startp = start;
594 *endp = end;
87dc0300 595 return false;
5924de0b 596}
597\f
5924de0b 598/* Return the label before INSN, or put a new label there. */
599
600rtx
601get_label_before (insn)
602 rtx insn;
603{
604 rtx label;
605
606 /* Find an existing label at this point
607 or make a new one if there is none. */
608 label = prev_nonnote_insn (insn);
609
610 if (label == 0 || GET_CODE (label) != CODE_LABEL)
611 {
612 rtx prev = PREV_INSN (insn);
613
5924de0b 614 label = gen_label_rtx ();
615 emit_label_after (label, prev);
616 LABEL_NUSES (label) = 0;
617 }
618 return label;
619}
620
621/* Return the label after INSN, or put a new label there. */
622
623rtx
624get_label_after (insn)
625 rtx insn;
626{
627 rtx label;
628
629 /* Find an existing label at this point
630 or make a new one if there is none. */
631 label = next_nonnote_insn (insn);
632
633 if (label == 0 || GET_CODE (label) != CODE_LABEL)
634 {
5924de0b 635 label = gen_label_rtx ();
636 emit_label_after (label, insn);
637 LABEL_NUSES (label) = 0;
638 }
639 return label;
640}
641\f
fa8b3d85 642/* Given a comparison (CODE ARG0 ARG1), inside an insn, INSN, return a code
8e98892d 643 of reversed comparison if it is possible to do so. Otherwise return UNKNOWN.
644 UNKNOWN may be returned in case we are having CC_MODE compare and we don't
645 know whether it's source is floating point or integer comparison. Machine
646 description should define REVERSIBLE_CC_MODE and REVERSE_CONDITION macros
647 to help this function avoid overhead in these cases. */
648enum rtx_code
649reversed_comparison_code_parts (code, arg0, arg1, insn)
650 rtx insn, arg0, arg1;
651 enum rtx_code code;
5924de0b 652{
8e98892d 653 enum machine_mode mode;
5924de0b 654
655 /* If this is not actually a comparison, we can't reverse it. */
8e98892d 656 if (GET_RTX_CLASS (code) != '<')
657 return UNKNOWN;
658
659 mode = GET_MODE (arg0);
660 if (mode == VOIDmode)
661 mode = GET_MODE (arg1);
662
663 /* First see if machine description supply us way to reverse the comparison.
664 Give it priority over everything else to allow machine description to do
665 tricks. */
666#ifdef REVERSIBLE_CC_MODE
0ec244e1 667 if (GET_MODE_CLASS (mode) == MODE_CC
8e98892d 668 && REVERSIBLE_CC_MODE (mode))
669 {
670#ifdef REVERSE_CONDITION
85fc0ad1 671 return REVERSE_CONDITION (code, mode);
8e98892d 672#endif
85fc0ad1 673 return reverse_condition (code);
674 }
8e98892d 675#endif
5924de0b 676
fa8b3d85 677 /* Try a few special cases based on the comparison code. */
8e98892d 678 switch (code)
679 {
85fc0ad1 680 case GEU:
681 case GTU:
682 case LEU:
683 case LTU:
684 case NE:
685 case EQ:
686 /* It is always safe to reverse EQ and NE, even for the floating
687 point. Similary the unsigned comparisons are never used for
688 floating point so we can reverse them in the default way. */
689 return reverse_condition (code);
690 case ORDERED:
691 case UNORDERED:
692 case LTGT:
693 case UNEQ:
694 /* In case we already see unordered comparison, we can be sure to
695 be dealing with floating point so we don't need any more tests. */
696 return reverse_condition_maybe_unordered (code);
697 case UNLT:
698 case UNLE:
699 case UNGT:
700 case UNGE:
701 /* We don't have safe way to reverse these yet. */
702 return UNKNOWN;
703 default:
704 break;
8e98892d 705 }
706
707 /* In case we give up IEEE compatibility, all comparisons are reversible. */
5924de0b 708 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
7f3be425 709 || flag_unsafe_math_optimizations)
8e98892d 710 return reverse_condition (code);
711
712 if (GET_MODE_CLASS (mode) == MODE_CC
5924de0b 713#ifdef HAVE_cc0
714 || arg0 == cc0_rtx
715#endif
716 )
717 {
8e98892d 718 rtx prev;
719 /* Try to search for the comparison to determine the real mode.
720 This code is expensive, but with sane machine description it
721 will be never used, since REVERSIBLE_CC_MODE will return true
722 in all cases. */
111f2389 723 if (! insn)
8e98892d 724 return UNKNOWN;
7113a566 725
7014838c 726 for (prev = prev_nonnote_insn (insn);
727 prev != 0 && GET_CODE (prev) != CODE_LABEL;
728 prev = prev_nonnote_insn (prev))
8e98892d 729 {
730 rtx set = set_of (arg0, prev);
731 if (set && GET_CODE (set) == SET
732 && rtx_equal_p (SET_DEST (set), arg0))
733 {
734 rtx src = SET_SRC (set);
5924de0b 735
8e98892d 736 if (GET_CODE (src) == COMPARE)
737 {
738 rtx comparison = src;
739 arg0 = XEXP (src, 0);
740 mode = GET_MODE (arg0);
741 if (mode == VOIDmode)
742 mode = GET_MODE (XEXP (comparison, 1));
743 break;
744 }
dd5b4b36 745 /* We can get past reg-reg moves. This may be useful for model
8e98892d 746 of i387 comparisons that first move flag registers around. */
747 if (REG_P (src))
748 {
749 arg0 = src;
750 continue;
751 }
752 }
753 /* If register is clobbered in some ununderstandable way,
754 give up. */
755 if (set)
756 return UNKNOWN;
757 }
5924de0b 758 }
759
8e98892d 760 /* An integer condition. */
761 if (GET_CODE (arg0) == CONST_INT
762 || (GET_MODE (arg0) != VOIDmode
763 && GET_MODE_CLASS (mode) != MODE_CC
764 && ! FLOAT_MODE_P (mode)))
765 return reverse_condition (code);
766
767 return UNKNOWN;
768}
769
770/* An wrapper around the previous function to take COMPARISON as rtx
771 expression. This simplifies many callers. */
772enum rtx_code
773reversed_comparison_code (comparison, insn)
774 rtx comparison, insn;
775{
776 if (GET_RTX_CLASS (GET_CODE (comparison)) != '<')
777 return UNKNOWN;
778 return reversed_comparison_code_parts (GET_CODE (comparison),
779 XEXP (comparison, 0),
780 XEXP (comparison, 1), insn);
781}
782\f
a4110d9a 783/* Given an rtx-code for a comparison, return the code for the negated
784 comparison. If no such code exists, return UNKNOWN.
785
786 WATCH OUT! reverse_condition is not safe to use on a jump that might
787 be acting on the results of an IEEE floating point comparison, because
7113a566 788 of the special treatment of non-signaling nans in comparisons.
8e98892d 789 Use reversed_comparison_code instead. */
5924de0b 790
791enum rtx_code
792reverse_condition (code)
793 enum rtx_code code;
794{
795 switch (code)
796 {
797 case EQ:
798 return NE;
5924de0b 799 case NE:
800 return EQ;
5924de0b 801 case GT:
802 return LE;
5924de0b 803 case GE:
804 return LT;
5924de0b 805 case LT:
806 return GE;
5924de0b 807 case LE:
808 return GT;
5924de0b 809 case GTU:
810 return LEU;
5924de0b 811 case GEU:
812 return LTU;
5924de0b 813 case LTU:
814 return GEU;
5924de0b 815 case LEU:
816 return GTU;
a4110d9a 817 case UNORDERED:
818 return ORDERED;
819 case ORDERED:
820 return UNORDERED;
821
822 case UNLT:
823 case UNLE:
824 case UNGT:
825 case UNGE:
826 case UNEQ:
79777bad 827 case LTGT:
a4110d9a 828 return UNKNOWN;
5924de0b 829
830 default:
831 abort ();
5924de0b 832 }
833}
834
79777bad 835/* Similar, but we're allowed to generate unordered comparisons, which
836 makes it safe for IEEE floating-point. Of course, we have to recognize
837 that the target will support them too... */
838
839enum rtx_code
840reverse_condition_maybe_unordered (code)
841 enum rtx_code code;
842{
843 /* Non-IEEE formats don't have unordered conditions. */
844 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
845 return reverse_condition (code);
846
847 switch (code)
848 {
849 case EQ:
850 return NE;
851 case NE:
852 return EQ;
853 case GT:
854 return UNLE;
855 case GE:
856 return UNLT;
857 case LT:
858 return UNGE;
859 case LE:
860 return UNGT;
861 case LTGT:
862 return UNEQ;
79777bad 863 case UNORDERED:
864 return ORDERED;
865 case ORDERED:
866 return UNORDERED;
867 case UNLT:
868 return GE;
869 case UNLE:
870 return GT;
871 case UNGT:
872 return LE;
873 case UNGE:
874 return LT;
875 case UNEQ:
876 return LTGT;
877
878 default:
879 abort ();
880 }
881}
882
5924de0b 883/* Similar, but return the code when two operands of a comparison are swapped.
884 This IS safe for IEEE floating-point. */
885
886enum rtx_code
887swap_condition (code)
888 enum rtx_code code;
889{
890 switch (code)
891 {
892 case EQ:
893 case NE:
a4110d9a 894 case UNORDERED:
895 case ORDERED:
896 case UNEQ:
79777bad 897 case LTGT:
5924de0b 898 return code;
899
900 case GT:
901 return LT;
5924de0b 902 case GE:
903 return LE;
5924de0b 904 case LT:
905 return GT;
5924de0b 906 case LE:
907 return GE;
5924de0b 908 case GTU:
909 return LTU;
5924de0b 910 case GEU:
911 return LEU;
5924de0b 912 case LTU:
913 return GTU;
5924de0b 914 case LEU:
915 return GEU;
a4110d9a 916 case UNLT:
917 return UNGT;
918 case UNLE:
919 return UNGE;
920 case UNGT:
921 return UNLT;
922 case UNGE:
923 return UNLE;
924
5924de0b 925 default:
926 abort ();
5924de0b 927 }
928}
929
930/* Given a comparison CODE, return the corresponding unsigned comparison.
931 If CODE is an equality comparison or already an unsigned comparison,
932 CODE is returned. */
933
934enum rtx_code
935unsigned_condition (code)
936 enum rtx_code code;
937{
938 switch (code)
939 {
940 case EQ:
941 case NE:
942 case GTU:
943 case GEU:
944 case LTU:
945 case LEU:
946 return code;
947
948 case GT:
949 return GTU;
5924de0b 950 case GE:
951 return GEU;
5924de0b 952 case LT:
953 return LTU;
5924de0b 954 case LE:
955 return LEU;
956
957 default:
958 abort ();
959 }
960}
961
962/* Similarly, return the signed version of a comparison. */
963
964enum rtx_code
965signed_condition (code)
966 enum rtx_code code;
967{
968 switch (code)
969 {
970 case EQ:
971 case NE:
972 case GT:
973 case GE:
974 case LT:
975 case LE:
976 return code;
977
978 case GTU:
979 return GT;
5924de0b 980 case GEU:
981 return GE;
5924de0b 982 case LTU:
983 return LT;
5924de0b 984 case LEU:
985 return LE;
986
987 default:
988 abort ();
989 }
990}
991\f
992/* Return non-zero if CODE1 is more strict than CODE2, i.e., if the
993 truth of CODE1 implies the truth of CODE2. */
994
995int
996comparison_dominates_p (code1, code2)
997 enum rtx_code code1, code2;
998{
ca7744c6 999 /* UNKNOWN comparison codes can happen as a result of trying to revert
1000 comparison codes.
1001 They can't match anything, so we have to reject them here. */
1002 if (code1 == UNKNOWN || code2 == UNKNOWN)
1003 return 0;
1004
5924de0b 1005 if (code1 == code2)
1006 return 1;
1007
1008 switch (code1)
1009 {
5aa3f5e2 1010 case UNEQ:
1011 if (code2 == UNLE || code2 == UNGE)
1012 return 1;
1013 break;
1014
5924de0b 1015 case EQ:
79777bad 1016 if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU
1017 || code2 == ORDERED)
5924de0b 1018 return 1;
1019 break;
1020
5aa3f5e2 1021 case UNLT:
1022 if (code2 == UNLE || code2 == NE)
1023 return 1;
1024 break;
1025
5924de0b 1026 case LT:
5aa3f5e2 1027 if (code2 == LE || code2 == NE || code2 == ORDERED || code2 == LTGT)
1028 return 1;
1029 break;
1030
1031 case UNGT:
1032 if (code2 == UNGE || code2 == NE)
5924de0b 1033 return 1;
1034 break;
1035
1036 case GT:
5aa3f5e2 1037 if (code2 == GE || code2 == NE || code2 == ORDERED || code2 == LTGT)
79777bad 1038 return 1;
1039 break;
1040
1041 case GE:
1042 case LE:
1043 if (code2 == ORDERED)
1044 return 1;
1045 break;
1046
1047 case LTGT:
1048 if (code2 == NE || code2 == ORDERED)
5924de0b 1049 return 1;
1050 break;
1051
1052 case LTU:
11088b43 1053 if (code2 == LEU || code2 == NE)
5924de0b 1054 return 1;
1055 break;
1056
1057 case GTU:
11088b43 1058 if (code2 == GEU || code2 == NE)
5924de0b 1059 return 1;
1060 break;
79777bad 1061
1062 case UNORDERED:
5aa3f5e2 1063 if (code2 == NE || code2 == UNEQ || code2 == UNLE || code2 == UNLT
1064 || code2 == UNGE || code2 == UNGT)
79777bad 1065 return 1;
1066 break;
7113a566 1067
0dbd1c74 1068 default:
1069 break;
5924de0b 1070 }
1071
1072 return 0;
1073}
1074\f
1075/* Return 1 if INSN is an unconditional jump and nothing else. */
1076
1077int
1078simplejump_p (insn)
1079 rtx insn;
1080{
8d472058 1081 return (GET_CODE (insn) == JUMP_INSN
1082 && GET_CODE (PATTERN (insn)) == SET
1083 && GET_CODE (SET_DEST (PATTERN (insn))) == PC
1084 && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
5924de0b 1085}
1086
1087/* Return nonzero if INSN is a (possibly) conditional jump
7113a566 1088 and nothing more.
1089
d670e794 1090 Use this function is deprecated, since we need to support combined
1091 branch and compare insns. Use any_condjump_p instead whenever possible. */
5924de0b 1092
1093int
1094condjump_p (insn)
1095 rtx insn;
1096{
19cb6b50 1097 rtx x = PATTERN (insn);
7014838c 1098
1099 if (GET_CODE (x) != SET
1100 || GET_CODE (SET_DEST (x)) != PC)
4fbe8fa7 1101 return 0;
7014838c 1102
1103 x = SET_SRC (x);
1104 if (GET_CODE (x) == LABEL_REF)
4fbe8fa7 1105 return 1;
7113a566 1106 else
1107 return (GET_CODE (x) == IF_THEN_ELSE
1108 && ((GET_CODE (XEXP (x, 2)) == PC
1109 && (GET_CODE (XEXP (x, 1)) == LABEL_REF
1110 || GET_CODE (XEXP (x, 1)) == RETURN))
1111 || (GET_CODE (XEXP (x, 1)) == PC
1112 && (GET_CODE (XEXP (x, 2)) == LABEL_REF
1113 || GET_CODE (XEXP (x, 2)) == RETURN))));
7014838c 1114
4fbe8fa7 1115 return 0;
1116}
1117
7014838c 1118/* Return nonzero if INSN is a (possibly) conditional jump inside a
3a941ad5 1119 PARALLEL.
7113a566 1120
d670e794 1121 Use this function is deprecated, since we need to support combined
1122 branch and compare insns. Use any_condjump_p instead whenever possible. */
4fbe8fa7 1123
1124int
1125condjump_in_parallel_p (insn)
1126 rtx insn;
1127{
19cb6b50 1128 rtx x = PATTERN (insn);
4fbe8fa7 1129
1130 if (GET_CODE (x) != PARALLEL)
1131 return 0;
1132 else
1133 x = XVECEXP (x, 0, 0);
1134
5924de0b 1135 if (GET_CODE (x) != SET)
1136 return 0;
1137 if (GET_CODE (SET_DEST (x)) != PC)
1138 return 0;
1139 if (GET_CODE (SET_SRC (x)) == LABEL_REF)
1140 return 1;
1141 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
1142 return 0;
1143 if (XEXP (SET_SRC (x), 2) == pc_rtx
1144 && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
1145 || GET_CODE (XEXP (SET_SRC (x), 1)) == RETURN))
1146 return 1;
1147 if (XEXP (SET_SRC (x), 1) == pc_rtx
1148 && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
1149 || GET_CODE (XEXP (SET_SRC (x), 2)) == RETURN))
1150 return 1;
1151 return 0;
1152}
1153
d670e794 1154/* Return set of PC, otherwise NULL. */
1155
3a941ad5 1156rtx
1157pc_set (insn)
1158 rtx insn;
1159{
1160 rtx pat;
1161 if (GET_CODE (insn) != JUMP_INSN)
d670e794 1162 return NULL_RTX;
3a941ad5 1163 pat = PATTERN (insn);
d670e794 1164
1165 /* The set is allowed to appear either as the insn pattern or
1166 the first set in a PARALLEL. */
1167 if (GET_CODE (pat) == PARALLEL)
1168 pat = XVECEXP (pat, 0, 0);
3a941ad5 1169 if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == PC)
1170 return pat;
d670e794 1171
1172 return NULL_RTX;
3a941ad5 1173}
1174
d670e794 1175/* Return true when insn is an unconditional direct jump,
1176 possibly bundled inside a PARALLEL. */
1177
3a941ad5 1178int
1179any_uncondjump_p (insn)
1180 rtx insn;
1181{
1182 rtx x = pc_set (insn);
1183 if (!x)
1184 return 0;
1185 if (GET_CODE (SET_SRC (x)) != LABEL_REF)
1186 return 0;
1187 return 1;
1188}
1189
d670e794 1190/* Return true when insn is a conditional jump. This function works for
3a941ad5 1191 instructions containing PC sets in PARALLELs. The instruction may have
1192 various other effects so before removing the jump you must verify
9641f63c 1193 onlyjump_p.
3a941ad5 1194
d670e794 1195 Note that unlike condjump_p it returns false for unconditional jumps. */
1196
3a941ad5 1197int
1198any_condjump_p (insn)
1199 rtx insn;
1200{
1201 rtx x = pc_set (insn);
d670e794 1202 enum rtx_code a, b;
1203
3a941ad5 1204 if (!x)
1205 return 0;
d670e794 1206 if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
1207 return 0;
3a941ad5 1208
d670e794 1209 a = GET_CODE (XEXP (SET_SRC (x), 1));
1210 b = GET_CODE (XEXP (SET_SRC (x), 2));
3a941ad5 1211
d670e794 1212 return ((b == PC && (a == LABEL_REF || a == RETURN))
7113a566 1213 || (a == PC && (b == LABEL_REF || b == RETURN)));
3a941ad5 1214}
1215
8f7b24f3 1216/* Return the label of a conditional jump. */
1217
1218rtx
1219condjump_label (insn)
1220 rtx insn;
1221{
d670e794 1222 rtx x = pc_set (insn);
8f7b24f3 1223
d670e794 1224 if (!x)
8f7b24f3 1225 return NULL_RTX;
1226 x = SET_SRC (x);
1227 if (GET_CODE (x) == LABEL_REF)
1228 return x;
1229 if (GET_CODE (x) != IF_THEN_ELSE)
1230 return NULL_RTX;
1231 if (XEXP (x, 2) == pc_rtx && GET_CODE (XEXP (x, 1)) == LABEL_REF)
1232 return XEXP (x, 1);
1233 if (XEXP (x, 1) == pc_rtx && GET_CODE (XEXP (x, 2)) == LABEL_REF)
1234 return XEXP (x, 2);
1235 return NULL_RTX;
1236}
1237
71caadc0 1238/* Return true if INSN is a (possibly conditional) return insn. */
1239
1240static int
1241returnjump_p_1 (loc, data)
1242 rtx *loc;
1243 void *data ATTRIBUTE_UNUSED;
1244{
1245 rtx x = *loc;
c3987c92 1246
1247 return x && (GET_CODE (x) == RETURN
1248 || (GET_CODE (x) == SET && SET_IS_RETURN_P (x)));
71caadc0 1249}
1250
1251int
1252returnjump_p (insn)
1253 rtx insn;
1254{
cbd914e1 1255 if (GET_CODE (insn) != JUMP_INSN)
1256 return 0;
71caadc0 1257 return for_each_rtx (&PATTERN (insn), returnjump_p_1, NULL);
1258}
1259
459e9193 1260/* Return true if INSN is a jump that only transfers control and
1261 nothing more. */
1262
1263int
1264onlyjump_p (insn)
1265 rtx insn;
1266{
1267 rtx set;
1268
1269 if (GET_CODE (insn) != JUMP_INSN)
1270 return 0;
1271
1272 set = single_set (insn);
1273 if (set == NULL)
1274 return 0;
1275 if (GET_CODE (SET_DEST (set)) != PC)
1276 return 0;
1277 if (side_effects_p (SET_SRC (set)))
1278 return 0;
1279
1280 return 1;
1281}
1282
9bf8c346 1283#ifdef HAVE_cc0
1284
2dcd83ba 1285/* Return non-zero if X is an RTX that only sets the condition codes
1286 and has no side effects. */
1287
1288int
1289only_sets_cc0_p (x)
1290 rtx x;
1291{
1292
1293 if (! x)
1294 return 0;
1295
1296 if (INSN_P (x))
1297 x = PATTERN (x);
1298
1299 return sets_cc0_p (x) == 1 && ! side_effects_p (x);
1300}
1301
5924de0b 1302/* Return 1 if X is an RTX that does nothing but set the condition codes
1303 and CLOBBER or USE registers.
1304 Return -1 if X does explicitly set the condition codes,
1305 but also does other things. */
1306
1307int
1308sets_cc0_p (x)
2dcd83ba 1309 rtx x;
5924de0b 1310{
2dcd83ba 1311
1312 if (! x)
1313 return 0;
1314
1315 if (INSN_P (x))
1316 x = PATTERN (x);
1317
5924de0b 1318 if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
1319 return 1;
1320 if (GET_CODE (x) == PARALLEL)
1321 {
1322 int i;
1323 int sets_cc0 = 0;
1324 int other_things = 0;
1325 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1326 {
1327 if (GET_CODE (XVECEXP (x, 0, i)) == SET
1328 && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
1329 sets_cc0 = 1;
1330 else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
1331 other_things = 1;
1332 }
1333 return ! sets_cc0 ? 0 : other_things ? -1 : 1;
1334 }
1335 return 0;
5924de0b 1336}
9bf8c346 1337#endif
5924de0b 1338\f
1339/* Follow any unconditional jump at LABEL;
1340 return the ultimate label reached by any such chain of jumps.
1341 If LABEL is not followed by a jump, return LABEL.
35e0b416 1342 If the chain loops or we can't find end, return LABEL,
1343 since that tells caller to avoid changing the insn.
5924de0b 1344
1345 If RELOAD_COMPLETED is 0, we do not chain across a NOTE_INSN_LOOP_BEG or
1346 a USE or CLOBBER. */
1347
1348rtx
1349follow_jumps (label)
1350 rtx label;
1351{
19cb6b50 1352 rtx insn;
1353 rtx next;
1354 rtx value = label;
1355 int depth;
5924de0b 1356
1357 for (depth = 0;
1358 (depth < 10
1359 && (insn = next_active_insn (value)) != 0
1360 && GET_CODE (insn) == JUMP_INSN
ba08b7e7 1361 && ((JUMP_LABEL (insn) != 0 && any_uncondjump_p (insn)
1362 && onlyjump_p (insn))
f93ed41b 1363 || GET_CODE (PATTERN (insn)) == RETURN)
5924de0b 1364 && (next = NEXT_INSN (insn))
1365 && GET_CODE (next) == BARRIER);
1366 depth++)
1367 {
1368 /* Don't chain through the insn that jumps into a loop
1369 from outside the loop,
1370 since that would create multiple loop entry jumps
1371 and prevent loop optimization. */
1372 rtx tem;
1373 if (!reload_completed)
1374 for (tem = value; tem != insn; tem = NEXT_INSN (tem))
1375 if (GET_CODE (tem) == NOTE
55b8f81a 1376 && (NOTE_LINE_NUMBER (tem) == NOTE_INSN_LOOP_BEG
1377 /* ??? Optional. Disables some optimizations, but makes
1378 gcov output more accurate with -O. */
1379 || (flag_test_coverage && NOTE_LINE_NUMBER (tem) > 0)))
5924de0b 1380 return value;
1381
1382 /* If we have found a cycle, make the insn jump to itself. */
1383 if (JUMP_LABEL (insn) == label)
35e0b416 1384 return label;
cf03b15b 1385
1386 tem = next_active_insn (JUMP_LABEL (insn));
1387 if (tem && (GET_CODE (PATTERN (tem)) == ADDR_VEC
1388 || GET_CODE (PATTERN (tem)) == ADDR_DIFF_VEC))
1389 break;
1390
5924de0b 1391 value = JUMP_LABEL (insn);
1392 }
35e0b416 1393 if (depth == 10)
1394 return label;
5924de0b 1395 return value;
1396}
1397
5924de0b 1398\f
1399/* Find all CODE_LABELs referred to in X, and increment their use counts.
1400 If INSN is a JUMP_INSN and there is at least one CODE_LABEL referenced
1401 in INSN, then store one of them in JUMP_LABEL (INSN).
1402 If INSN is an INSN or a CALL_INSN and there is at least one CODE_LABEL
1403 referenced in INSN, add a REG_LABEL note containing that label to INSN.
1404 Also, when there are consecutive labels, canonicalize on the last of them.
1405
1406 Note that two labels separated by a loop-beginning note
1407 must be kept distinct if we have not yet done loop-optimization,
1408 because the gap between them is where loop-optimize
1409 will want to move invariant code to. CROSS_JUMP tells us
bf73fcf4 1410 that loop-optimization is done with. */
5924de0b 1411
5377f687 1412void
bf73fcf4 1413mark_jump_label (x, insn, in_mem)
19cb6b50 1414 rtx x;
5924de0b 1415 rtx insn;
190099a6 1416 int in_mem;
5924de0b 1417{
19cb6b50 1418 RTX_CODE code = GET_CODE (x);
1419 int i;
1420 const char *fmt;
5924de0b 1421
1422 switch (code)
1423 {
1424 case PC:
1425 case CC0:
1426 case REG:
1427 case SUBREG:
1428 case CONST_INT:
5924de0b 1429 case CONST_DOUBLE:
1430 case CLOBBER:
1431 case CALL:
1432 return;
1433
d8e0d332 1434 case MEM:
190099a6 1435 in_mem = 1;
1436 break;
1437
1438 case SYMBOL_REF:
1439 if (!in_mem)
7113a566 1440 return;
190099a6 1441
d8e0d332 1442 /* If this is a constant-pool reference, see if it is a label. */
190099a6 1443 if (CONSTANT_POOL_ADDRESS_P (x))
bf73fcf4 1444 mark_jump_label (get_pool_constant (x), insn, in_mem);
d8e0d332 1445 break;
1446
5924de0b 1447 case LABEL_REF:
1448 {
b4d3bcce 1449 rtx label = XEXP (x, 0);
b4d3bcce 1450
74b0991d 1451 /* Ignore remaining references to unreachable labels that
1452 have been deleted. */
7113a566 1453 if (GET_CODE (label) == NOTE
74b0991d 1454 && NOTE_LINE_NUMBER (label) == NOTE_INSN_DELETED_LABEL)
1455 break;
1456
5924de0b 1457 if (GET_CODE (label) != CODE_LABEL)
1458 abort ();
b4d3bcce 1459
f08cae9d 1460 /* Ignore references to labels of containing functions. */
1461 if (LABEL_REF_NONLOCAL_P (x))
1462 break;
b4d3bcce 1463
5924de0b 1464 XEXP (x, 0) = label;
943e16d8 1465 if (! insn || ! INSN_DELETED_P (insn))
1466 ++LABEL_NUSES (label);
b4d3bcce 1467
5924de0b 1468 if (insn)
1469 {
1470 if (GET_CODE (insn) == JUMP_INSN)
1471 JUMP_LABEL (insn) = label;
ab2237b5 1472 else
e89849bd 1473 {
ab2237b5 1474 /* Add a REG_LABEL note for LABEL unless there already
1475 is one. All uses of a label, except for labels
1476 that are the targets of jumps, must have a
1477 REG_LABEL note. */
1478 if (! find_reg_note (insn, REG_LABEL, label))
60d9e0ee 1479 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
ab2237b5 1480 REG_NOTES (insn));
5924de0b 1481 }
1482 }
1483 return;
1484 }
1485
1486 /* Do walk the labels in a vector, but not the first operand of an
1487 ADDR_DIFF_VEC. Don't set the JUMP_LABEL of a vector. */
1488 case ADDR_VEC:
1489 case ADDR_DIFF_VEC:
943e16d8 1490 if (! INSN_DELETED_P (insn))
1491 {
1492 int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
5924de0b 1493
943e16d8 1494 for (i = 0; i < XVECLEN (x, eltnum); i++)
bf73fcf4 1495 mark_jump_label (XVECEXP (x, eltnum, i), NULL_RTX, in_mem);
943e16d8 1496 }
0dbd1c74 1497 return;
7113a566 1498
0dbd1c74 1499 default:
1500 break;
5924de0b 1501 }
1502
1503 fmt = GET_RTX_FORMAT (code);
1504 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1505 {
1506 if (fmt[i] == 'e')
bf73fcf4 1507 mark_jump_label (XEXP (x, i), insn, in_mem);
5924de0b 1508 else if (fmt[i] == 'E')
1509 {
19cb6b50 1510 int j;
5924de0b 1511 for (j = 0; j < XVECLEN (x, i); j++)
bf73fcf4 1512 mark_jump_label (XVECEXP (x, i, j), insn, in_mem);
5924de0b 1513 }
1514 }
1515}
1516
1517/* If all INSN does is set the pc, delete it,
1518 and delete the insn that set the condition codes for it
1519 if that's what the previous thing was. */
1520
1521void
1522delete_jump (insn)
1523 rtx insn;
1524{
19cb6b50 1525 rtx set = single_set (insn);
f4ef05ab 1526
1527 if (set && GET_CODE (SET_DEST (set)) == PC)
1528 delete_computation (insn);
1529}
1530
155b05dc 1531/* Verify INSN is a BARRIER and delete it. */
1532
1533void
1534delete_barrier (insn)
1535 rtx insn;
1536{
1537 if (GET_CODE (insn) != BARRIER)
1538 abort ();
1539
1540 delete_insn (insn);
1541}
1542
ab1241f2 1543/* Recursively delete prior insns that compute the value (used only by INSN
1544 which the caller is deleting) stored in the register mentioned by NOTE
1545 which is a REG_DEAD note associated with INSN. */
1546
1547static void
1548delete_prior_computation (note, insn)
1549 rtx note;
1550 rtx insn;
1551{
1552 rtx our_prev;
1553 rtx reg = XEXP (note, 0);
1554
1555 for (our_prev = prev_nonnote_insn (insn);
272a2170 1556 our_prev && (GET_CODE (our_prev) == INSN
1557 || GET_CODE (our_prev) == CALL_INSN);
ab1241f2 1558 our_prev = prev_nonnote_insn (our_prev))
1559 {
1560 rtx pat = PATTERN (our_prev);
1561
272a2170 1562 /* If we reach a CALL which is not calling a const function
1563 or the callee pops the arguments, then give up. */
1564 if (GET_CODE (our_prev) == CALL_INSN
06a652d1 1565 && (! CONST_OR_PURE_CALL_P (our_prev)
272a2170 1566 || GET_CODE (pat) != SET || GET_CODE (SET_SRC (pat)) != CALL))
1567 break;
1568
ab1241f2 1569 /* If we reach a SEQUENCE, it is too complex to try to
1570 do anything with it, so give up. */
1571 if (GET_CODE (pat) == SEQUENCE)
1572 break;
1573
1574 if (GET_CODE (pat) == USE
1575 && GET_CODE (XEXP (pat, 0)) == INSN)
1576 /* reorg creates USEs that look like this. We leave them
1577 alone because reorg needs them for its own purposes. */
1578 break;
1579
1580 if (reg_set_p (reg, pat))
1581 {
272a2170 1582 if (side_effects_p (pat) && GET_CODE (our_prev) != CALL_INSN)
ab1241f2 1583 break;
1584
1585 if (GET_CODE (pat) == PARALLEL)
1586 {
1587 /* If we find a SET of something else, we can't
1588 delete the insn. */
1589
1590 int i;
1591
1592 for (i = 0; i < XVECLEN (pat, 0); i++)
1593 {
1594 rtx part = XVECEXP (pat, 0, i);
1595
1596 if (GET_CODE (part) == SET
1597 && SET_DEST (part) != reg)
1598 break;
1599 }
1600
1601 if (i == XVECLEN (pat, 0))
1602 delete_computation (our_prev);
1603 }
1604 else if (GET_CODE (pat) == SET
1605 && GET_CODE (SET_DEST (pat)) == REG)
1606 {
1607 int dest_regno = REGNO (SET_DEST (pat));
1608 int dest_endregno
7113a566 1609 = (dest_regno
1610 + (dest_regno < FIRST_PSEUDO_REGISTER
ab1241f2 1611 ? HARD_REGNO_NREGS (dest_regno,
7113a566 1612 GET_MODE (SET_DEST (pat))) : 1));
ab1241f2 1613 int regno = REGNO (reg);
7113a566 1614 int endregno
1615 = (regno
1616 + (regno < FIRST_PSEUDO_REGISTER
1617 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1));
ab1241f2 1618
1619 if (dest_regno >= regno
1620 && dest_endregno <= endregno)
1621 delete_computation (our_prev);
1622
1623 /* We may have a multi-word hard register and some, but not
1624 all, of the words of the register are needed in subsequent
1625 insns. Write REG_UNUSED notes for those parts that were not
1626 needed. */
1627 else if (dest_regno <= regno
272a2170 1628 && dest_endregno >= endregno)
ab1241f2 1629 {
1630 int i;
1631
1632 REG_NOTES (our_prev)
7113a566 1633 = gen_rtx_EXPR_LIST (REG_UNUSED, reg,
1634 REG_NOTES (our_prev));
ab1241f2 1635
1636 for (i = dest_regno; i < dest_endregno; i++)
1637 if (! find_regno_note (our_prev, REG_UNUSED, i))
1638 break;
1639
1640 if (i == dest_endregno)
1641 delete_computation (our_prev);
1642 }
1643 }
1644
1645 break;
1646 }
1647
1648 /* If PAT references the register that dies here, it is an
1649 additional use. Hence any prior SET isn't dead. However, this
1650 insn becomes the new place for the REG_DEAD note. */
1651 if (reg_overlap_mentioned_p (reg, pat))
1652 {
1653 XEXP (note, 1) = REG_NOTES (our_prev);
1654 REG_NOTES (our_prev) = note;
1655 break;
1656 }
1657 }
1658}
1659
f4ef05ab 1660/* Delete INSN and recursively delete insns that compute values used only
1661 by INSN. This uses the REG_DEAD notes computed during flow analysis.
1662 If we are running before flow.c, we need do nothing since flow.c will
1663 delete dead code. We also can't know if the registers being used are
1664 dead or not at this point.
1665
1666 Otherwise, look at all our REG_DEAD notes. If a previous insn does
1667 nothing other than set a register that dies in this insn, we can delete
1668 that insn as well.
1669
1670 On machines with CC0, if CC0 is used in this insn, we may be able to
1671 delete the insn that set it. */
1672
fb374064 1673static void
f4ef05ab 1674delete_computation (insn)
1675 rtx insn;
1676{
1677 rtx note, next;
5924de0b 1678
5924de0b 1679#ifdef HAVE_cc0
41d75671 1680 if (reg_referenced_p (cc0_rtx, PATTERN (insn)))
f4ef05ab 1681 {
5b39732e 1682 rtx prev = prev_nonnote_insn (insn);
5924de0b 1683 /* We assume that at this stage
1684 CC's are always set explicitly
1685 and always immediately before the jump that
1686 will use them. So if the previous insn
1687 exists to set the CC's, delete it
1688 (unless it performs auto-increments, etc.). */
1689 if (prev && GET_CODE (prev) == INSN
1690 && sets_cc0_p (PATTERN (prev)))
1691 {
1692 if (sets_cc0_p (PATTERN (prev)) > 0
ab1241f2 1693 && ! side_effects_p (PATTERN (prev)))
f4ef05ab 1694 delete_computation (prev);
5924de0b 1695 else
1696 /* Otherwise, show that cc0 won't be used. */
941522d6 1697 REG_NOTES (prev) = gen_rtx_EXPR_LIST (REG_UNUSED,
1698 cc0_rtx, REG_NOTES (prev));
5924de0b 1699 }
5b39732e 1700 }
f4ef05ab 1701#endif
5924de0b 1702
5b39732e 1703 for (note = REG_NOTES (insn); note; note = next)
1704 {
5b39732e 1705 next = XEXP (note, 1);
5924de0b 1706
5b39732e 1707 if (REG_NOTE_KIND (note) != REG_DEAD
1708 /* Verify that the REG_NOTE is legitimate. */
1709 || GET_CODE (XEXP (note, 0)) != REG)
1710 continue;
5924de0b 1711
ab1241f2 1712 delete_prior_computation (note, insn);
5924de0b 1713 }
f4ef05ab 1714
e4bf866d 1715 delete_related_insns (insn);
5924de0b 1716}
1717\f
e4bf866d 1718/* Delete insn INSN from the chain of insns and update label ref counts
1719 and delete insns now unreachable.
1720
1721 Returns the first insn after INSN that was not deleted.
5924de0b 1722
e4bf866d 1723 Usage of this instruction is deprecated. Use delete_insn instead and
1724 subsequent cfg_cleanup pass to delete unreachable code if needed. */
5924de0b 1725
1726rtx
e4bf866d 1727delete_related_insns (insn)
19cb6b50 1728 rtx insn;
5924de0b 1729{
19cb6b50 1730 int was_code_label = (GET_CODE (insn) == CODE_LABEL);
d3df77e9 1731 rtx note;
e4bf866d 1732 rtx next = NEXT_INSN (insn), prev = PREV_INSN (insn);
5924de0b 1733
1734 while (next && INSN_DELETED_P (next))
1735 next = NEXT_INSN (next);
1736
1737 /* This insn is already deleted => return first following nondeleted. */
1738 if (INSN_DELETED_P (insn))
1739 return next;
1740
e4bf866d 1741 delete_insn (insn);
5924de0b 1742
5924de0b 1743 /* If instruction is followed by a barrier,
1744 delete the barrier too. */
1745
1746 if (next != 0 && GET_CODE (next) == BARRIER)
e4bf866d 1747 delete_insn (next);
5924de0b 1748
1749 /* If deleting a jump, decrement the count of the label,
1750 and delete the label if it is now unused. */
1751
1752 if (GET_CODE (insn) == JUMP_INSN && JUMP_LABEL (insn))
1793cd6b 1753 {
1754 rtx lab = JUMP_LABEL (insn), lab_next;
1755
e4bf866d 1756 if (LABEL_NUSES (lab) == 0)
1793cd6b 1757 {
1758 /* This can delete NEXT or PREV,
1759 either directly if NEXT is JUMP_LABEL (INSN),
1760 or indirectly through more levels of jumps. */
e4bf866d 1761 delete_related_insns (lab);
1793cd6b 1762
1763 /* I feel a little doubtful about this loop,
1764 but I see no clean and sure alternative way
1765 to find the first insn after INSN that is not now deleted.
1766 I hope this works. */
1767 while (next && INSN_DELETED_P (next))
1768 next = NEXT_INSN (next);
1769 return next;
1770 }
1771 else if ((lab_next = next_nonnote_insn (lab)) != NULL
1772 && GET_CODE (lab_next) == JUMP_INSN
1773 && (GET_CODE (PATTERN (lab_next)) == ADDR_VEC
1774 || GET_CODE (PATTERN (lab_next)) == ADDR_DIFF_VEC))
1775 {
1776 /* If we're deleting the tablejump, delete the dispatch table.
4a82352a 1777 We may not be able to kill the label immediately preceding
1793cd6b 1778 just yet, as it might be referenced in code leading up to
1779 the tablejump. */
e4bf866d 1780 delete_related_insns (lab_next);
1793cd6b 1781 }
1782 }
5924de0b 1783
9c9e0c01 1784 /* Likewise if we're deleting a dispatch table. */
1785
1786 if (GET_CODE (insn) == JUMP_INSN
1787 && (GET_CODE (PATTERN (insn)) == ADDR_VEC
1788 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
1789 {
1790 rtx pat = PATTERN (insn);
1791 int i, diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
1792 int len = XVECLEN (pat, diff_vec_p);
1793
1794 for (i = 0; i < len; i++)
e4bf866d 1795 if (LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0)) == 0)
1796 delete_related_insns (XEXP (XVECEXP (pat, diff_vec_p, i), 0));
9c9e0c01 1797 while (next && INSN_DELETED_P (next))
1798 next = NEXT_INSN (next);
1799 return next;
1800 }
1801
d3df77e9 1802 /* Likewise for an ordinary INSN / CALL_INSN with a REG_LABEL note. */
1803 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
1804 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
0c97f529 1805 if (REG_NOTE_KIND (note) == REG_LABEL
1806 /* This could also be a NOTE_INSN_DELETED_LABEL note. */
1807 && GET_CODE (XEXP (note, 0)) == CODE_LABEL)
e4bf866d 1808 if (LABEL_NUSES (XEXP (note, 0)) == 0)
1809 delete_related_insns (XEXP (note, 0));
d3df77e9 1810
5924de0b 1811 while (prev && (INSN_DELETED_P (prev) || GET_CODE (prev) == NOTE))
1812 prev = PREV_INSN (prev);
1813
1814 /* If INSN was a label and a dispatch table follows it,
1815 delete the dispatch table. The tablejump must have gone already.
1816 It isn't useful to fall through into a table. */
1817
9cdc08c6 1818 if (was_code_label
5924de0b 1819 && NEXT_INSN (insn) != 0
1820 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
1821 && (GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_VEC
1822 || GET_CODE (PATTERN (NEXT_INSN (insn))) == ADDR_DIFF_VEC))
e4bf866d 1823 next = delete_related_insns (NEXT_INSN (insn));
5924de0b 1824
1825 /* If INSN was a label, delete insns following it if now unreachable. */
1826
9cdc08c6 1827 if (was_code_label && prev && GET_CODE (prev) == BARRIER)
5924de0b 1828 {
19cb6b50 1829 RTX_CODE code;
5924de0b 1830 while (next != 0
fb374064 1831 && (GET_RTX_CLASS (code = GET_CODE (next)) == 'i'
64f0b11f 1832 || code == NOTE || code == BARRIER
59bee35e 1833 || (code == CODE_LABEL && INSN_DELETED_P (next))))
5924de0b 1834 {
1835 if (code == NOTE
1836 && NOTE_LINE_NUMBER (next) != NOTE_INSN_FUNCTION_END)
1837 next = NEXT_INSN (next);
59bee35e 1838 /* Keep going past other deleted labels to delete what follows. */
1839 else if (code == CODE_LABEL && INSN_DELETED_P (next))
1840 next = NEXT_INSN (next);
5924de0b 1841 else
1842 /* Note: if this deletes a jump, it can cause more
1843 deletion of unreachable code, after a different label.
1844 As long as the value from this recursive call is correct,
1845 this invocation functions correctly. */
e4bf866d 1846 next = delete_related_insns (next);
5924de0b 1847 }
1848 }
1849
1850 return next;
1851}
1852
1853/* Advance from INSN till reaching something not deleted
1854 then return that. May return INSN itself. */
1855
1856rtx
1857next_nondeleted_insn (insn)
1858 rtx insn;
1859{
1860 while (INSN_DELETED_P (insn))
1861 insn = NEXT_INSN (insn);
1862 return insn;
1863}
1864\f
1865/* Delete a range of insns from FROM to TO, inclusive.
1866 This is for the sake of peephole optimization, so assume
1867 that whatever these insns do will still be done by a new
1868 peephole insn that will replace them. */
1869
1870void
1871delete_for_peephole (from, to)
19cb6b50 1872 rtx from, to;
5924de0b 1873{
19cb6b50 1874 rtx insn = from;
5924de0b 1875
1876 while (1)
1877 {
19cb6b50 1878 rtx next = NEXT_INSN (insn);
1879 rtx prev = PREV_INSN (insn);
5924de0b 1880
1881 if (GET_CODE (insn) != NOTE)
1882 {
1883 INSN_DELETED_P (insn) = 1;
1884
1885 /* Patch this insn out of the chain. */
1886 /* We don't do this all at once, because we
1887 must preserve all NOTEs. */
1888 if (prev)
1889 NEXT_INSN (prev) = next;
1890
1891 if (next)
1892 PREV_INSN (next) = prev;
1893 }
1894
1895 if (insn == to)
1896 break;
1897 insn = next;
1898 }
1899
1900 /* Note that if TO is an unconditional jump
1901 we *do not* delete the BARRIER that follows,
1902 since the peephole that replaces this sequence
1903 is also an unconditional jump in that case. */
1904}
1905\f
71a3455a 1906/* We have determined that INSN is never reached, and are about to
1907 delete it. Print a warning if the user asked for one.
1908
1909 To try to make this warning more useful, this should only be called
1910 once per basic block not reached, and it only warns when the basic
1911 block contains more than one line from the current function, and
1912 contains at least one operation. CSE and inlining can duplicate insns,
1913 so it's possible to get spurious warnings from this. */
1914
1915void
5b154f04 1916never_reached_warning (avoided_insn, finish)
1917 rtx avoided_insn, finish;
71a3455a 1918{
1919 rtx insn;
1920 rtx a_line_note = NULL;
5b154f04 1921 int two_avoided_lines = 0, contains_insn = 0, reached_end = 0;
7113a566 1922
71a3455a 1923 if (! warn_notreached)
1924 return;
1925
1926 /* Scan forwards, looking at LINE_NUMBER notes, until
1927 we hit a LABEL or we run out of insns. */
7113a566 1928
71a3455a 1929 for (insn = avoided_insn; insn != NULL; insn = NEXT_INSN (insn))
1930 {
5b154f04 1931 if (finish == NULL && GET_CODE (insn) == CODE_LABEL)
7113a566 1932 break;
5b154f04 1933
1934 if (GET_CODE (insn) == NOTE /* A line number note? */
1935 && NOTE_LINE_NUMBER (insn) >= 0)
71a3455a 1936 {
1937 if (a_line_note == NULL)
1938 a_line_note = insn;
1939 else
1940 two_avoided_lines |= (NOTE_LINE_NUMBER (a_line_note)
1941 != NOTE_LINE_NUMBER (insn));
1942 }
9204e736 1943 else if (INSN_P (insn))
5b154f04 1944 {
1945 if (reached_end)
1946 break;
1947 contains_insn = 1;
1948 }
1949
1950 if (insn == finish)
1951 reached_end = 1;
71a3455a 1952 }
1953 if (two_avoided_lines && contains_insn)
1954 warning_with_file_and_line (NOTE_SOURCE_FILE (a_line_note),
1955 NOTE_LINE_NUMBER (a_line_note),
1956 "will never be executed");
1957}
1958\f
a8b5d014 1959/* Throughout LOC, redirect OLABEL to NLABEL. Treat null OLABEL or
1960 NLABEL as a return. Accrue modifications into the change group. */
5924de0b 1961
a8b5d014 1962static void
1963redirect_exp_1 (loc, olabel, nlabel, insn)
1964 rtx *loc;
1965 rtx olabel, nlabel;
5924de0b 1966 rtx insn;
1967{
19cb6b50 1968 rtx x = *loc;
1969 RTX_CODE code = GET_CODE (x);
1970 int i;
1971 const char *fmt;
5924de0b 1972
a8b5d014 1973 if (code == LABEL_REF)
5924de0b 1974 {
a8b5d014 1975 if (XEXP (x, 0) == olabel)
1976 {
1977 rtx n;
1978 if (nlabel)
1979 n = gen_rtx_LABEL_REF (VOIDmode, nlabel);
1980 else
7113a566 1981 n = gen_rtx_RETURN (VOIDmode);
5924de0b 1982
a8b5d014 1983 validate_change (insn, loc, n, 1);
1984 return;
1985 }
1986 }
1987 else if (code == RETURN && olabel == 0)
1988 {
1989 x = gen_rtx_LABEL_REF (VOIDmode, nlabel);
1990 if (loc == &PATTERN (insn))
1991 x = gen_rtx_SET (VOIDmode, pc_rtx, x);
1992 validate_change (insn, loc, x, 1);
1993 return;
1994 }
5924de0b 1995
a8b5d014 1996 if (code == SET && nlabel == 0 && SET_DEST (x) == pc_rtx
1997 && GET_CODE (SET_SRC (x)) == LABEL_REF
1998 && XEXP (SET_SRC (x), 0) == olabel)
1999 {
2000 validate_change (insn, loc, gen_rtx_RETURN (VOIDmode), 1);
2001 return;
5924de0b 2002 }
2003
2004 fmt = GET_RTX_FORMAT (code);
2005 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2006 {
2007 if (fmt[i] == 'e')
a8b5d014 2008 redirect_exp_1 (&XEXP (x, i), olabel, nlabel, insn);
1bd8ca86 2009 else if (fmt[i] == 'E')
5924de0b 2010 {
19cb6b50 2011 int j;
5924de0b 2012 for (j = 0; j < XVECLEN (x, i); j++)
a8b5d014 2013 redirect_exp_1 (&XVECEXP (x, i, j), olabel, nlabel, insn);
5924de0b 2014 }
2015 }
a8b5d014 2016}
5924de0b 2017
a8b5d014 2018/* Similar, but apply the change group and report success or failure. */
2019
ba08b7e7 2020static int
2021redirect_exp (olabel, nlabel, insn)
a8b5d014 2022 rtx olabel, nlabel;
2023 rtx insn;
2024{
ba08b7e7 2025 rtx *loc;
2026
2027 if (GET_CODE (PATTERN (insn)) == PARALLEL)
2028 loc = &XVECEXP (PATTERN (insn), 0, 0);
2029 else
2030 loc = &PATTERN (insn);
2031
a8b5d014 2032 redirect_exp_1 (loc, olabel, nlabel, insn);
2033 if (num_validated_changes () == 0)
2034 return 0;
2035
2036 return apply_change_group ();
5924de0b 2037}
a8b5d014 2038
2039/* Make JUMP go to NLABEL instead of where it jumps now. Accrue
2040 the modifications into the change group. Return false if we did
2041 not see how to do that. */
2042
2043int
2044redirect_jump_1 (jump, nlabel)
2045 rtx jump, nlabel;
2046{
2047 int ochanges = num_validated_changes ();
ba08b7e7 2048 rtx *loc;
2049
2050 if (GET_CODE (PATTERN (jump)) == PARALLEL)
2051 loc = &XVECEXP (PATTERN (jump), 0, 0);
2052 else
2053 loc = &PATTERN (jump);
2054
2055 redirect_exp_1 (loc, JUMP_LABEL (jump), nlabel, jump);
a8b5d014 2056 return num_validated_changes () > ochanges;
2057}
2058
2059/* Make JUMP go to NLABEL instead of where it jumps now. If the old
2060 jump target label is unused as a result, it and the code following
2061 it may be deleted.
5924de0b 2062
2063 If NLABEL is zero, we are to turn the jump into a (possibly conditional)
2064 RETURN insn.
2065
a8b5d014 2066 The return value will be 1 if the change was made, 0 if it wasn't
2067 (this can only occur for NLABEL == 0). */
5924de0b 2068
2069int
f8cacb57 2070redirect_jump (jump, nlabel, delete_unused)
5924de0b 2071 rtx jump, nlabel;
f8cacb57 2072 int delete_unused;
5924de0b 2073{
19cb6b50 2074 rtx olabel = JUMP_LABEL (jump);
5924de0b 2075
2076 if (nlabel == olabel)
2077 return 1;
2078
ba08b7e7 2079 if (! redirect_exp (olabel, nlabel, jump))
5924de0b 2080 return 0;
2081
5924de0b 2082 JUMP_LABEL (jump) = nlabel;
2083 if (nlabel)
2084 ++LABEL_NUSES (nlabel);
2085
9cf49039 2086 /* If we're eliding the jump over exception cleanups at the end of a
2087 function, move the function end note so that -Wreturn-type works. */
4476207f 2088 if (olabel && nlabel
2089 && NEXT_INSN (olabel)
9cf49039 2090 && GET_CODE (NEXT_INSN (olabel)) == NOTE
2091 && NOTE_LINE_NUMBER (NEXT_INSN (olabel)) == NOTE_INSN_FUNCTION_END)
2092 emit_note_after (NOTE_INSN_FUNCTION_END, nlabel);
2093
f8cacb57 2094 if (olabel && --LABEL_NUSES (olabel) == 0 && delete_unused)
e4bf866d 2095 delete_related_insns (olabel);
5924de0b 2096
2097 return 1;
2098}
2099
7113a566 2100/* Invert the jump condition of rtx X contained in jump insn, INSN.
a8b5d014 2101 Accrue the modifications into the change group. */
2102
2103static void
ba08b7e7 2104invert_exp_1 (insn)
a8b5d014 2105 rtx insn;
2106{
19cb6b50 2107 RTX_CODE code;
ba08b7e7 2108 rtx x = pc_set (insn);
2109
2110 if (!x)
7113a566 2111 abort ();
ba08b7e7 2112 x = SET_SRC (x);
a8b5d014 2113
2114 code = GET_CODE (x);
2115
2116 if (code == IF_THEN_ELSE)
2117 {
19cb6b50 2118 rtx comp = XEXP (x, 0);
2119 rtx tem;
7da6ea0c 2120 enum rtx_code reversed_code;
a8b5d014 2121
2122 /* We can do this in two ways: The preferable way, which can only
2123 be done if this is not an integer comparison, is to reverse
2124 the comparison code. Otherwise, swap the THEN-part and ELSE-part
2125 of the IF_THEN_ELSE. If we can't do either, fail. */
2126
7da6ea0c 2127 reversed_code = reversed_comparison_code (comp, insn);
2128
2129 if (reversed_code != UNKNOWN)
a8b5d014 2130 {
2131 validate_change (insn, &XEXP (x, 0),
7da6ea0c 2132 gen_rtx_fmt_ee (reversed_code,
a8b5d014 2133 GET_MODE (comp), XEXP (comp, 0),
2134 XEXP (comp, 1)),
2135 1);
2136 return;
2137 }
7113a566 2138
a8b5d014 2139 tem = XEXP (x, 1);
2140 validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
2141 validate_change (insn, &XEXP (x, 2), tem, 1);
a8b5d014 2142 }
ba08b7e7 2143 else
2144 abort ();
a8b5d014 2145}
2146
7113a566 2147/* Invert the jump condition of conditional jump insn, INSN.
a8b5d014 2148
2149 Return 1 if we can do so, 0 if we cannot find a way to do so that
2150 matches a pattern. */
2151
ba08b7e7 2152static int
2153invert_exp (insn)
a8b5d014 2154 rtx insn;
2155{
ba08b7e7 2156 invert_exp_1 (insn);
a8b5d014 2157 if (num_validated_changes () == 0)
2158 return 0;
2159
2160 return apply_change_group ();
2161}
2162
2163/* Invert the condition of the jump JUMP, and make it jump to label
2164 NLABEL instead of where it jumps now. Accrue changes into the
2165 change group. Return false if we didn't see how to perform the
2166 inversion and redirection. */
2167
2168int
2169invert_jump_1 (jump, nlabel)
2170 rtx jump, nlabel;
2171{
2172 int ochanges;
2173
2174 ochanges = num_validated_changes ();
ba08b7e7 2175 invert_exp_1 (jump);
a8b5d014 2176 if (num_validated_changes () == ochanges)
2177 return 0;
2178
2179 return redirect_jump_1 (jump, nlabel);
2180}
2181
2182/* Invert the condition of the jump JUMP, and make it jump to label
2183 NLABEL instead of where it jumps now. Return true if successful. */
2184
2185int
f8cacb57 2186invert_jump (jump, nlabel, delete_unused)
a8b5d014 2187 rtx jump, nlabel;
f8cacb57 2188 int delete_unused;
a8b5d014 2189{
2190 /* We have to either invert the condition and change the label or
2191 do neither. Either operation could fail. We first try to invert
2192 the jump. If that succeeds, we try changing the label. If that fails,
2193 we invert the jump back to what it was. */
2194
ba08b7e7 2195 if (! invert_exp (jump))
a8b5d014 2196 return 0;
2197
f8cacb57 2198 if (redirect_jump (jump, nlabel, delete_unused))
a8b5d014 2199 {
13488c51 2200 invert_br_probabilities (jump);
a8b5d014 2201
2202 return 1;
2203 }
2204
ba08b7e7 2205 if (! invert_exp (jump))
a8b5d014 2206 /* This should just be putting it back the way it was. */
2207 abort ();
2208
2209 return 0;
2210}
2211
5924de0b 2212\f
2213/* Like rtx_equal_p except that it considers two REGs as equal
6c60c295 2214 if they renumber to the same value and considers two commutative
2215 operations to be the same if the order of the operands has been
2207ad6a 2216 reversed.
2217
2218 ??? Addition is not commutative on the PA due to the weird implicit
2219 space register selection rules for memory addresses. Therefore, we
2220 don't consider a + b == b + a.
2221
2222 We could/should make this test a little tighter. Possibly only
2223 disabling it on the PA via some backend macro or only disabling this
2224 case when the PLUS is inside a MEM. */
5924de0b 2225
2226int
2227rtx_renumbered_equal_p (x, y)
2228 rtx x, y;
2229{
19cb6b50 2230 int i;
2231 RTX_CODE code = GET_CODE (x);
2232 const char *fmt;
7113a566 2233
5924de0b 2234 if (x == y)
2235 return 1;
6c60c295 2236
5924de0b 2237 if ((code == REG || (code == SUBREG && GET_CODE (SUBREG_REG (x)) == REG))
2238 && (GET_CODE (y) == REG || (GET_CODE (y) == SUBREG
2239 && GET_CODE (SUBREG_REG (y)) == REG)))
2240 {
6c60c295 2241 int reg_x = -1, reg_y = -1;
701e46d0 2242 int byte_x = 0, byte_y = 0;
5924de0b 2243
2244 if (GET_MODE (x) != GET_MODE (y))
2245 return 0;
2246
2247 /* If we haven't done any renumbering, don't
2248 make any assumptions. */
2249 if (reg_renumber == 0)
2250 return rtx_equal_p (x, y);
2251
2252 if (code == SUBREG)
2253 {
6c60c295 2254 reg_x = REGNO (SUBREG_REG (x));
701e46d0 2255 byte_x = SUBREG_BYTE (x);
6c60c295 2256
2257 if (reg_renumber[reg_x] >= 0)
2258 {
701e46d0 2259 reg_x = subreg_regno_offset (reg_renumber[reg_x],
2260 GET_MODE (SUBREG_REG (x)),
2261 byte_x,
2262 GET_MODE (x));
2263 byte_x = 0;
6c60c295 2264 }
5924de0b 2265 }
2266 else
2267 {
6c60c295 2268 reg_x = REGNO (x);
2269 if (reg_renumber[reg_x] >= 0)
2270 reg_x = reg_renumber[reg_x];
5924de0b 2271 }
6c60c295 2272
5924de0b 2273 if (GET_CODE (y) == SUBREG)
2274 {
6c60c295 2275 reg_y = REGNO (SUBREG_REG (y));
701e46d0 2276 byte_y = SUBREG_BYTE (y);
6c60c295 2277
2278 if (reg_renumber[reg_y] >= 0)
2279 {
701e46d0 2280 reg_y = subreg_regno_offset (reg_renumber[reg_y],
2281 GET_MODE (SUBREG_REG (y)),
2282 byte_y,
2283 GET_MODE (y));
2284 byte_y = 0;
6c60c295 2285 }
5924de0b 2286 }
2287 else
2288 {
6c60c295 2289 reg_y = REGNO (y);
2290 if (reg_renumber[reg_y] >= 0)
2291 reg_y = reg_renumber[reg_y];
5924de0b 2292 }
6c60c295 2293
701e46d0 2294 return reg_x >= 0 && reg_x == reg_y && byte_x == byte_y;
5924de0b 2295 }
6c60c295 2296
7113a566 2297 /* Now we have disposed of all the cases
5924de0b 2298 in which different rtx codes can match. */
2299 if (code != GET_CODE (y))
2300 return 0;
6c60c295 2301
5924de0b 2302 switch (code)
2303 {
2304 case PC:
2305 case CC0:
2306 case ADDR_VEC:
2307 case ADDR_DIFF_VEC:
2308 return 0;
2309
2310 case CONST_INT:
5fbd420b 2311 return INTVAL (x) == INTVAL (y);
5924de0b 2312
2313 case LABEL_REF:
f08cae9d 2314 /* We can't assume nonlocal labels have their following insns yet. */
2315 if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
2316 return XEXP (x, 0) == XEXP (y, 0);
6c60c295 2317
5924de0b 2318 /* Two label-refs are equivalent if they point at labels
2319 in the same position in the instruction stream. */
2320 return (next_real_insn (XEXP (x, 0))
2321 == next_real_insn (XEXP (y, 0)));
2322
2323 case SYMBOL_REF:
2324 return XSTR (x, 0) == XSTR (y, 0);
0dbd1c74 2325
fc41ccae 2326 case CODE_LABEL:
2327 /* If we didn't match EQ equality above, they aren't the same. */
2328 return 0;
2329
0dbd1c74 2330 default:
2331 break;
5924de0b 2332 }
2333
2334 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
2335
2336 if (GET_MODE (x) != GET_MODE (y))
2337 return 0;
2338
6c60c295 2339 /* For commutative operations, the RTX match if the operand match in any
2207ad6a 2340 order. Also handle the simple binary and unary cases without a loop.
2341
2342 ??? Don't consider PLUS a commutative operator; see comments above. */
2343 if ((code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
2344 && code != PLUS)
6c60c295 2345 return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
2346 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
2347 || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
2348 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
2349 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
2350 return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
2351 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
2352 else if (GET_RTX_CLASS (code) == '1')
2353 return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
2354
5924de0b 2355 /* Compare the elements. If any pair of corresponding elements
2356 fail to match, return 0 for the whole things. */
2357
2358 fmt = GET_RTX_FORMAT (code);
2359 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2360 {
19cb6b50 2361 int j;
5924de0b 2362 switch (fmt[i])
2363 {
1bb04728 2364 case 'w':
2365 if (XWINT (x, i) != XWINT (y, i))
2366 return 0;
2367 break;
2368
5924de0b 2369 case 'i':
2370 if (XINT (x, i) != XINT (y, i))
2371 return 0;
2372 break;
2373
a0d79d69 2374 case 't':
2375 if (XTREE (x, i) != XTREE (y, i))
2376 return 0;
2377 break;
2378
5924de0b 2379 case 's':
2380 if (strcmp (XSTR (x, i), XSTR (y, i)))
2381 return 0;
2382 break;
2383
2384 case 'e':
2385 if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
2386 return 0;
2387 break;
2388
2389 case 'u':
2390 if (XEXP (x, i) != XEXP (y, i))
2391 return 0;
2392 /* fall through. */
2393 case '0':
2394 break;
2395
2396 case 'E':
2397 if (XVECLEN (x, i) != XVECLEN (y, i))
2398 return 0;
2399 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2400 if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
2401 return 0;
2402 break;
2403
2404 default:
2405 abort ();
2406 }
2407 }
2408 return 1;
2409}
2410\f
2411/* If X is a hard register or equivalent to one or a subregister of one,
2412 return the hard register number. If X is a pseudo register that was not
2413 assigned a hard register, return the pseudo register number. Otherwise,
2414 return -1. Any rtx is valid for X. */
2415
2416int
2417true_regnum (x)
2418 rtx x;
2419{
2420 if (GET_CODE (x) == REG)
2421 {
2422 if (REGNO (x) >= FIRST_PSEUDO_REGISTER && reg_renumber[REGNO (x)] >= 0)
2423 return reg_renumber[REGNO (x)];
2424 return REGNO (x);
2425 }
2426 if (GET_CODE (x) == SUBREG)
2427 {
2428 int base = true_regnum (SUBREG_REG (x));
2429 if (base >= 0 && base < FIRST_PSEUDO_REGISTER)
701e46d0 2430 return base + subreg_regno_offset (REGNO (SUBREG_REG (x)),
2431 GET_MODE (SUBREG_REG (x)),
2432 SUBREG_BYTE (x), GET_MODE (x));
5924de0b 2433 }
2434 return -1;
2435}