Update copyright years.
[platform/upstream/gcc.git] / gcc / jump.c
1 /* Optimize jump instructions, for GNU compiler.
2    Copyright (C) 1987-2017 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 /* This is the pathetic reminder of old fame of the jump-optimization pass
21    of the compiler.  Now it contains basically a set of utility functions to
22    operate with jumps.
23
24    Each CODE_LABEL has a count of the times it is used
25    stored in the LABEL_NUSES internal field, and each JUMP_INSN
26    has one label that it refers to stored in the
27    JUMP_LABEL internal field.  With this we can detect labels that
28    become unused because of the deletion of all the jumps that
29    formerly used them.  The JUMP_LABEL info is sometimes looked
30    at by later passes.  For return insns, it contains either a
31    RETURN or a SIMPLE_RETURN rtx.
32
33    The subroutines redirect_jump and invert_jump are used
34    from other passes as well.  */
35
36 #include "config.h"
37 #include "system.h"
38 #include "coretypes.h"
39 #include "backend.h"
40 #include "target.h"
41 #include "rtl.h"
42 #include "tree.h"
43 #include "cfghooks.h"
44 #include "tree-pass.h"
45 #include "memmodel.h"
46 #include "tm_p.h"
47 #include "insn-config.h"
48 #include "regs.h"
49 #include "emit-rtl.h"
50 #include "recog.h"
51 #include "cfgrtl.h"
52 #include "rtl-iter.h"
53
54 /* Optimize jump y; x: ... y: jumpif... x?
55    Don't know if it is worth bothering with.  */
56 /* Optimize two cases of conditional jump to conditional jump?
57    This can never delete any instruction or make anything dead,
58    or even change what is live at any point.
59    So perhaps let combiner do it.  */
60
61 static void init_label_info (rtx_insn *);
62 static void mark_all_labels (rtx_insn *);
63 static void mark_jump_label_1 (rtx, rtx_insn *, bool, bool);
64 static void mark_jump_label_asm (rtx, rtx_insn *);
65 static void redirect_exp_1 (rtx *, rtx, rtx, rtx_insn *);
66 static int invert_exp_1 (rtx, rtx_insn *);
67 \f
68 /* Worker for rebuild_jump_labels and rebuild_jump_labels_chain.  */
69 static void
70 rebuild_jump_labels_1 (rtx_insn *f, bool count_forced)
71 {
72   timevar_push (TV_REBUILD_JUMP);
73   init_label_info (f);
74   mark_all_labels (f);
75
76   /* Keep track of labels used from static data; we don't track them
77      closely enough to delete them here, so make sure their reference
78      count doesn't drop to zero.  */
79
80   if (count_forced)
81     {
82       rtx_insn *insn;
83       unsigned int i;
84       FOR_EACH_VEC_SAFE_ELT (forced_labels, i, insn)
85         if (LABEL_P (insn))
86           LABEL_NUSES (insn)++;
87     }
88   timevar_pop (TV_REBUILD_JUMP);
89 }
90
91 /* This function rebuilds the JUMP_LABEL field and REG_LABEL_TARGET
92    notes in jumping insns and REG_LABEL_OPERAND notes in non-jumping
93    instructions and jumping insns that have labels as operands
94    (e.g. cbranchsi4).  */
95 void
96 rebuild_jump_labels (rtx_insn *f)
97 {
98   rebuild_jump_labels_1 (f, true);
99 }
100
101 /* This function is like rebuild_jump_labels, but doesn't run over
102    forced_labels.  It can be used on insn chains that aren't the 
103    main function chain.  */
104 void
105 rebuild_jump_labels_chain (rtx_insn *chain)
106 {
107   rebuild_jump_labels_1 (chain, false);
108 }
109 \f
110 /* Some old code expects exactly one BARRIER as the NEXT_INSN of a
111    non-fallthru insn.  This is not generally true, as multiple barriers
112    may have crept in, or the BARRIER may be separated from the last
113    real insn by one or more NOTEs.
114
115    This simple pass moves barriers and removes duplicates so that the
116    old code is happy.
117  */
118 static unsigned int
119 cleanup_barriers (void)
120 {
121   rtx_insn *insn;
122   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
123     {
124       if (BARRIER_P (insn))
125         {
126           rtx_insn *prev = prev_nonnote_insn (insn);
127           if (!prev)
128             continue;
129
130           if (CALL_P (prev))
131             {
132               /* Make sure we do not split a call and its corresponding
133                  CALL_ARG_LOCATION note.  */
134               rtx_insn *next = NEXT_INSN (prev);
135
136               if (NOTE_P (next)
137                   && NOTE_KIND (next) == NOTE_INSN_CALL_ARG_LOCATION)
138                 prev = next;
139             }
140
141           if (BARRIER_P (prev))
142             delete_insn (insn);
143           else if (prev != PREV_INSN (insn))
144             {
145               basic_block bb = BLOCK_FOR_INSN (prev);
146               rtx_insn *end = PREV_INSN (insn);
147               reorder_insns_nobb (insn, insn, prev);
148               if (bb)
149                 {
150                   /* If the backend called in machine reorg compute_bb_for_insn
151                      and didn't free_bb_for_insn again, preserve basic block
152                      boundaries.  Move the end of basic block to PREV since
153                      it is followed by a barrier now, and clear BLOCK_FOR_INSN
154                      on the following notes.
155                      ???  Maybe the proper solution for the targets that have
156                      cfg around after machine reorg is not to run cleanup_barriers
157                      pass at all.  */
158                   BB_END (bb) = prev;
159                   do
160                     {
161                       prev = NEXT_INSN (prev);
162                       if (prev != insn && BLOCK_FOR_INSN (prev) == bb)
163                         BLOCK_FOR_INSN (prev) = NULL;
164                     }
165                   while (prev != end);
166                 }
167             }
168         }
169     }
170   return 0;
171 }
172
173 namespace {
174
175 const pass_data pass_data_cleanup_barriers =
176 {
177   RTL_PASS, /* type */
178   "barriers", /* name */
179   OPTGROUP_NONE, /* optinfo_flags */
180   TV_NONE, /* tv_id */
181   0, /* properties_required */
182   0, /* properties_provided */
183   0, /* properties_destroyed */
184   0, /* todo_flags_start */
185   0, /* todo_flags_finish */
186 };
187
188 class pass_cleanup_barriers : public rtl_opt_pass
189 {
190 public:
191   pass_cleanup_barriers (gcc::context *ctxt)
192     : rtl_opt_pass (pass_data_cleanup_barriers, ctxt)
193   {}
194
195   /* opt_pass methods: */
196   virtual unsigned int execute (function *) { return cleanup_barriers (); }
197
198 }; // class pass_cleanup_barriers
199
200 } // anon namespace
201
202 rtl_opt_pass *
203 make_pass_cleanup_barriers (gcc::context *ctxt)
204 {
205   return new pass_cleanup_barriers (ctxt);
206 }
207
208 \f
209 /* Initialize LABEL_NUSES and JUMP_LABEL fields, add REG_LABEL_TARGET
210    for remaining targets for JUMP_P.  Delete any REG_LABEL_OPERAND
211    notes whose labels don't occur in the insn any more.  */
212
213 static void
214 init_label_info (rtx_insn *f)
215 {
216   rtx_insn *insn;
217
218   for (insn = f; insn; insn = NEXT_INSN (insn))
219     {
220       if (LABEL_P (insn))
221         LABEL_NUSES (insn) = (LABEL_PRESERVE_P (insn) != 0);
222
223       /* REG_LABEL_TARGET notes (including the JUMP_LABEL field) are
224          sticky and not reset here; that way we won't lose association
225          with a label when e.g. the source for a target register
226          disappears out of reach for targets that may use jump-target
227          registers.  Jump transformations are supposed to transform
228          any REG_LABEL_TARGET notes.  The target label reference in a
229          branch may disappear from the branch (and from the
230          instruction before it) for other reasons, like register
231          allocation.  */
232
233       if (INSN_P (insn))
234         {
235           rtx note, next;
236
237           for (note = REG_NOTES (insn); note; note = next)
238             {
239               next = XEXP (note, 1);
240               if (REG_NOTE_KIND (note) == REG_LABEL_OPERAND
241                   && ! reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
242                 remove_note (insn, note);
243             }
244         }
245     }
246 }
247
248 /* A subroutine of mark_all_labels.  Trivially propagate a simple label
249    load into a jump_insn that uses it.  */
250
251 static void
252 maybe_propagate_label_ref (rtx_insn *jump_insn, rtx_insn *prev_nonjump_insn)
253 {
254   rtx label_note, pc, pc_src;
255
256   pc = pc_set (jump_insn);
257   pc_src = pc != NULL ? SET_SRC (pc) : NULL;
258   label_note = find_reg_note (prev_nonjump_insn, REG_LABEL_OPERAND, NULL);
259
260   /* If the previous non-jump insn sets something to a label,
261      something that this jump insn uses, make that label the primary
262      target of this insn if we don't yet have any.  That previous
263      insn must be a single_set and not refer to more than one label.
264      The jump insn must not refer to other labels as jump targets
265      and must be a plain (set (pc) ...), maybe in a parallel, and
266      may refer to the item being set only directly or as one of the
267      arms in an IF_THEN_ELSE.  */
268
269   if (label_note != NULL && pc_src != NULL)
270     {
271       rtx label_set = single_set (prev_nonjump_insn);
272       rtx label_dest = label_set != NULL ? SET_DEST (label_set) : NULL;
273
274       if (label_set != NULL
275           /* The source must be the direct LABEL_REF, not a
276              PLUS, UNSPEC, IF_THEN_ELSE etc.  */
277           && GET_CODE (SET_SRC (label_set)) == LABEL_REF
278           && (rtx_equal_p (label_dest, pc_src)
279               || (GET_CODE (pc_src) == IF_THEN_ELSE
280                   && (rtx_equal_p (label_dest, XEXP (pc_src, 1))
281                       || rtx_equal_p (label_dest, XEXP (pc_src, 2))))))
282         {
283           /* The CODE_LABEL referred to in the note must be the
284              CODE_LABEL in the LABEL_REF of the "set".  We can
285              conveniently use it for the marker function, which
286              requires a LABEL_REF wrapping.  */
287           gcc_assert (XEXP (label_note, 0) == label_ref_label (SET_SRC (label_set)));
288
289           mark_jump_label_1 (label_set, jump_insn, false, true);
290
291           gcc_assert (JUMP_LABEL (jump_insn) == XEXP (label_note, 0));
292         }
293     }
294 }
295
296 /* Mark the label each jump jumps to.
297    Combine consecutive labels, and count uses of labels.  */
298
299 static void
300 mark_all_labels (rtx_insn *f)
301 {
302   rtx_insn *insn;
303
304   if (current_ir_type () == IR_RTL_CFGLAYOUT)
305     {
306       basic_block bb;
307       FOR_EACH_BB_FN (bb, cfun)
308         {
309           /* In cfglayout mode, we don't bother with trivial next-insn
310              propagation of LABEL_REFs into JUMP_LABEL.  This will be
311              handled by other optimizers using better algorithms.  */
312           FOR_BB_INSNS (bb, insn)
313             {
314               gcc_assert (! insn->deleted ());
315               if (NONDEBUG_INSN_P (insn))
316                 mark_jump_label (PATTERN (insn), insn, 0);
317             }
318
319           /* In cfglayout mode, there may be non-insns between the
320              basic blocks.  If those non-insns represent tablejump data,
321              they contain label references that we must record.  */
322           for (insn = BB_HEADER (bb); insn; insn = NEXT_INSN (insn))
323             if (JUMP_TABLE_DATA_P (insn))
324               mark_jump_label (PATTERN (insn), insn, 0);
325           for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
326             if (JUMP_TABLE_DATA_P (insn))
327               mark_jump_label (PATTERN (insn), insn, 0);
328         }
329     }
330   else
331     {
332       rtx_insn *prev_nonjump_insn = NULL;
333       for (insn = f; insn; insn = NEXT_INSN (insn))
334         {
335           if (insn->deleted ())
336             ;
337           else if (LABEL_P (insn))
338             prev_nonjump_insn = NULL;
339           else if (JUMP_TABLE_DATA_P (insn))
340             mark_jump_label (PATTERN (insn), insn, 0);
341           else if (NONDEBUG_INSN_P (insn))
342             {
343               mark_jump_label (PATTERN (insn), insn, 0);
344               if (JUMP_P (insn))
345                 {
346                   if (JUMP_LABEL (insn) == NULL && prev_nonjump_insn != NULL)
347                     maybe_propagate_label_ref (insn, prev_nonjump_insn);
348                 }
349               else
350                 prev_nonjump_insn = insn;
351             }
352         }
353     }
354 }
355 \f
356 /* Given a comparison (CODE ARG0 ARG1), inside an insn, INSN, return a code
357    of reversed comparison if it is possible to do so.  Otherwise return UNKNOWN.
358    UNKNOWN may be returned in case we are having CC_MODE compare and we don't
359    know whether it's source is floating point or integer comparison.  Machine
360    description should define REVERSIBLE_CC_MODE and REVERSE_CONDITION macros
361    to help this function avoid overhead in these cases.  */
362 enum rtx_code
363 reversed_comparison_code_parts (enum rtx_code code, const_rtx arg0,
364                                 const_rtx arg1, const rtx_insn *insn)
365 {
366   machine_mode mode;
367
368   /* If this is not actually a comparison, we can't reverse it.  */
369   if (GET_RTX_CLASS (code) != RTX_COMPARE
370       && GET_RTX_CLASS (code) != RTX_COMM_COMPARE)
371     return UNKNOWN;
372
373   mode = GET_MODE (arg0);
374   if (mode == VOIDmode)
375     mode = GET_MODE (arg1);
376
377   /* First see if machine description supplies us way to reverse the
378      comparison.  Give it priority over everything else to allow
379      machine description to do tricks.  */
380   if (GET_MODE_CLASS (mode) == MODE_CC
381       && REVERSIBLE_CC_MODE (mode))
382     return REVERSE_CONDITION (code, mode);
383
384   /* Try a few special cases based on the comparison code.  */
385   switch (code)
386     {
387     case GEU:
388     case GTU:
389     case LEU:
390     case LTU:
391     case NE:
392     case EQ:
393       /* It is always safe to reverse EQ and NE, even for the floating
394          point.  Similarly the unsigned comparisons are never used for
395          floating point so we can reverse them in the default way.  */
396       return reverse_condition (code);
397     case ORDERED:
398     case UNORDERED:
399     case LTGT:
400     case UNEQ:
401       /* In case we already see unordered comparison, we can be sure to
402          be dealing with floating point so we don't need any more tests.  */
403       return reverse_condition_maybe_unordered (code);
404     case UNLT:
405     case UNLE:
406     case UNGT:
407     case UNGE:
408       /* We don't have safe way to reverse these yet.  */
409       return UNKNOWN;
410     default:
411       break;
412     }
413
414   if (GET_MODE_CLASS (mode) == MODE_CC || CC0_P (arg0))
415     {
416       /* Try to search for the comparison to determine the real mode.
417          This code is expensive, but with sane machine description it
418          will be never used, since REVERSIBLE_CC_MODE will return true
419          in all cases.  */
420       if (! insn)
421         return UNKNOWN;
422
423       /* These CONST_CAST's are okay because prev_nonnote_insn just
424          returns its argument and we assign it to a const_rtx
425          variable.  */
426       for (rtx_insn *prev = prev_nonnote_insn (const_cast<rtx_insn *> (insn));
427            prev != 0 && !LABEL_P (prev);
428            prev = prev_nonnote_insn (prev))
429         {
430           const_rtx set = set_of (arg0, prev);
431           if (set && GET_CODE (set) == SET
432               && rtx_equal_p (SET_DEST (set), arg0))
433             {
434               rtx src = SET_SRC (set);
435
436               if (GET_CODE (src) == COMPARE)
437                 {
438                   rtx comparison = src;
439                   arg0 = XEXP (src, 0);
440                   mode = GET_MODE (arg0);
441                   if (mode == VOIDmode)
442                     mode = GET_MODE (XEXP (comparison, 1));
443                   break;
444                 }
445               /* We can get past reg-reg moves.  This may be useful for model
446                  of i387 comparisons that first move flag registers around.  */
447               if (REG_P (src))
448                 {
449                   arg0 = src;
450                   continue;
451                 }
452             }
453           /* If register is clobbered in some ununderstandable way,
454              give up.  */
455           if (set)
456             return UNKNOWN;
457         }
458     }
459
460   /* Test for an integer condition, or a floating-point comparison
461      in which NaNs can be ignored.  */
462   if (CONST_INT_P (arg0)
463       || (GET_MODE (arg0) != VOIDmode
464           && GET_MODE_CLASS (mode) != MODE_CC
465           && !HONOR_NANS (mode)))
466     return reverse_condition (code);
467
468   return UNKNOWN;
469 }
470
471 /* A wrapper around the previous function to take COMPARISON as rtx
472    expression.  This simplifies many callers.  */
473 enum rtx_code
474 reversed_comparison_code (const_rtx comparison, const rtx_insn *insn)
475 {
476   if (!COMPARISON_P (comparison))
477     return UNKNOWN;
478   return reversed_comparison_code_parts (GET_CODE (comparison),
479                                          XEXP (comparison, 0),
480                                          XEXP (comparison, 1), insn);
481 }
482
483 /* Return comparison with reversed code of EXP.
484    Return NULL_RTX in case we fail to do the reversal.  */
485 rtx
486 reversed_comparison (const_rtx exp, machine_mode mode)
487 {
488   enum rtx_code reversed_code = reversed_comparison_code (exp, NULL);
489   if (reversed_code == UNKNOWN)
490     return NULL_RTX;
491   else
492     return simplify_gen_relational (reversed_code, mode, VOIDmode,
493                                     XEXP (exp, 0), XEXP (exp, 1));
494 }
495
496 \f
497 /* Given an rtx-code for a comparison, return the code for the negated
498    comparison.  If no such code exists, return UNKNOWN.
499
500    WATCH OUT!  reverse_condition is not safe to use on a jump that might
501    be acting on the results of an IEEE floating point comparison, because
502    of the special treatment of non-signaling nans in comparisons.
503    Use reversed_comparison_code instead.  */
504
505 enum rtx_code
506 reverse_condition (enum rtx_code code)
507 {
508   switch (code)
509     {
510     case EQ:
511       return NE;
512     case NE:
513       return EQ;
514     case GT:
515       return LE;
516     case GE:
517       return LT;
518     case LT:
519       return GE;
520     case LE:
521       return GT;
522     case GTU:
523       return LEU;
524     case GEU:
525       return LTU;
526     case LTU:
527       return GEU;
528     case LEU:
529       return GTU;
530     case UNORDERED:
531       return ORDERED;
532     case ORDERED:
533       return UNORDERED;
534
535     case UNLT:
536     case UNLE:
537     case UNGT:
538     case UNGE:
539     case UNEQ:
540     case LTGT:
541       return UNKNOWN;
542
543     default:
544       gcc_unreachable ();
545     }
546 }
547
548 /* Similar, but we're allowed to generate unordered comparisons, which
549    makes it safe for IEEE floating-point.  Of course, we have to recognize
550    that the target will support them too...  */
551
552 enum rtx_code
553 reverse_condition_maybe_unordered (enum rtx_code code)
554 {
555   switch (code)
556     {
557     case EQ:
558       return NE;
559     case NE:
560       return EQ;
561     case GT:
562       return UNLE;
563     case GE:
564       return UNLT;
565     case LT:
566       return UNGE;
567     case LE:
568       return UNGT;
569     case LTGT:
570       return UNEQ;
571     case UNORDERED:
572       return ORDERED;
573     case ORDERED:
574       return UNORDERED;
575     case UNLT:
576       return GE;
577     case UNLE:
578       return GT;
579     case UNGT:
580       return LE;
581     case UNGE:
582       return LT;
583     case UNEQ:
584       return LTGT;
585
586     default:
587       gcc_unreachable ();
588     }
589 }
590
591 /* Similar, but return the code when two operands of a comparison are swapped.
592    This IS safe for IEEE floating-point.  */
593
594 enum rtx_code
595 swap_condition (enum rtx_code code)
596 {
597   switch (code)
598     {
599     case EQ:
600     case NE:
601     case UNORDERED:
602     case ORDERED:
603     case UNEQ:
604     case LTGT:
605       return code;
606
607     case GT:
608       return LT;
609     case GE:
610       return LE;
611     case LT:
612       return GT;
613     case LE:
614       return GE;
615     case GTU:
616       return LTU;
617     case GEU:
618       return LEU;
619     case LTU:
620       return GTU;
621     case LEU:
622       return GEU;
623     case UNLT:
624       return UNGT;
625     case UNLE:
626       return UNGE;
627     case UNGT:
628       return UNLT;
629     case UNGE:
630       return UNLE;
631
632     default:
633       gcc_unreachable ();
634     }
635 }
636
637 /* Given a comparison CODE, return the corresponding unsigned comparison.
638    If CODE is an equality comparison or already an unsigned comparison,
639    CODE is returned.  */
640
641 enum rtx_code
642 unsigned_condition (enum rtx_code code)
643 {
644   switch (code)
645     {
646     case EQ:
647     case NE:
648     case GTU:
649     case GEU:
650     case LTU:
651     case LEU:
652       return code;
653
654     case GT:
655       return GTU;
656     case GE:
657       return GEU;
658     case LT:
659       return LTU;
660     case LE:
661       return LEU;
662
663     default:
664       gcc_unreachable ();
665     }
666 }
667
668 /* Similarly, return the signed version of a comparison.  */
669
670 enum rtx_code
671 signed_condition (enum rtx_code code)
672 {
673   switch (code)
674     {
675     case EQ:
676     case NE:
677     case GT:
678     case GE:
679     case LT:
680     case LE:
681       return code;
682
683     case GTU:
684       return GT;
685     case GEU:
686       return GE;
687     case LTU:
688       return LT;
689     case LEU:
690       return LE;
691
692     default:
693       gcc_unreachable ();
694     }
695 }
696 \f
697 /* Return nonzero if CODE1 is more strict than CODE2, i.e., if the
698    truth of CODE1 implies the truth of CODE2.  */
699
700 int
701 comparison_dominates_p (enum rtx_code code1, enum rtx_code code2)
702 {
703   /* UNKNOWN comparison codes can happen as a result of trying to revert
704      comparison codes.
705      They can't match anything, so we have to reject them here.  */
706   if (code1 == UNKNOWN || code2 == UNKNOWN)
707     return 0;
708
709   if (code1 == code2)
710     return 1;
711
712   switch (code1)
713     {
714     case UNEQ:
715       if (code2 == UNLE || code2 == UNGE)
716         return 1;
717       break;
718
719     case EQ:
720       if (code2 == LE || code2 == LEU || code2 == GE || code2 == GEU
721           || code2 == ORDERED)
722         return 1;
723       break;
724
725     case UNLT:
726       if (code2 == UNLE || code2 == NE)
727         return 1;
728       break;
729
730     case LT:
731       if (code2 == LE || code2 == NE || code2 == ORDERED || code2 == LTGT)
732         return 1;
733       break;
734
735     case UNGT:
736       if (code2 == UNGE || code2 == NE)
737         return 1;
738       break;
739
740     case GT:
741       if (code2 == GE || code2 == NE || code2 == ORDERED || code2 == LTGT)
742         return 1;
743       break;
744
745     case GE:
746     case LE:
747       if (code2 == ORDERED)
748         return 1;
749       break;
750
751     case LTGT:
752       if (code2 == NE || code2 == ORDERED)
753         return 1;
754       break;
755
756     case LTU:
757       if (code2 == LEU || code2 == NE)
758         return 1;
759       break;
760
761     case GTU:
762       if (code2 == GEU || code2 == NE)
763         return 1;
764       break;
765
766     case UNORDERED:
767       if (code2 == NE || code2 == UNEQ || code2 == UNLE || code2 == UNLT
768           || code2 == UNGE || code2 == UNGT)
769         return 1;
770       break;
771
772     default:
773       break;
774     }
775
776   return 0;
777 }
778 \f
779 /* Return 1 if INSN is an unconditional jump and nothing else.  */
780
781 int
782 simplejump_p (const rtx_insn *insn)
783 {
784   return (JUMP_P (insn)
785           && GET_CODE (PATTERN (insn)) == SET
786           && GET_CODE (SET_DEST (PATTERN (insn))) == PC
787           && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF);
788 }
789
790 /* Return nonzero if INSN is a (possibly) conditional jump
791    and nothing more.
792
793    Use of this function is deprecated, since we need to support combined
794    branch and compare insns.  Use any_condjump_p instead whenever possible.  */
795
796 int
797 condjump_p (const rtx_insn *insn)
798 {
799   const_rtx x = PATTERN (insn);
800
801   if (GET_CODE (x) != SET
802       || GET_CODE (SET_DEST (x)) != PC)
803     return 0;
804
805   x = SET_SRC (x);
806   if (GET_CODE (x) == LABEL_REF)
807     return 1;
808   else
809     return (GET_CODE (x) == IF_THEN_ELSE
810             && ((GET_CODE (XEXP (x, 2)) == PC
811                  && (GET_CODE (XEXP (x, 1)) == LABEL_REF
812                      || ANY_RETURN_P (XEXP (x, 1))))
813                 || (GET_CODE (XEXP (x, 1)) == PC
814                     && (GET_CODE (XEXP (x, 2)) == LABEL_REF
815                         || ANY_RETURN_P (XEXP (x, 2))))));
816 }
817
818 /* Return nonzero if INSN is a (possibly) conditional jump inside a
819    PARALLEL.
820
821    Use this function is deprecated, since we need to support combined
822    branch and compare insns.  Use any_condjump_p instead whenever possible.  */
823
824 int
825 condjump_in_parallel_p (const rtx_insn *insn)
826 {
827   const_rtx x = PATTERN (insn);
828
829   if (GET_CODE (x) != PARALLEL)
830     return 0;
831   else
832     x = XVECEXP (x, 0, 0);
833
834   if (GET_CODE (x) != SET)
835     return 0;
836   if (GET_CODE (SET_DEST (x)) != PC)
837     return 0;
838   if (GET_CODE (SET_SRC (x)) == LABEL_REF)
839     return 1;
840   if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
841     return 0;
842   if (XEXP (SET_SRC (x), 2) == pc_rtx
843       && (GET_CODE (XEXP (SET_SRC (x), 1)) == LABEL_REF
844           || ANY_RETURN_P (XEXP (SET_SRC (x), 1))))
845     return 1;
846   if (XEXP (SET_SRC (x), 1) == pc_rtx
847       && (GET_CODE (XEXP (SET_SRC (x), 2)) == LABEL_REF
848           || ANY_RETURN_P (XEXP (SET_SRC (x), 2))))
849     return 1;
850   return 0;
851 }
852
853 /* Return set of PC, otherwise NULL.  */
854
855 rtx
856 pc_set (const rtx_insn *insn)
857 {
858   rtx pat;
859   if (!JUMP_P (insn))
860     return NULL_RTX;
861   pat = PATTERN (insn);
862
863   /* The set is allowed to appear either as the insn pattern or
864      the first set in a PARALLEL.  */
865   if (GET_CODE (pat) == PARALLEL)
866     pat = XVECEXP (pat, 0, 0);
867   if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == PC)
868     return pat;
869
870   return NULL_RTX;
871 }
872
873 /* Return true when insn is an unconditional direct jump,
874    possibly bundled inside a PARALLEL.  */
875
876 int
877 any_uncondjump_p (const rtx_insn *insn)
878 {
879   const_rtx x = pc_set (insn);
880   if (!x)
881     return 0;
882   if (GET_CODE (SET_SRC (x)) != LABEL_REF)
883     return 0;
884   if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
885     return 0;
886   return 1;
887 }
888
889 /* Return true when insn is a conditional jump.  This function works for
890    instructions containing PC sets in PARALLELs.  The instruction may have
891    various other effects so before removing the jump you must verify
892    onlyjump_p.
893
894    Note that unlike condjump_p it returns false for unconditional jumps.  */
895
896 int
897 any_condjump_p (const rtx_insn *insn)
898 {
899   const_rtx x = pc_set (insn);
900   enum rtx_code a, b;
901
902   if (!x)
903     return 0;
904   if (GET_CODE (SET_SRC (x)) != IF_THEN_ELSE)
905     return 0;
906
907   a = GET_CODE (XEXP (SET_SRC (x), 1));
908   b = GET_CODE (XEXP (SET_SRC (x), 2));
909
910   return ((b == PC && (a == LABEL_REF || a == RETURN || a == SIMPLE_RETURN))
911           || (a == PC
912               && (b == LABEL_REF || b == RETURN || b == SIMPLE_RETURN)));
913 }
914
915 /* Return the label of a conditional jump.  */
916
917 rtx
918 condjump_label (const rtx_insn *insn)
919 {
920   rtx x = pc_set (insn);
921
922   if (!x)
923     return NULL_RTX;
924   x = SET_SRC (x);
925   if (GET_CODE (x) == LABEL_REF)
926     return x;
927   if (GET_CODE (x) != IF_THEN_ELSE)
928     return NULL_RTX;
929   if (XEXP (x, 2) == pc_rtx && GET_CODE (XEXP (x, 1)) == LABEL_REF)
930     return XEXP (x, 1);
931   if (XEXP (x, 1) == pc_rtx && GET_CODE (XEXP (x, 2)) == LABEL_REF)
932     return XEXP (x, 2);
933   return NULL_RTX;
934 }
935
936 /* Return TRUE if INSN is a return jump.  */
937
938 int
939 returnjump_p (const rtx_insn *insn)
940 {
941   if (JUMP_P (insn))
942     {
943       subrtx_iterator::array_type array;
944       FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
945         {
946           const_rtx x = *iter;
947           switch (GET_CODE (x))
948             {
949             case RETURN:
950             case SIMPLE_RETURN:
951             case EH_RETURN:
952               return true;
953
954             case SET:
955               if (SET_IS_RETURN_P (x))
956                 return true;
957               break;
958
959             default:
960               break;
961             }
962         }
963     }
964   return false;
965 }
966
967 /* Return true if INSN is a (possibly conditional) return insn.  */
968
969 int
970 eh_returnjump_p (rtx_insn *insn)
971 {
972   if (JUMP_P (insn))
973     {
974       subrtx_iterator::array_type array;
975       FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
976         if (GET_CODE (*iter) == EH_RETURN)
977           return true;
978     }
979   return false;
980 }
981
982 /* Return true if INSN is a jump that only transfers control and
983    nothing more.  */
984
985 int
986 onlyjump_p (const rtx_insn *insn)
987 {
988   rtx set;
989
990   if (!JUMP_P (insn))
991     return 0;
992
993   set = single_set (insn);
994   if (set == NULL)
995     return 0;
996   if (GET_CODE (SET_DEST (set)) != PC)
997     return 0;
998   if (side_effects_p (SET_SRC (set)))
999     return 0;
1000
1001   return 1;
1002 }
1003
1004 /* Return true iff INSN is a jump and its JUMP_LABEL is a label, not
1005    NULL or a return.  */
1006 bool
1007 jump_to_label_p (const rtx_insn *insn)
1008 {
1009   return (JUMP_P (insn)
1010           && JUMP_LABEL (insn) != NULL && !ANY_RETURN_P (JUMP_LABEL (insn)));
1011 }
1012
1013 /* Return nonzero if X is an RTX that only sets the condition codes
1014    and has no side effects.  */
1015
1016 int
1017 only_sets_cc0_p (const_rtx x)
1018 {
1019   if (! x)
1020     return 0;
1021
1022   if (INSN_P (x))
1023     x = PATTERN (x);
1024
1025   return sets_cc0_p (x) == 1 && ! side_effects_p (x);
1026 }
1027
1028 /* Return 1 if X is an RTX that does nothing but set the condition codes
1029    and CLOBBER or USE registers.
1030    Return -1 if X does explicitly set the condition codes,
1031    but also does other things.  */
1032
1033 int
1034 sets_cc0_p (const_rtx x)
1035 {
1036   if (! x)
1037     return 0;
1038
1039   if (INSN_P (x))
1040     x = PATTERN (x);
1041
1042   if (GET_CODE (x) == SET && SET_DEST (x) == cc0_rtx)
1043     return 1;
1044   if (GET_CODE (x) == PARALLEL)
1045     {
1046       int i;
1047       int sets_cc0 = 0;
1048       int other_things = 0;
1049       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1050         {
1051           if (GET_CODE (XVECEXP (x, 0, i)) == SET
1052               && SET_DEST (XVECEXP (x, 0, i)) == cc0_rtx)
1053             sets_cc0 = 1;
1054           else if (GET_CODE (XVECEXP (x, 0, i)) == SET)
1055             other_things = 1;
1056         }
1057       return ! sets_cc0 ? 0 : other_things ? -1 : 1;
1058     }
1059   return 0;
1060 }
1061 \f
1062 /* Find all CODE_LABELs referred to in X, and increment their use
1063    counts.  If INSN is a JUMP_INSN and there is at least one
1064    CODE_LABEL referenced in INSN as a jump target, then store the last
1065    one in JUMP_LABEL (INSN).  For a tablejump, this must be the label
1066    for the ADDR_VEC.  Store any other jump targets as REG_LABEL_TARGET
1067    notes.  If INSN is an INSN or a CALL_INSN or non-target operands of
1068    a JUMP_INSN, and there is at least one CODE_LABEL referenced in
1069    INSN, add a REG_LABEL_OPERAND note containing that label to INSN.
1070    For returnjumps, the JUMP_LABEL will also be set as appropriate.
1071
1072    Note that two labels separated by a loop-beginning note
1073    must be kept distinct if we have not yet done loop-optimization,
1074    because the gap between them is where loop-optimize
1075    will want to move invariant code to.  CROSS_JUMP tells us
1076    that loop-optimization is done with.  */
1077
1078 void
1079 mark_jump_label (rtx x, rtx_insn *insn, int in_mem)
1080 {
1081   rtx asmop = extract_asm_operands (x);
1082   if (asmop)
1083     mark_jump_label_asm (asmop, insn);
1084   else
1085     mark_jump_label_1 (x, insn, in_mem != 0,
1086                        (insn != NULL && x == PATTERN (insn) && JUMP_P (insn)));
1087 }
1088
1089 /* Worker function for mark_jump_label.  IN_MEM is TRUE when X occurs
1090    within a (MEM ...).  IS_TARGET is TRUE when X is to be treated as a
1091    jump-target; when the JUMP_LABEL field of INSN should be set or a
1092    REG_LABEL_TARGET note should be added, not a REG_LABEL_OPERAND
1093    note.  */
1094
1095 static void
1096 mark_jump_label_1 (rtx x, rtx_insn *insn, bool in_mem, bool is_target)
1097 {
1098   RTX_CODE code = GET_CODE (x);
1099   int i;
1100   const char *fmt;
1101
1102   switch (code)
1103     {
1104     case PC:
1105     case CC0:
1106     case REG:
1107     case CLOBBER:
1108     case CALL:
1109       return;
1110
1111     case RETURN:
1112     case SIMPLE_RETURN:
1113       if (is_target)
1114         {
1115           gcc_assert (JUMP_LABEL (insn) == NULL || JUMP_LABEL (insn) == x);
1116           JUMP_LABEL (insn) = x;
1117         }
1118       return;
1119
1120     case MEM:
1121       in_mem = true;
1122       break;
1123
1124     case SEQUENCE:
1125       {
1126         rtx_sequence *seq = as_a <rtx_sequence *> (x);
1127         for (i = 0; i < seq->len (); i++)
1128           mark_jump_label (PATTERN (seq->insn (i)),
1129                            seq->insn (i), 0);
1130       }
1131       return;
1132
1133     case SYMBOL_REF:
1134       if (!in_mem)
1135         return;
1136
1137       /* If this is a constant-pool reference, see if it is a label.  */
1138       if (CONSTANT_POOL_ADDRESS_P (x))
1139         mark_jump_label_1 (get_pool_constant (x), insn, in_mem, is_target);
1140       break;
1141
1142       /* Handle operands in the condition of an if-then-else as for a
1143          non-jump insn.  */
1144     case IF_THEN_ELSE:
1145       if (!is_target)
1146         break;
1147       mark_jump_label_1 (XEXP (x, 0), insn, in_mem, false);
1148       mark_jump_label_1 (XEXP (x, 1), insn, in_mem, true);
1149       mark_jump_label_1 (XEXP (x, 2), insn, in_mem, true);
1150       return;
1151
1152     case LABEL_REF:
1153       {
1154         rtx_insn *label = label_ref_label (x);
1155
1156         /* Ignore remaining references to unreachable labels that
1157            have been deleted.  */
1158         if (NOTE_P (label)
1159             && NOTE_KIND (label) == NOTE_INSN_DELETED_LABEL)
1160           break;
1161
1162         gcc_assert (LABEL_P (label));
1163
1164         /* Ignore references to labels of containing functions.  */
1165         if (LABEL_REF_NONLOCAL_P (x))
1166           break;
1167
1168         set_label_ref_label (x, label);
1169         if (! insn || ! insn->deleted ())
1170           ++LABEL_NUSES (label);
1171
1172         if (insn)
1173           {
1174             if (is_target
1175                 /* Do not change a previous setting of JUMP_LABEL.  If the
1176                    JUMP_LABEL slot is occupied by a different label,
1177                    create a note for this label.  */
1178                 && (JUMP_LABEL (insn) == NULL || JUMP_LABEL (insn) == label))
1179               JUMP_LABEL (insn) = label;
1180             else
1181               {
1182                 enum reg_note kind
1183                   = is_target ? REG_LABEL_TARGET : REG_LABEL_OPERAND;
1184
1185                 /* Add a REG_LABEL_OPERAND or REG_LABEL_TARGET note
1186                    for LABEL unless there already is one.  All uses of
1187                    a label, except for the primary target of a jump,
1188                    must have such a note.  */
1189                 if (! find_reg_note (insn, kind, label))
1190                   add_reg_note (insn, kind, label);
1191               }
1192           }
1193         return;
1194       }
1195
1196     /* Do walk the labels in a vector, but not the first operand of an
1197        ADDR_DIFF_VEC.  Don't set the JUMP_LABEL of a vector.  */
1198     case ADDR_VEC:
1199     case ADDR_DIFF_VEC:
1200       if (! insn->deleted ())
1201         {
1202           int eltnum = code == ADDR_DIFF_VEC ? 1 : 0;
1203
1204           for (i = 0; i < XVECLEN (x, eltnum); i++)
1205             mark_jump_label_1 (XVECEXP (x, eltnum, i), NULL, in_mem,
1206                                is_target);
1207         }
1208       return;
1209
1210     default:
1211       break;
1212     }
1213
1214   fmt = GET_RTX_FORMAT (code);
1215
1216   /* The primary target of a tablejump is the label of the ADDR_VEC,
1217      which is canonically mentioned *last* in the insn.  To get it
1218      marked as JUMP_LABEL, we iterate over items in reverse order.  */
1219   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1220     {
1221       if (fmt[i] == 'e')
1222         mark_jump_label_1 (XEXP (x, i), insn, in_mem, is_target);
1223       else if (fmt[i] == 'E')
1224         {
1225           int j;
1226
1227           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1228             mark_jump_label_1 (XVECEXP (x, i, j), insn, in_mem,
1229                                is_target);
1230         }
1231     }
1232 }
1233
1234 /* Worker function for mark_jump_label.  Handle asm insns specially.
1235    In particular, output operands need not be considered so we can
1236    avoid re-scanning the replicated asm_operand.  Also, the asm_labels
1237    need to be considered targets.  */
1238
1239 static void
1240 mark_jump_label_asm (rtx asmop, rtx_insn *insn)
1241 {
1242   int i;
1243
1244   for (i = ASM_OPERANDS_INPUT_LENGTH (asmop) - 1; i >= 0; --i)
1245     mark_jump_label_1 (ASM_OPERANDS_INPUT (asmop, i), insn, false, false);
1246
1247   for (i = ASM_OPERANDS_LABEL_LENGTH (asmop) - 1; i >= 0; --i)
1248     mark_jump_label_1 (ASM_OPERANDS_LABEL (asmop, i), insn, false, true);
1249 }
1250 \f
1251 /* Delete insn INSN from the chain of insns and update label ref counts
1252    and delete insns now unreachable.
1253
1254    Returns the first insn after INSN that was not deleted.
1255
1256    Usage of this instruction is deprecated.  Use delete_insn instead and
1257    subsequent cfg_cleanup pass to delete unreachable code if needed.  */
1258
1259 rtx_insn *
1260 delete_related_insns (rtx uncast_insn)
1261 {
1262   rtx_insn *insn = as_a <rtx_insn *> (uncast_insn);
1263   int was_code_label = (LABEL_P (insn));
1264   rtx note;
1265   rtx_insn *next = NEXT_INSN (insn), *prev = PREV_INSN (insn);
1266
1267   while (next && next->deleted ())
1268     next = NEXT_INSN (next);
1269
1270   /* This insn is already deleted => return first following nondeleted.  */
1271   if (insn->deleted ())
1272     return next;
1273
1274   delete_insn (insn);
1275
1276   /* If instruction is followed by a barrier,
1277      delete the barrier too.  */
1278
1279   if (next != 0 && BARRIER_P (next))
1280     delete_insn (next);
1281
1282   /* If this is a call, then we have to remove the var tracking note
1283      for the call arguments.  */
1284
1285   if (CALL_P (insn)
1286       || (NONJUMP_INSN_P (insn)
1287           && GET_CODE (PATTERN (insn)) == SEQUENCE
1288           && CALL_P (XVECEXP (PATTERN (insn), 0, 0))))
1289     {
1290       rtx_insn *p;
1291
1292       for (p = next && next->deleted () ? NEXT_INSN (next) : next;
1293            p && NOTE_P (p);
1294            p = NEXT_INSN (p))
1295         if (NOTE_KIND (p) == NOTE_INSN_CALL_ARG_LOCATION)
1296           {
1297             remove_insn (p);
1298             break;
1299           }
1300     }
1301
1302   /* If deleting a jump, decrement the count of the label,
1303      and delete the label if it is now unused.  */
1304
1305   if (jump_to_label_p (insn))
1306     {
1307       rtx lab = JUMP_LABEL (insn);
1308       rtx_jump_table_data *lab_next;
1309
1310       if (LABEL_NUSES (lab) == 0)
1311         /* This can delete NEXT or PREV,
1312            either directly if NEXT is JUMP_LABEL (INSN),
1313            or indirectly through more levels of jumps.  */
1314         delete_related_insns (lab);
1315       else if (tablejump_p (insn, NULL, &lab_next))
1316         {
1317           /* If we're deleting the tablejump, delete the dispatch table.
1318              We may not be able to kill the label immediately preceding
1319              just yet, as it might be referenced in code leading up to
1320              the tablejump.  */
1321           delete_related_insns (lab_next);
1322         }
1323     }
1324
1325   /* Likewise if we're deleting a dispatch table.  */
1326
1327   if (rtx_jump_table_data *table = dyn_cast <rtx_jump_table_data *> (insn))
1328     {
1329       rtvec labels = table->get_labels ();
1330       int i;
1331       int len = GET_NUM_ELEM (labels);
1332
1333       for (i = 0; i < len; i++)
1334         if (LABEL_NUSES (XEXP (RTVEC_ELT (labels, i), 0)) == 0)
1335           delete_related_insns (XEXP (RTVEC_ELT (labels, i), 0));
1336       while (next && next->deleted ())
1337         next = NEXT_INSN (next);
1338       return next;
1339     }
1340
1341   /* Likewise for any JUMP_P / INSN / CALL_INSN with a
1342      REG_LABEL_OPERAND or REG_LABEL_TARGET note.  */
1343   if (INSN_P (insn))
1344     for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1345       if ((REG_NOTE_KIND (note) == REG_LABEL_OPERAND
1346            || REG_NOTE_KIND (note) == REG_LABEL_TARGET)
1347           /* This could also be a NOTE_INSN_DELETED_LABEL note.  */
1348           && LABEL_P (XEXP (note, 0)))
1349         if (LABEL_NUSES (XEXP (note, 0)) == 0)
1350           delete_related_insns (XEXP (note, 0));
1351
1352   while (prev && (prev->deleted () || NOTE_P (prev)))
1353     prev = PREV_INSN (prev);
1354
1355   /* If INSN was a label and a dispatch table follows it,
1356      delete the dispatch table.  The tablejump must have gone already.
1357      It isn't useful to fall through into a table.  */
1358
1359   if (was_code_label
1360       && NEXT_INSN (insn) != 0
1361       && JUMP_TABLE_DATA_P (NEXT_INSN (insn)))
1362     next = delete_related_insns (NEXT_INSN (insn));
1363
1364   /* If INSN was a label, delete insns following it if now unreachable.  */
1365
1366   if (was_code_label && prev && BARRIER_P (prev))
1367     {
1368       enum rtx_code code;
1369       while (next)
1370         {
1371           code = GET_CODE (next);
1372           if (code == NOTE)
1373             next = NEXT_INSN (next);
1374           /* Keep going past other deleted labels to delete what follows.  */
1375           else if (code == CODE_LABEL && next->deleted ())
1376             next = NEXT_INSN (next);
1377           /* Keep the (use (insn))s created by dbr_schedule, which needs
1378              them in order to track liveness relative to a previous
1379              barrier.  */
1380           else if (INSN_P (next)
1381                    && GET_CODE (PATTERN (next)) == USE
1382                    && INSN_P (XEXP (PATTERN (next), 0)))
1383             next = NEXT_INSN (next);
1384           else if (code == BARRIER || INSN_P (next))
1385             /* Note: if this deletes a jump, it can cause more
1386                deletion of unreachable code, after a different label.
1387                As long as the value from this recursive call is correct,
1388                this invocation functions correctly.  */
1389             next = delete_related_insns (next);
1390           else
1391             break;
1392         }
1393     }
1394
1395   /* I feel a little doubtful about this loop,
1396      but I see no clean and sure alternative way
1397      to find the first insn after INSN that is not now deleted.
1398      I hope this works.  */
1399   while (next && next->deleted ())
1400     next = NEXT_INSN (next);
1401   return next;
1402 }
1403 \f
1404 /* Delete a range of insns from FROM to TO, inclusive.
1405    This is for the sake of peephole optimization, so assume
1406    that whatever these insns do will still be done by a new
1407    peephole insn that will replace them.  */
1408
1409 void
1410 delete_for_peephole (rtx_insn *from, rtx_insn *to)
1411 {
1412   rtx_insn *insn = from;
1413
1414   while (1)
1415     {
1416       rtx_insn *next = NEXT_INSN (insn);
1417       rtx_insn *prev = PREV_INSN (insn);
1418
1419       if (!NOTE_P (insn))
1420         {
1421           insn->set_deleted();
1422
1423           /* Patch this insn out of the chain.  */
1424           /* We don't do this all at once, because we
1425              must preserve all NOTEs.  */
1426           if (prev)
1427             SET_NEXT_INSN (prev) = next;
1428
1429           if (next)
1430             SET_PREV_INSN (next) = prev;
1431         }
1432
1433       if (insn == to)
1434         break;
1435       insn = next;
1436     }
1437
1438   /* Note that if TO is an unconditional jump
1439      we *do not* delete the BARRIER that follows,
1440      since the peephole that replaces this sequence
1441      is also an unconditional jump in that case.  */
1442 }
1443 \f
1444 /* A helper function for redirect_exp_1; examines its input X and returns
1445    either a LABEL_REF around a label, or a RETURN if X was NULL.  */
1446 static rtx
1447 redirect_target (rtx x)
1448 {
1449   if (x == NULL_RTX)
1450     return ret_rtx;
1451   if (!ANY_RETURN_P (x))
1452     return gen_rtx_LABEL_REF (Pmode, x);
1453   return x;
1454 }
1455
1456 /* Throughout LOC, redirect OLABEL to NLABEL.  Treat null OLABEL or
1457    NLABEL as a return.  Accrue modifications into the change group.  */
1458
1459 static void
1460 redirect_exp_1 (rtx *loc, rtx olabel, rtx nlabel, rtx_insn *insn)
1461 {
1462   rtx x = *loc;
1463   RTX_CODE code = GET_CODE (x);
1464   int i;
1465   const char *fmt;
1466
1467   if ((code == LABEL_REF && label_ref_label (x) == olabel)
1468       || x == olabel)
1469     {
1470       x = redirect_target (nlabel);
1471       if (GET_CODE (x) == LABEL_REF && loc == &PATTERN (insn))
1472         x = gen_rtx_SET (pc_rtx, x);
1473       validate_change (insn, loc, x, 1);
1474       return;
1475     }
1476
1477   if (code == SET && SET_DEST (x) == pc_rtx
1478       && ANY_RETURN_P (nlabel)
1479       && GET_CODE (SET_SRC (x)) == LABEL_REF
1480       && label_ref_label (SET_SRC (x)) == olabel)
1481     {
1482       validate_change (insn, loc, nlabel, 1);
1483       return;
1484     }
1485
1486   if (code == IF_THEN_ELSE)
1487     {
1488       /* Skip the condition of an IF_THEN_ELSE.  We only want to
1489          change jump destinations, not eventual label comparisons.  */
1490       redirect_exp_1 (&XEXP (x, 1), olabel, nlabel, insn);
1491       redirect_exp_1 (&XEXP (x, 2), olabel, nlabel, insn);
1492       return;
1493     }
1494
1495   fmt = GET_RTX_FORMAT (code);
1496   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1497     {
1498       if (fmt[i] == 'e')
1499         redirect_exp_1 (&XEXP (x, i), olabel, nlabel, insn);
1500       else if (fmt[i] == 'E')
1501         {
1502           int j;
1503           for (j = 0; j < XVECLEN (x, i); j++)
1504             redirect_exp_1 (&XVECEXP (x, i, j), olabel, nlabel, insn);
1505         }
1506     }
1507 }
1508
1509 /* Make JUMP go to NLABEL instead of where it jumps now.  Accrue
1510    the modifications into the change group.  Return false if we did
1511    not see how to do that.  */
1512
1513 int
1514 redirect_jump_1 (rtx_insn *jump, rtx nlabel)
1515 {
1516   int ochanges = num_validated_changes ();
1517   rtx *loc, asmop;
1518
1519   gcc_assert (nlabel != NULL_RTX);
1520   asmop = extract_asm_operands (PATTERN (jump));
1521   if (asmop)
1522     {
1523       if (nlabel == NULL)
1524         return 0;
1525       gcc_assert (ASM_OPERANDS_LABEL_LENGTH (asmop) == 1);
1526       loc = &ASM_OPERANDS_LABEL (asmop, 0);
1527     }
1528   else if (GET_CODE (PATTERN (jump)) == PARALLEL)
1529     loc = &XVECEXP (PATTERN (jump), 0, 0);
1530   else
1531     loc = &PATTERN (jump);
1532
1533   redirect_exp_1 (loc, JUMP_LABEL (jump), nlabel, jump);
1534   return num_validated_changes () > ochanges;
1535 }
1536
1537 /* Make JUMP go to NLABEL instead of where it jumps now.  If the old
1538    jump target label is unused as a result, it and the code following
1539    it may be deleted.
1540
1541    Normally, NLABEL will be a label, but it may also be a RETURN rtx;
1542    in that case we are to turn the jump into a (possibly conditional)
1543    return insn.
1544
1545    The return value will be 1 if the change was made, 0 if it wasn't
1546    (this can only occur when trying to produce return insns).  */
1547
1548 int
1549 redirect_jump (rtx_jump_insn *jump, rtx nlabel, int delete_unused)
1550 {
1551   rtx olabel = jump->jump_label ();
1552
1553   if (!nlabel)
1554     {
1555       /* If there is no label, we are asked to redirect to the EXIT block.
1556          When before the epilogue is emitted, return/simple_return cannot be
1557          created so we return 0 immediately.  After the epilogue is emitted,
1558          we always expect a label, either a non-null label, or a
1559          return/simple_return RTX.  */
1560
1561       if (!epilogue_completed)
1562         return 0;
1563       gcc_unreachable ();
1564     }
1565
1566   if (nlabel == olabel)
1567     return 1;
1568
1569   if (! redirect_jump_1 (jump, nlabel) || ! apply_change_group ())
1570     return 0;
1571
1572   redirect_jump_2 (jump, olabel, nlabel, delete_unused, 0);
1573   return 1;
1574 }
1575
1576 /* Fix up JUMP_LABEL and label ref counts after OLABEL has been replaced with
1577    NLABEL in JUMP.
1578    If DELETE_UNUSED is positive, delete related insn to OLABEL if its ref
1579    count has dropped to zero.  */
1580 void
1581 redirect_jump_2 (rtx_jump_insn *jump, rtx olabel, rtx nlabel, int delete_unused,
1582                  int invert)
1583 {
1584   rtx note;
1585
1586   gcc_assert (JUMP_LABEL (jump) == olabel);
1587
1588   /* Negative DELETE_UNUSED used to be used to signalize behavior on
1589      moving FUNCTION_END note.  Just sanity check that no user still worry
1590      about this.  */
1591   gcc_assert (delete_unused >= 0);
1592   JUMP_LABEL (jump) = nlabel;
1593   if (!ANY_RETURN_P (nlabel))
1594     ++LABEL_NUSES (nlabel);
1595
1596   /* Update labels in any REG_EQUAL note.  */
1597   if ((note = find_reg_note (jump, REG_EQUAL, NULL_RTX)) != NULL_RTX)
1598     {
1599       if (ANY_RETURN_P (nlabel)
1600           || (invert && !invert_exp_1 (XEXP (note, 0), jump)))
1601         remove_note (jump, note);
1602       else
1603         {
1604           redirect_exp_1 (&XEXP (note, 0), olabel, nlabel, jump);
1605           confirm_change_group ();
1606         }
1607     }
1608
1609   /* Handle the case where we had a conditional crossing jump to a return
1610      label and are now changing it into a direct conditional return.
1611      The jump is no longer crossing in that case.  */
1612   if (ANY_RETURN_P (nlabel))
1613     CROSSING_JUMP_P (jump) = 0;
1614
1615   if (!ANY_RETURN_P (olabel)
1616       && --LABEL_NUSES (olabel) == 0 && delete_unused > 0
1617       /* Undefined labels will remain outside the insn stream.  */
1618       && INSN_UID (olabel))
1619     delete_related_insns (olabel);
1620   if (invert)
1621     invert_br_probabilities (jump);
1622 }
1623
1624 /* Invert the jump condition X contained in jump insn INSN.  Accrue the
1625    modifications into the change group.  Return nonzero for success.  */
1626 static int
1627 invert_exp_1 (rtx x, rtx_insn *insn)
1628 {
1629   RTX_CODE code = GET_CODE (x);
1630
1631   if (code == IF_THEN_ELSE)
1632     {
1633       rtx comp = XEXP (x, 0);
1634       rtx tem;
1635       enum rtx_code reversed_code;
1636
1637       /* We can do this in two ways:  The preferable way, which can only
1638          be done if this is not an integer comparison, is to reverse
1639          the comparison code.  Otherwise, swap the THEN-part and ELSE-part
1640          of the IF_THEN_ELSE.  If we can't do either, fail.  */
1641
1642       reversed_code = reversed_comparison_code (comp, insn);
1643
1644       if (reversed_code != UNKNOWN)
1645         {
1646           validate_change (insn, &XEXP (x, 0),
1647                            gen_rtx_fmt_ee (reversed_code,
1648                                            GET_MODE (comp), XEXP (comp, 0),
1649                                            XEXP (comp, 1)),
1650                            1);
1651           return 1;
1652         }
1653
1654       tem = XEXP (x, 1);
1655       validate_change (insn, &XEXP (x, 1), XEXP (x, 2), 1);
1656       validate_change (insn, &XEXP (x, 2), tem, 1);
1657       return 1;
1658     }
1659   else
1660     return 0;
1661 }
1662
1663 /* Invert the condition of the jump JUMP, and make it jump to label
1664    NLABEL instead of where it jumps now.  Accrue changes into the
1665    change group.  Return false if we didn't see how to perform the
1666    inversion and redirection.  */
1667
1668 int
1669 invert_jump_1 (rtx_jump_insn *jump, rtx nlabel)
1670 {
1671   rtx x = pc_set (jump);
1672   int ochanges;
1673   int ok;
1674
1675   ochanges = num_validated_changes ();
1676   if (x == NULL)
1677     return 0;
1678   ok = invert_exp_1 (SET_SRC (x), jump);
1679   gcc_assert (ok);
1680
1681   if (num_validated_changes () == ochanges)
1682     return 0;
1683
1684   /* redirect_jump_1 will fail of nlabel == olabel, and the current use is
1685      in Pmode, so checking this is not merely an optimization.  */
1686   return nlabel == JUMP_LABEL (jump) || redirect_jump_1 (jump, nlabel);
1687 }
1688
1689 /* Invert the condition of the jump JUMP, and make it jump to label
1690    NLABEL instead of where it jumps now.  Return true if successful.  */
1691
1692 int
1693 invert_jump (rtx_jump_insn *jump, rtx nlabel, int delete_unused)
1694 {
1695   rtx olabel = JUMP_LABEL (jump);
1696
1697   if (invert_jump_1 (jump, nlabel) && apply_change_group ())
1698     {
1699       redirect_jump_2 (jump, olabel, nlabel, delete_unused, 1);
1700       return 1;
1701     }
1702   cancel_changes (0);
1703   return 0;
1704 }
1705
1706 \f
1707 /* Like rtx_equal_p except that it considers two REGs as equal
1708    if they renumber to the same value and considers two commutative
1709    operations to be the same if the order of the operands has been
1710    reversed.  */
1711
1712 int
1713 rtx_renumbered_equal_p (const_rtx x, const_rtx y)
1714 {
1715   int i;
1716   const enum rtx_code code = GET_CODE (x);
1717   const char *fmt;
1718
1719   if (x == y)
1720     return 1;
1721
1722   if ((code == REG || (code == SUBREG && REG_P (SUBREG_REG (x))))
1723       && (REG_P (y) || (GET_CODE (y) == SUBREG
1724                                   && REG_P (SUBREG_REG (y)))))
1725     {
1726       int reg_x = -1, reg_y = -1;
1727       int byte_x = 0, byte_y = 0;
1728       struct subreg_info info;
1729
1730       if (GET_MODE (x) != GET_MODE (y))
1731         return 0;
1732
1733       /* If we haven't done any renumbering, don't
1734          make any assumptions.  */
1735       if (reg_renumber == 0)
1736         return rtx_equal_p (x, y);
1737
1738       if (code == SUBREG)
1739         {
1740           reg_x = REGNO (SUBREG_REG (x));
1741           byte_x = SUBREG_BYTE (x);
1742
1743           if (reg_renumber[reg_x] >= 0)
1744             {
1745               subreg_get_info (reg_renumber[reg_x],
1746                                GET_MODE (SUBREG_REG (x)), byte_x,
1747                                GET_MODE (x), &info);
1748               if (!info.representable_p)
1749                 return 0;
1750               reg_x = info.offset;
1751               byte_x = 0;
1752             }
1753         }
1754       else
1755         {
1756           reg_x = REGNO (x);
1757           if (reg_renumber[reg_x] >= 0)
1758             reg_x = reg_renumber[reg_x];
1759         }
1760
1761       if (GET_CODE (y) == SUBREG)
1762         {
1763           reg_y = REGNO (SUBREG_REG (y));
1764           byte_y = SUBREG_BYTE (y);
1765
1766           if (reg_renumber[reg_y] >= 0)
1767             {
1768               subreg_get_info (reg_renumber[reg_y],
1769                                GET_MODE (SUBREG_REG (y)), byte_y,
1770                                GET_MODE (y), &info);
1771               if (!info.representable_p)
1772                 return 0;
1773               reg_y = info.offset;
1774               byte_y = 0;
1775             }
1776         }
1777       else
1778         {
1779           reg_y = REGNO (y);
1780           if (reg_renumber[reg_y] >= 0)
1781             reg_y = reg_renumber[reg_y];
1782         }
1783
1784       return reg_x >= 0 && reg_x == reg_y && byte_x == byte_y;
1785     }
1786
1787   /* Now we have disposed of all the cases
1788      in which different rtx codes can match.  */
1789   if (code != GET_CODE (y))
1790     return 0;
1791
1792   switch (code)
1793     {
1794     case PC:
1795     case CC0:
1796     case ADDR_VEC:
1797     case ADDR_DIFF_VEC:
1798     CASE_CONST_UNIQUE:
1799       return 0;
1800
1801     case LABEL_REF:
1802       /* We can't assume nonlocal labels have their following insns yet.  */
1803       if (LABEL_REF_NONLOCAL_P (x) || LABEL_REF_NONLOCAL_P (y))
1804         return label_ref_label (x) == label_ref_label (y);
1805
1806       /* Two label-refs are equivalent if they point at labels
1807          in the same position in the instruction stream.  */
1808       else
1809         {
1810           rtx_insn *xi = next_nonnote_nondebug_insn (label_ref_label (x));
1811           rtx_insn *yi = next_nonnote_nondebug_insn (label_ref_label (y));
1812           while (xi && LABEL_P (xi))
1813             xi = next_nonnote_nondebug_insn (xi);
1814           while (yi && LABEL_P (yi))
1815             yi = next_nonnote_nondebug_insn (yi);
1816           return xi == yi;
1817         }
1818
1819     case SYMBOL_REF:
1820       return XSTR (x, 0) == XSTR (y, 0);
1821
1822     case CODE_LABEL:
1823       /* If we didn't match EQ equality above, they aren't the same.  */
1824       return 0;
1825
1826     default:
1827       break;
1828     }
1829
1830   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
1831
1832   if (GET_MODE (x) != GET_MODE (y))
1833     return 0;
1834
1835   /* MEMs referring to different address space are not equivalent.  */
1836   if (code == MEM && MEM_ADDR_SPACE (x) != MEM_ADDR_SPACE (y))
1837     return 0;
1838
1839   /* For commutative operations, the RTX match if the operand match in any
1840      order.  Also handle the simple binary and unary cases without a loop.  */
1841   if (targetm.commutative_p (x, UNKNOWN))
1842     return ((rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
1843              && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)))
1844             || (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 1))
1845                 && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 0))));
1846   else if (NON_COMMUTATIVE_P (x))
1847     return (rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0))
1848             && rtx_renumbered_equal_p (XEXP (x, 1), XEXP (y, 1)));
1849   else if (UNARY_P (x))
1850     return rtx_renumbered_equal_p (XEXP (x, 0), XEXP (y, 0));
1851
1852   /* Compare the elements.  If any pair of corresponding elements
1853      fail to match, return 0 for the whole things.  */
1854
1855   fmt = GET_RTX_FORMAT (code);
1856   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1857     {
1858       int j;
1859       switch (fmt[i])
1860         {
1861         case 'w':
1862           if (XWINT (x, i) != XWINT (y, i))
1863             return 0;
1864           break;
1865
1866         case 'i':
1867           if (XINT (x, i) != XINT (y, i))
1868             {
1869               if (((code == ASM_OPERANDS && i == 6)
1870                    || (code == ASM_INPUT && i == 1)))
1871                 break;
1872               return 0;
1873             }
1874           break;
1875
1876         case 't':
1877           if (XTREE (x, i) != XTREE (y, i))
1878             return 0;
1879           break;
1880
1881         case 's':
1882           if (strcmp (XSTR (x, i), XSTR (y, i)))
1883             return 0;
1884           break;
1885
1886         case 'e':
1887           if (! rtx_renumbered_equal_p (XEXP (x, i), XEXP (y, i)))
1888             return 0;
1889           break;
1890
1891         case 'u':
1892           if (XEXP (x, i) != XEXP (y, i))
1893             return 0;
1894           /* Fall through.  */
1895         case '0':
1896           break;
1897
1898         case 'E':
1899           if (XVECLEN (x, i) != XVECLEN (y, i))
1900             return 0;
1901           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1902             if (!rtx_renumbered_equal_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
1903               return 0;
1904           break;
1905
1906         default:
1907           gcc_unreachable ();
1908         }
1909     }
1910   return 1;
1911 }
1912 \f
1913 /* If X is a hard register or equivalent to one or a subregister of one,
1914    return the hard register number.  If X is a pseudo register that was not
1915    assigned a hard register, return the pseudo register number.  Otherwise,
1916    return -1.  Any rtx is valid for X.  */
1917
1918 int
1919 true_regnum (const_rtx x)
1920 {
1921   if (REG_P (x))
1922     {
1923       if (REGNO (x) >= FIRST_PSEUDO_REGISTER
1924           && (lra_in_progress || reg_renumber[REGNO (x)] >= 0))
1925         return reg_renumber[REGNO (x)];
1926       return REGNO (x);
1927     }
1928   if (GET_CODE (x) == SUBREG)
1929     {
1930       int base = true_regnum (SUBREG_REG (x));
1931       if (base >= 0
1932           && base < FIRST_PSEUDO_REGISTER)
1933         {
1934           struct subreg_info info;
1935
1936           subreg_get_info (lra_in_progress
1937                            ? (unsigned) base : REGNO (SUBREG_REG (x)),
1938                            GET_MODE (SUBREG_REG (x)),
1939                            SUBREG_BYTE (x), GET_MODE (x), &info);
1940
1941           if (info.representable_p)
1942             return base + info.offset;
1943         }
1944     }
1945   return -1;
1946 }
1947
1948 /* Return regno of the register REG and handle subregs too.  */
1949 unsigned int
1950 reg_or_subregno (const_rtx reg)
1951 {
1952   if (GET_CODE (reg) == SUBREG)
1953     reg = SUBREG_REG (reg);
1954   gcc_assert (REG_P (reg));
1955   return REGNO (reg);
1956 }