OSDN Git Service

* gcc.c-torture/execute/builtin-abs-1.c
[pf3gnuchains/gcc-fork.git] / gcc / rtlanal.c
1 /* Analyze RTL for C-Compiler
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "toplev.h"
28 #include "rtl.h"
29 #include "hard-reg-set.h"
30 #include "insn-config.h"
31 #include "recog.h"
32 #include "target.h"
33 #include "output.h"
34 #include "tm_p.h"
35 #include "flags.h"
36 #include "basic-block.h"
37 #include "real.h"
38 #include "regs.h"
39 #include "function.h"
40
41 /* Forward declarations */
42 static int global_reg_mentioned_p_1 (rtx *, void *);
43 static void set_of_1 (rtx, rtx, void *);
44 static void insn_dependent_p_1 (rtx, rtx, void *);
45 static int rtx_referenced_p_1 (rtx *, void *);
46 static int computed_jump_p_1 (rtx);
47 static void parms_set (rtx, rtx, void *);
48 static bool hoist_test_store (rtx, rtx, regset);
49 static void hoist_update_store (rtx, rtx *, rtx, rtx);
50
51 static unsigned HOST_WIDE_INT cached_nonzero_bits (rtx, enum machine_mode,
52                                                    rtx, enum machine_mode,
53                                                    unsigned HOST_WIDE_INT);
54 static unsigned HOST_WIDE_INT nonzero_bits1 (rtx, enum machine_mode, rtx,
55                                              enum machine_mode,
56                                              unsigned HOST_WIDE_INT);
57 static unsigned int cached_num_sign_bit_copies (rtx, enum machine_mode, rtx,
58                                                 enum machine_mode,
59                                                 unsigned int);
60 static unsigned int num_sign_bit_copies1 (rtx, enum machine_mode, rtx,
61                                           enum machine_mode, unsigned int);
62
63 /* Bit flags that specify the machine subtype we are compiling for.
64    Bits are tested using macros TARGET_... defined in the tm.h file
65    and set by `-m...' switches.  Must be defined in rtlanal.c.  */
66
67 int target_flags;
68 \f
69 /* Return 1 if the value of X is unstable
70    (would be different at a different point in the program).
71    The frame pointer, arg pointer, etc. are considered stable
72    (within one function) and so is anything marked `unchanging'.  */
73
74 int
75 rtx_unstable_p (rtx x)
76 {
77   RTX_CODE code = GET_CODE (x);
78   int i;
79   const char *fmt;
80
81   switch (code)
82     {
83     case MEM:
84       return ! RTX_UNCHANGING_P (x) || rtx_unstable_p (XEXP (x, 0));
85
86     case QUEUED:
87       return 1;
88
89     case ADDRESSOF:
90     case CONST:
91     case CONST_INT:
92     case CONST_DOUBLE:
93     case CONST_VECTOR:
94     case SYMBOL_REF:
95     case LABEL_REF:
96       return 0;
97
98     case REG:
99       /* As in rtx_varies_p, we have to use the actual rtx, not reg number.  */
100       if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
101           /* The arg pointer varies if it is not a fixed register.  */
102           || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
103           || RTX_UNCHANGING_P (x))
104         return 0;
105 #ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
106       /* ??? When call-clobbered, the value is stable modulo the restore
107          that must happen after a call.  This currently screws up local-alloc
108          into believing that the restore is not needed.  */
109       if (x == pic_offset_table_rtx)
110         return 0;
111 #endif
112       return 1;
113
114     case ASM_OPERANDS:
115       if (MEM_VOLATILE_P (x))
116         return 1;
117
118       /* Fall through.  */
119
120     default:
121       break;
122     }
123
124   fmt = GET_RTX_FORMAT (code);
125   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
126     if (fmt[i] == 'e')
127       {
128         if (rtx_unstable_p (XEXP (x, i)))
129           return 1;
130       }
131     else if (fmt[i] == 'E')
132       {
133         int j;
134         for (j = 0; j < XVECLEN (x, i); j++)
135           if (rtx_unstable_p (XVECEXP (x, i, j)))
136             return 1;
137       }
138
139   return 0;
140 }
141
142 /* Return 1 if X has a value that can vary even between two
143    executions of the program.  0 means X can be compared reliably
144    against certain constants or near-constants.
145    FOR_ALIAS is nonzero if we are called from alias analysis; if it is
146    zero, we are slightly more conservative.
147    The frame pointer and the arg pointer are considered constant.  */
148
149 int
150 rtx_varies_p (rtx x, int for_alias)
151 {
152   RTX_CODE code;
153   int i;
154   const char *fmt;
155
156   if (!x)
157     return 0;
158
159   code = GET_CODE (x);
160   switch (code)
161     {
162     case MEM:
163       return ! RTX_UNCHANGING_P (x) || rtx_varies_p (XEXP (x, 0), for_alias);
164
165     case QUEUED:
166       return 1;
167
168     case CONST:
169     case CONST_INT:
170     case CONST_DOUBLE:
171     case CONST_VECTOR:
172     case SYMBOL_REF:
173     case LABEL_REF:
174       return 0;
175
176     case ADDRESSOF:
177       /* This will resolve to some offset from the frame pointer.  */
178       return 0;
179
180     case REG:
181       /* Note that we have to test for the actual rtx used for the frame
182          and arg pointers and not just the register number in case we have
183          eliminated the frame and/or arg pointer and are using it
184          for pseudos.  */
185       if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
186           /* The arg pointer varies if it is not a fixed register.  */
187           || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
188         return 0;
189       if (x == pic_offset_table_rtx
190 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
191           /* ??? When call-clobbered, the value is stable modulo the restore
192              that must happen after a call.  This currently screws up
193              local-alloc into believing that the restore is not needed, so we
194              must return 0 only if we are called from alias analysis.  */
195           && for_alias
196 #endif
197           )
198         return 0;
199       return 1;
200
201     case LO_SUM:
202       /* The operand 0 of a LO_SUM is considered constant
203          (in fact it is related specifically to operand 1)
204          during alias analysis.  */
205       return (! for_alias && rtx_varies_p (XEXP (x, 0), for_alias))
206              || rtx_varies_p (XEXP (x, 1), for_alias);
207
208     case ASM_OPERANDS:
209       if (MEM_VOLATILE_P (x))
210         return 1;
211
212       /* Fall through.  */
213
214     default:
215       break;
216     }
217
218   fmt = GET_RTX_FORMAT (code);
219   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
220     if (fmt[i] == 'e')
221       {
222         if (rtx_varies_p (XEXP (x, i), for_alias))
223           return 1;
224       }
225     else if (fmt[i] == 'E')
226       {
227         int j;
228         for (j = 0; j < XVECLEN (x, i); j++)
229           if (rtx_varies_p (XVECEXP (x, i, j), for_alias))
230             return 1;
231       }
232
233   return 0;
234 }
235
236 /* Return 0 if the use of X as an address in a MEM can cause a trap.  */
237
238 int
239 rtx_addr_can_trap_p (rtx x)
240 {
241   enum rtx_code code = GET_CODE (x);
242
243   switch (code)
244     {
245     case SYMBOL_REF:
246       return SYMBOL_REF_WEAK (x);
247
248     case LABEL_REF:
249       return 0;
250
251     case ADDRESSOF:
252       /* This will resolve to some offset from the frame pointer.  */
253       return 0;
254
255     case REG:
256       /* As in rtx_varies_p, we have to use the actual rtx, not reg number.  */
257       if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
258           || x == stack_pointer_rtx
259           /* The arg pointer varies if it is not a fixed register.  */
260           || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
261         return 0;
262       /* All of the virtual frame registers are stack references.  */
263       if (REGNO (x) >= FIRST_VIRTUAL_REGISTER
264           && REGNO (x) <= LAST_VIRTUAL_REGISTER)
265         return 0;
266       return 1;
267
268     case CONST:
269       return rtx_addr_can_trap_p (XEXP (x, 0));
270
271     case PLUS:
272       /* An address is assumed not to trap if it is an address that can't
273          trap plus a constant integer or it is the pic register plus a
274          constant.  */
275       return ! ((! rtx_addr_can_trap_p (XEXP (x, 0))
276                  && GET_CODE (XEXP (x, 1)) == CONST_INT)
277                 || (XEXP (x, 0) == pic_offset_table_rtx
278                     && CONSTANT_P (XEXP (x, 1))));
279
280     case LO_SUM:
281     case PRE_MODIFY:
282       return rtx_addr_can_trap_p (XEXP (x, 1));
283
284     case PRE_DEC:
285     case PRE_INC:
286     case POST_DEC:
287     case POST_INC:
288     case POST_MODIFY:
289       return rtx_addr_can_trap_p (XEXP (x, 0));
290
291     default:
292       break;
293     }
294
295   /* If it isn't one of the case above, it can cause a trap.  */
296   return 1;
297 }
298
299 /* Return true if X is an address that is known to not be zero.  */
300
301 bool
302 nonzero_address_p (rtx x)
303 {
304   enum rtx_code code = GET_CODE (x);
305
306   switch (code)
307     {
308     case SYMBOL_REF:
309       return !SYMBOL_REF_WEAK (x);
310
311     case LABEL_REF:
312       return true;
313
314     case ADDRESSOF:
315       /* This will resolve to some offset from the frame pointer.  */
316       return true;
317
318     case REG:
319       /* As in rtx_varies_p, we have to use the actual rtx, not reg number.  */
320       if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
321           || x == stack_pointer_rtx
322           || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
323         return true;
324       /* All of the virtual frame registers are stack references.  */
325       if (REGNO (x) >= FIRST_VIRTUAL_REGISTER
326           && REGNO (x) <= LAST_VIRTUAL_REGISTER)
327         return true;
328       return false;
329
330     case CONST:
331       return nonzero_address_p (XEXP (x, 0));
332
333     case PLUS:
334       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
335         {
336           /* Pointers aren't allowed to wrap.  If we've got a register
337              that is known to be a pointer, and a positive offset, then
338              the composite can't be zero.  */
339           if (INTVAL (XEXP (x, 1)) > 0
340               && REG_P (XEXP (x, 0))
341               && REG_POINTER (XEXP (x, 0)))
342             return true;
343
344           return nonzero_address_p (XEXP (x, 0));
345         }
346       /* Handle PIC references.  */
347       else if (XEXP (x, 0) == pic_offset_table_rtx
348                && CONSTANT_P (XEXP (x, 1)))
349         return true;
350       return false;
351
352     case PRE_MODIFY:
353       /* Similar to the above; allow positive offsets.  Further, since
354          auto-inc is only allowed in memories, the register must be a
355          pointer.  */
356       if (GET_CODE (XEXP (x, 1)) == CONST_INT
357           && INTVAL (XEXP (x, 1)) > 0)
358         return true;
359       return nonzero_address_p (XEXP (x, 0));
360
361     case PRE_INC:
362       /* Similarly.  Further, the offset is always positive.  */
363       return true;
364
365     case PRE_DEC:
366     case POST_DEC:
367     case POST_INC:
368     case POST_MODIFY:
369       return nonzero_address_p (XEXP (x, 0));
370
371     case LO_SUM:
372       return nonzero_address_p (XEXP (x, 1));
373
374     default:
375       break;
376     }
377
378   /* If it isn't one of the case above, might be zero.  */
379   return false;
380 }
381
382 /* Return 1 if X refers to a memory location whose address
383    cannot be compared reliably with constant addresses,
384    or if X refers to a BLKmode memory object.
385    FOR_ALIAS is nonzero if we are called from alias analysis; if it is
386    zero, we are slightly more conservative.  */
387
388 int
389 rtx_addr_varies_p (rtx x, int for_alias)
390 {
391   enum rtx_code code;
392   int i;
393   const char *fmt;
394
395   if (x == 0)
396     return 0;
397
398   code = GET_CODE (x);
399   if (code == MEM)
400     return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0), for_alias);
401
402   fmt = GET_RTX_FORMAT (code);
403   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
404     if (fmt[i] == 'e')
405       {
406         if (rtx_addr_varies_p (XEXP (x, i), for_alias))
407           return 1;
408       }
409     else if (fmt[i] == 'E')
410       {
411         int j;
412         for (j = 0; j < XVECLEN (x, i); j++)
413           if (rtx_addr_varies_p (XVECEXP (x, i, j), for_alias))
414             return 1;
415       }
416   return 0;
417 }
418 \f
419 /* Return the value of the integer term in X, if one is apparent;
420    otherwise return 0.
421    Only obvious integer terms are detected.
422    This is used in cse.c with the `related_value' field.  */
423
424 HOST_WIDE_INT
425 get_integer_term (rtx x)
426 {
427   if (GET_CODE (x) == CONST)
428     x = XEXP (x, 0);
429
430   if (GET_CODE (x) == MINUS
431       && GET_CODE (XEXP (x, 1)) == CONST_INT)
432     return - INTVAL (XEXP (x, 1));
433   if (GET_CODE (x) == PLUS
434       && GET_CODE (XEXP (x, 1)) == CONST_INT)
435     return INTVAL (XEXP (x, 1));
436   return 0;
437 }
438
439 /* If X is a constant, return the value sans apparent integer term;
440    otherwise return 0.
441    Only obvious integer terms are detected.  */
442
443 rtx
444 get_related_value (rtx x)
445 {
446   if (GET_CODE (x) != CONST)
447     return 0;
448   x = XEXP (x, 0);
449   if (GET_CODE (x) == PLUS
450       && GET_CODE (XEXP (x, 1)) == CONST_INT)
451     return XEXP (x, 0);
452   else if (GET_CODE (x) == MINUS
453            && GET_CODE (XEXP (x, 1)) == CONST_INT)
454     return XEXP (x, 0);
455   return 0;
456 }
457 \f
458 /* Given a tablejump insn INSN, return the RTL expression for the offset
459    into the jump table.  If the offset cannot be determined, then return
460    NULL_RTX.
461
462    If EARLIEST is nonzero, it is a pointer to a place where the earliest
463    insn used in locating the offset was found.  */
464
465 rtx
466 get_jump_table_offset (rtx insn, rtx *earliest)
467 {
468   rtx label = NULL;
469   rtx table = NULL;
470   rtx set;
471   rtx old_insn;
472   rtx x;
473   rtx old_x;
474   rtx y;
475   rtx old_y;
476   int i;
477
478   if (!tablejump_p (insn, &label, &table) || !(set = single_set (insn)))
479     return NULL_RTX;
480
481   x = SET_SRC (set);
482
483   /* Some targets (eg, ARM) emit a tablejump that also
484      contains the out-of-range target.  */
485   if (GET_CODE (x) == IF_THEN_ELSE
486       && GET_CODE (XEXP (x, 2)) == LABEL_REF)
487     x = XEXP (x, 1);
488
489   /* Search backwards and locate the expression stored in X.  */
490   for (old_x = NULL_RTX; REG_P (x) && x != old_x;
491        old_x = x, x = find_last_value (x, &insn, NULL_RTX, 0))
492     ;
493
494   /* If X is an expression using a relative address then strip
495      off the addition / subtraction of PC, PIC_OFFSET_TABLE_REGNUM,
496      or the jump table label.  */
497   if (GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC
498       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS))
499     {
500       for (i = 0; i < 2; i++)
501         {
502           old_insn = insn;
503           y = XEXP (x, i);
504
505           if (y == pc_rtx || y == pic_offset_table_rtx)
506             break;
507
508           for (old_y = NULL_RTX; REG_P (y) && y != old_y;
509                old_y = y, y = find_last_value (y, &old_insn, NULL_RTX, 0))
510             ;
511
512           if ((GET_CODE (y) == LABEL_REF && XEXP (y, 0) == label))
513             break;
514         }
515
516       if (i >= 2)
517         return NULL_RTX;
518
519       x = XEXP (x, 1 - i);
520
521       for (old_x = NULL_RTX; REG_P (x) && x != old_x;
522            old_x = x, x = find_last_value (x, &insn, NULL_RTX, 0))
523         ;
524     }
525
526   /* Strip off any sign or zero extension.  */
527   if (GET_CODE (x) == SIGN_EXTEND || GET_CODE (x) == ZERO_EXTEND)
528     {
529       x = XEXP (x, 0);
530
531       for (old_x = NULL_RTX; REG_P (x) && x != old_x;
532            old_x = x, x = find_last_value (x, &insn, NULL_RTX, 0))
533         ;
534     }
535
536   /* If X isn't a MEM then this isn't a tablejump we understand.  */
537   if (!MEM_P (x))
538     return NULL_RTX;
539
540   /* Strip off the MEM.  */
541   x = XEXP (x, 0);
542
543   for (old_x = NULL_RTX; REG_P (x) && x != old_x;
544        old_x = x, x = find_last_value (x, &insn, NULL_RTX, 0))
545     ;
546
547   /* If X isn't a PLUS than this isn't a tablejump we understand.  */
548   if (GET_CODE (x) != PLUS)
549     return NULL_RTX;
550
551   /* At this point we should have an expression representing the jump table
552      plus an offset.  Examine each operand in order to determine which one
553      represents the jump table.  Knowing that tells us that the other operand
554      must represent the offset.  */
555   for (i = 0; i < 2; i++)
556     {
557       old_insn = insn;
558       y = XEXP (x, i);
559
560       for (old_y = NULL_RTX; REG_P (y) && y != old_y;
561            old_y = y, y = find_last_value (y, &old_insn, NULL_RTX, 0))
562         ;
563
564       if ((GET_CODE (y) == CONST || GET_CODE (y) == LABEL_REF)
565           && reg_mentioned_p (label, y))
566         break;
567     }
568
569   if (i >= 2)
570     return NULL_RTX;
571
572   x = XEXP (x, 1 - i);
573
574   /* Strip off the addition / subtraction of PIC_OFFSET_TABLE_REGNUM.  */
575   if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS)
576     for (i = 0; i < 2; i++)
577       if (XEXP (x, i) == pic_offset_table_rtx)
578         {
579           x = XEXP (x, 1 - i);
580           break;
581         }
582
583   if (earliest)
584     *earliest = insn;
585
586   /* Return the RTL expression representing the offset.  */
587   return x;
588 }
589 \f
590 /* A subroutine of global_reg_mentioned_p, returns 1 if *LOC mentions
591    a global register.  */
592
593 static int
594 global_reg_mentioned_p_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
595 {
596   int regno;
597   rtx x = *loc;
598
599   if (! x)
600     return 0;
601
602   switch (GET_CODE (x))
603     {
604     case SUBREG:
605       if (REG_P (SUBREG_REG (x)))
606         {
607           if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
608               && global_regs[subreg_regno (x)])
609             return 1;
610           return 0;
611         }
612       break;
613
614     case REG:
615       regno = REGNO (x);
616       if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
617         return 1;
618       return 0;
619
620     case SCRATCH:
621     case PC:
622     case CC0:
623     case CONST_INT:
624     case CONST_DOUBLE:
625     case CONST:
626     case LABEL_REF:
627       return 0;
628
629     case CALL:
630       /* A non-constant call might use a global register.  */
631       return 1;
632
633     default:
634       break;
635     }
636
637   return 0;
638 }
639
640 /* Returns nonzero if X mentions a global register.  */
641
642 int
643 global_reg_mentioned_p (rtx x)
644 {
645   if (INSN_P (x))
646     {
647       if (GET_CODE (x) == CALL_INSN)
648         {
649           if (! CONST_OR_PURE_CALL_P (x))
650             return 1;
651           x = CALL_INSN_FUNCTION_USAGE (x);
652           if (x == 0)
653             return 0;
654         }
655       else
656         x = PATTERN (x);
657     }
658
659   return for_each_rtx (&x, global_reg_mentioned_p_1, NULL);
660 }
661 \f
662 /* Return the number of places FIND appears within X.  If COUNT_DEST is
663    zero, we do not count occurrences inside the destination of a SET.  */
664
665 int
666 count_occurrences (rtx x, rtx find, int count_dest)
667 {
668   int i, j;
669   enum rtx_code code;
670   const char *format_ptr;
671   int count;
672
673   if (x == find)
674     return 1;
675
676   code = GET_CODE (x);
677
678   switch (code)
679     {
680     case REG:
681     case CONST_INT:
682     case CONST_DOUBLE:
683     case CONST_VECTOR:
684     case SYMBOL_REF:
685     case CODE_LABEL:
686     case PC:
687     case CC0:
688       return 0;
689
690     case MEM:
691       if (MEM_P (find) && rtx_equal_p (x, find))
692         return 1;
693       break;
694
695     case SET:
696       if (SET_DEST (x) == find && ! count_dest)
697         return count_occurrences (SET_SRC (x), find, count_dest);
698       break;
699
700     default:
701       break;
702     }
703
704   format_ptr = GET_RTX_FORMAT (code);
705   count = 0;
706
707   for (i = 0; i < GET_RTX_LENGTH (code); i++)
708     {
709       switch (*format_ptr++)
710         {
711         case 'e':
712           count += count_occurrences (XEXP (x, i), find, count_dest);
713           break;
714
715         case 'E':
716           for (j = 0; j < XVECLEN (x, i); j++)
717             count += count_occurrences (XVECEXP (x, i, j), find, count_dest);
718           break;
719         }
720     }
721   return count;
722 }
723 \f
724 /* Nonzero if register REG appears somewhere within IN.
725    Also works if REG is not a register; in this case it checks
726    for a subexpression of IN that is Lisp "equal" to REG.  */
727
728 int
729 reg_mentioned_p (rtx reg, rtx in)
730 {
731   const char *fmt;
732   int i;
733   enum rtx_code code;
734
735   if (in == 0)
736     return 0;
737
738   if (reg == in)
739     return 1;
740
741   if (GET_CODE (in) == LABEL_REF)
742     return reg == XEXP (in, 0);
743
744   code = GET_CODE (in);
745
746   switch (code)
747     {
748       /* Compare registers by number.  */
749     case REG:
750       return REG_P (reg) && REGNO (in) == REGNO (reg);
751
752       /* These codes have no constituent expressions
753          and are unique.  */
754     case SCRATCH:
755     case CC0:
756     case PC:
757       return 0;
758
759     case CONST_INT:
760     case CONST_VECTOR:
761     case CONST_DOUBLE:
762       /* These are kept unique for a given value.  */
763       return 0;
764
765     default:
766       break;
767     }
768
769   if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
770     return 1;
771
772   fmt = GET_RTX_FORMAT (code);
773
774   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
775     {
776       if (fmt[i] == 'E')
777         {
778           int j;
779           for (j = XVECLEN (in, i) - 1; j >= 0; j--)
780             if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
781               return 1;
782         }
783       else if (fmt[i] == 'e'
784                && reg_mentioned_p (reg, XEXP (in, i)))
785         return 1;
786     }
787   return 0;
788 }
789 \f
790 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
791    no CODE_LABEL insn.  */
792
793 int
794 no_labels_between_p (rtx beg, rtx end)
795 {
796   rtx p;
797   if (beg == end)
798     return 0;
799   for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
800     if (GET_CODE (p) == CODE_LABEL)
801       return 0;
802   return 1;
803 }
804
805 /* Return 1 if in between BEG and END, exclusive of BEG and END, there is
806    no JUMP_INSN insn.  */
807
808 int
809 no_jumps_between_p (rtx beg, rtx end)
810 {
811   rtx p;
812   for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
813     if (GET_CODE (p) == JUMP_INSN)
814       return 0;
815   return 1;
816 }
817
818 /* Nonzero if register REG is used in an insn between
819    FROM_INSN and TO_INSN (exclusive of those two).  */
820
821 int
822 reg_used_between_p (rtx reg, rtx from_insn, rtx to_insn)
823 {
824   rtx insn;
825
826   if (from_insn == to_insn)
827     return 0;
828
829   for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
830     if (INSN_P (insn)
831         && (reg_overlap_mentioned_p (reg, PATTERN (insn))
832            || (GET_CODE (insn) == CALL_INSN
833               && (find_reg_fusage (insn, USE, reg)
834                   || find_reg_fusage (insn, CLOBBER, reg)))))
835       return 1;
836   return 0;
837 }
838 \f
839 /* Nonzero if the old value of X, a register, is referenced in BODY.  If X
840    is entirely replaced by a new value and the only use is as a SET_DEST,
841    we do not consider it a reference.  */
842
843 int
844 reg_referenced_p (rtx x, rtx body)
845 {
846   int i;
847
848   switch (GET_CODE (body))
849     {
850     case SET:
851       if (reg_overlap_mentioned_p (x, SET_SRC (body)))
852         return 1;
853
854       /* If the destination is anything other than CC0, PC, a REG or a SUBREG
855          of a REG that occupies all of the REG, the insn references X if
856          it is mentioned in the destination.  */
857       if (GET_CODE (SET_DEST (body)) != CC0
858           && GET_CODE (SET_DEST (body)) != PC
859           && !REG_P (SET_DEST (body))
860           && ! (GET_CODE (SET_DEST (body)) == SUBREG
861                 && REG_P (SUBREG_REG (SET_DEST (body)))
862                 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (body))))
863                       + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
864                     == ((GET_MODE_SIZE (GET_MODE (SET_DEST (body)))
865                          + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
866           && reg_overlap_mentioned_p (x, SET_DEST (body)))
867         return 1;
868       return 0;
869
870     case ASM_OPERANDS:
871       for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
872         if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
873           return 1;
874       return 0;
875
876     case CALL:
877     case USE:
878     case IF_THEN_ELSE:
879       return reg_overlap_mentioned_p (x, body);
880
881     case TRAP_IF:
882       return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
883
884     case PREFETCH:
885       return reg_overlap_mentioned_p (x, XEXP (body, 0));
886
887     case UNSPEC:
888     case UNSPEC_VOLATILE:
889       for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
890         if (reg_overlap_mentioned_p (x, XVECEXP (body, 0, i)))
891           return 1;
892       return 0;
893
894     case PARALLEL:
895       for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
896         if (reg_referenced_p (x, XVECEXP (body, 0, i)))
897           return 1;
898       return 0;
899
900     case CLOBBER:
901       if (MEM_P (XEXP (body, 0)))
902         if (reg_overlap_mentioned_p (x, XEXP (XEXP (body, 0), 0)))
903           return 1;
904       return 0;
905
906     case COND_EXEC:
907       if (reg_overlap_mentioned_p (x, COND_EXEC_TEST (body)))
908         return 1;
909       return reg_referenced_p (x, COND_EXEC_CODE (body));
910
911     default:
912       return 0;
913     }
914 }
915
916 /* Nonzero if register REG is referenced in an insn between
917    FROM_INSN and TO_INSN (exclusive of those two).  Sets of REG do
918    not count.  */
919
920 int
921 reg_referenced_between_p (rtx reg, rtx from_insn, rtx to_insn)
922 {
923   rtx insn;
924
925   if (from_insn == to_insn)
926     return 0;
927
928   for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
929     if (INSN_P (insn)
930         && (reg_referenced_p (reg, PATTERN (insn))
931            || (GET_CODE (insn) == CALL_INSN
932               && find_reg_fusage (insn, USE, reg))))
933       return 1;
934   return 0;
935 }
936 \f
937 /* Nonzero if register REG is set or clobbered in an insn between
938    FROM_INSN and TO_INSN (exclusive of those two).  */
939
940 int
941 reg_set_between_p (rtx reg, rtx from_insn, rtx to_insn)
942 {
943   rtx insn;
944
945   if (from_insn == to_insn)
946     return 0;
947
948   for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
949     if (INSN_P (insn) && reg_set_p (reg, insn))
950       return 1;
951   return 0;
952 }
953
954 /* Internals of reg_set_between_p.  */
955 int
956 reg_set_p (rtx reg, rtx insn)
957 {
958   /* We can be passed an insn or part of one.  If we are passed an insn,
959      check if a side-effect of the insn clobbers REG.  */
960   if (INSN_P (insn)
961       && (FIND_REG_INC_NOTE (insn, reg)
962           || (GET_CODE (insn) == CALL_INSN
963               /* We'd like to test call_used_regs here, but rtlanal.c can't
964                  reference that variable due to its use in genattrtab.  So
965                  we'll just be more conservative.
966
967                  ??? Unless we could ensure that the CALL_INSN_FUNCTION_USAGE
968                  information holds all clobbered registers.  */
969               && ((REG_P (reg)
970                    && REGNO (reg) < FIRST_PSEUDO_REGISTER)
971                   || MEM_P (reg)
972                   || find_reg_fusage (insn, CLOBBER, reg)))))
973     return 1;
974
975   return set_of (reg, insn) != NULL_RTX;
976 }
977
978 /* Similar to reg_set_between_p, but check all registers in X.  Return 0
979    only if none of them are modified between START and END.  Do not
980    consider non-registers one way or the other.  */
981
982 int
983 regs_set_between_p (rtx x, rtx start, rtx end)
984 {
985   enum rtx_code code = GET_CODE (x);
986   const char *fmt;
987   int i, j;
988
989   switch (code)
990     {
991     case CONST_INT:
992     case CONST_DOUBLE:
993     case CONST_VECTOR:
994     case CONST:
995     case SYMBOL_REF:
996     case LABEL_REF:
997     case PC:
998     case CC0:
999       return 0;
1000
1001     case REG:
1002       return reg_set_between_p (x, start, end);
1003
1004     default:
1005       break;
1006     }
1007
1008   fmt = GET_RTX_FORMAT (code);
1009   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1010     {
1011       if (fmt[i] == 'e' && regs_set_between_p (XEXP (x, i), start, end))
1012         return 1;
1013
1014       else if (fmt[i] == 'E')
1015         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1016           if (regs_set_between_p (XVECEXP (x, i, j), start, end))
1017             return 1;
1018     }
1019
1020   return 0;
1021 }
1022
1023 /* Similar to reg_set_between_p, but check all registers in X.  Return 0
1024    only if none of them are modified between START and END.  Return 1 if
1025    X contains a MEM; this routine does usememory aliasing.  */
1026
1027 int
1028 modified_between_p (rtx x, rtx start, rtx end)
1029 {
1030   enum rtx_code code = GET_CODE (x);
1031   const char *fmt;
1032   int i, j;
1033   rtx insn;
1034
1035   if (start == end)
1036     return 0;
1037
1038   switch (code)
1039     {
1040     case CONST_INT:
1041     case CONST_DOUBLE:
1042     case CONST_VECTOR:
1043     case CONST:
1044     case SYMBOL_REF:
1045     case LABEL_REF:
1046       return 0;
1047
1048     case PC:
1049     case CC0:
1050       return 1;
1051
1052     case MEM:
1053       if (RTX_UNCHANGING_P (x))
1054         return 0;
1055       if (modified_between_p (XEXP (x, 0), start, end))
1056         return 1;
1057       for (insn = NEXT_INSN (start); insn != end; insn = NEXT_INSN (insn))
1058         if (memory_modified_in_insn_p (x, insn))
1059           return 1;
1060       return 0;
1061       break;
1062
1063     case REG:
1064       return reg_set_between_p (x, start, end);
1065
1066     default:
1067       break;
1068     }
1069
1070   fmt = GET_RTX_FORMAT (code);
1071   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1072     {
1073       if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
1074         return 1;
1075
1076       else if (fmt[i] == 'E')
1077         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1078           if (modified_between_p (XVECEXP (x, i, j), start, end))
1079             return 1;
1080     }
1081
1082   return 0;
1083 }
1084
1085 /* Similar to reg_set_p, but check all registers in X.  Return 0 only if none
1086    of them are modified in INSN.  Return 1 if X contains a MEM; this routine
1087    does use memory aliasing.  */
1088
1089 int
1090 modified_in_p (rtx x, rtx insn)
1091 {
1092   enum rtx_code code = GET_CODE (x);
1093   const char *fmt;
1094   int i, j;
1095
1096   switch (code)
1097     {
1098     case CONST_INT:
1099     case CONST_DOUBLE:
1100     case CONST_VECTOR:
1101     case CONST:
1102     case SYMBOL_REF:
1103     case LABEL_REF:
1104       return 0;
1105
1106     case PC:
1107     case CC0:
1108       return 1;
1109
1110     case MEM:
1111       if (RTX_UNCHANGING_P (x))
1112         return 0;
1113       if (modified_in_p (XEXP (x, 0), insn))
1114         return 1;
1115       if (memory_modified_in_insn_p (x, insn))
1116         return 1;
1117       return 0;
1118       break;
1119
1120     case REG:
1121       return reg_set_p (x, insn);
1122
1123     default:
1124       break;
1125     }
1126
1127   fmt = GET_RTX_FORMAT (code);
1128   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1129     {
1130       if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
1131         return 1;
1132
1133       else if (fmt[i] == 'E')
1134         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1135           if (modified_in_p (XVECEXP (x, i, j), insn))
1136             return 1;
1137     }
1138
1139   return 0;
1140 }
1141
1142 /* Return true if anything in insn X is (anti,output,true) dependent on
1143    anything in insn Y.  */
1144
1145 int
1146 insn_dependent_p (rtx x, rtx y)
1147 {
1148   rtx tmp;
1149
1150   if (! INSN_P (x) || ! INSN_P (y))
1151     abort ();
1152
1153   tmp = PATTERN (y);
1154   note_stores (PATTERN (x), insn_dependent_p_1, &tmp);
1155   if (tmp == NULL_RTX)
1156     return 1;
1157
1158   tmp = PATTERN (x);
1159   note_stores (PATTERN (y), insn_dependent_p_1, &tmp);
1160   if (tmp == NULL_RTX)
1161     return 1;
1162
1163   return 0;
1164 }
1165
1166 /* A helper routine for insn_dependent_p called through note_stores.  */
1167
1168 static void
1169 insn_dependent_p_1 (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
1170 {
1171   rtx * pinsn = (rtx *) data;
1172
1173   if (*pinsn && reg_mentioned_p (x, *pinsn))
1174     *pinsn = NULL_RTX;
1175 }
1176 \f
1177 /* Helper function for set_of.  */
1178 struct set_of_data
1179   {
1180     rtx found;
1181     rtx pat;
1182   };
1183
1184 static void
1185 set_of_1 (rtx x, rtx pat, void *data1)
1186 {
1187    struct set_of_data *data = (struct set_of_data *) (data1);
1188    if (rtx_equal_p (x, data->pat)
1189        || (!MEM_P (x) && reg_overlap_mentioned_p (data->pat, x)))
1190      data->found = pat;
1191 }
1192
1193 /* Give an INSN, return a SET or CLOBBER expression that does modify PAT
1194    (either directly or via STRICT_LOW_PART and similar modifiers).  */
1195 rtx
1196 set_of (rtx pat, rtx insn)
1197 {
1198   struct set_of_data data;
1199   data.found = NULL_RTX;
1200   data.pat = pat;
1201   note_stores (INSN_P (insn) ? PATTERN (insn) : insn, set_of_1, &data);
1202   return data.found;
1203 }
1204 \f
1205 /* Given an INSN, return a SET expression if this insn has only a single SET.
1206    It may also have CLOBBERs, USEs, or SET whose output
1207    will not be used, which we ignore.  */
1208
1209 rtx
1210 single_set_2 (rtx insn, rtx pat)
1211 {
1212   rtx set = NULL;
1213   int set_verified = 1;
1214   int i;
1215
1216   if (GET_CODE (pat) == PARALLEL)
1217     {
1218       for (i = 0; i < XVECLEN (pat, 0); i++)
1219         {
1220           rtx sub = XVECEXP (pat, 0, i);
1221           switch (GET_CODE (sub))
1222             {
1223             case USE:
1224             case CLOBBER:
1225               break;
1226
1227             case SET:
1228               /* We can consider insns having multiple sets, where all
1229                  but one are dead as single set insns.  In common case
1230                  only single set is present in the pattern so we want
1231                  to avoid checking for REG_UNUSED notes unless necessary.
1232
1233                  When we reach set first time, we just expect this is
1234                  the single set we are looking for and only when more
1235                  sets are found in the insn, we check them.  */
1236               if (!set_verified)
1237                 {
1238                   if (find_reg_note (insn, REG_UNUSED, SET_DEST (set))
1239                       && !side_effects_p (set))
1240                     set = NULL;
1241                   else
1242                     set_verified = 1;
1243                 }
1244               if (!set)
1245                 set = sub, set_verified = 0;
1246               else if (!find_reg_note (insn, REG_UNUSED, SET_DEST (sub))
1247                        || side_effects_p (sub))
1248                 return NULL_RTX;
1249               break;
1250
1251             default:
1252               return NULL_RTX;
1253             }
1254         }
1255     }
1256   return set;
1257 }
1258
1259 /* Given an INSN, return nonzero if it has more than one SET, else return
1260    zero.  */
1261
1262 int
1263 multiple_sets (rtx insn)
1264 {
1265   int found;
1266   int i;
1267
1268   /* INSN must be an insn.  */
1269   if (! INSN_P (insn))
1270     return 0;
1271
1272   /* Only a PARALLEL can have multiple SETs.  */
1273   if (GET_CODE (PATTERN (insn)) == PARALLEL)
1274     {
1275       for (i = 0, found = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1276         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1277           {
1278             /* If we have already found a SET, then return now.  */
1279             if (found)
1280               return 1;
1281             else
1282               found = 1;
1283           }
1284     }
1285
1286   /* Either zero or one SET.  */
1287   return 0;
1288 }
1289 \f
1290 /* Return nonzero if the destination of SET equals the source
1291    and there are no side effects.  */
1292
1293 int
1294 set_noop_p (rtx set)
1295 {
1296   rtx src = SET_SRC (set);
1297   rtx dst = SET_DEST (set);
1298
1299   if (dst == pc_rtx && src == pc_rtx)
1300     return 1;
1301
1302   if (MEM_P (dst) && MEM_P (src))
1303     return rtx_equal_p (dst, src) && !side_effects_p (dst);
1304
1305   if (GET_CODE (dst) == SIGN_EXTRACT
1306       || GET_CODE (dst) == ZERO_EXTRACT)
1307     return rtx_equal_p (XEXP (dst, 0), src)
1308            && ! BYTES_BIG_ENDIAN && XEXP (dst, 2) == const0_rtx
1309            && !side_effects_p (src);
1310
1311   if (GET_CODE (dst) == STRICT_LOW_PART)
1312     dst = XEXP (dst, 0);
1313
1314   if (GET_CODE (src) == SUBREG && GET_CODE (dst) == SUBREG)
1315     {
1316       if (SUBREG_BYTE (src) != SUBREG_BYTE (dst))
1317         return 0;
1318       src = SUBREG_REG (src);
1319       dst = SUBREG_REG (dst);
1320     }
1321
1322   return (REG_P (src) && REG_P (dst)
1323           && REGNO (src) == REGNO (dst));
1324 }
1325 \f
1326 /* Return nonzero if an insn consists only of SETs, each of which only sets a
1327    value to itself.  */
1328
1329 int
1330 noop_move_p (rtx insn)
1331 {
1332   rtx pat = PATTERN (insn);
1333
1334   if (INSN_CODE (insn) == NOOP_MOVE_INSN_CODE)
1335     return 1;
1336
1337   /* Insns carrying these notes are useful later on.  */
1338   if (find_reg_note (insn, REG_EQUAL, NULL_RTX))
1339     return 0;
1340
1341   /* For now treat an insn with a REG_RETVAL note as a
1342      a special insn which should not be considered a no-op.  */
1343   if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
1344     return 0;
1345
1346   if (GET_CODE (pat) == SET && set_noop_p (pat))
1347     return 1;
1348
1349   if (GET_CODE (pat) == PARALLEL)
1350     {
1351       int i;
1352       /* If nothing but SETs of registers to themselves,
1353          this insn can also be deleted.  */
1354       for (i = 0; i < XVECLEN (pat, 0); i++)
1355         {
1356           rtx tem = XVECEXP (pat, 0, i);
1357
1358           if (GET_CODE (tem) == USE
1359               || GET_CODE (tem) == CLOBBER)
1360             continue;
1361
1362           if (GET_CODE (tem) != SET || ! set_noop_p (tem))
1363             return 0;
1364         }
1365
1366       return 1;
1367     }
1368   return 0;
1369 }
1370 \f
1371
1372 /* Return the last thing that X was assigned from before *PINSN.  If VALID_TO
1373    is not NULL_RTX then verify that the object is not modified up to VALID_TO.
1374    If the object was modified, if we hit a partial assignment to X, or hit a
1375    CODE_LABEL first, return X.  If we found an assignment, update *PINSN to
1376    point to it.  ALLOW_HWREG is set to 1 if hardware registers are allowed to
1377    be the src.  */
1378
1379 rtx
1380 find_last_value (rtx x, rtx *pinsn, rtx valid_to, int allow_hwreg)
1381 {
1382   rtx p;
1383
1384   for (p = PREV_INSN (*pinsn); p && GET_CODE (p) != CODE_LABEL;
1385        p = PREV_INSN (p))
1386     if (INSN_P (p))
1387       {
1388         rtx set = single_set (p);
1389         rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
1390
1391         if (set && rtx_equal_p (x, SET_DEST (set)))
1392           {
1393             rtx src = SET_SRC (set);
1394
1395             if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
1396               src = XEXP (note, 0);
1397
1398             if ((valid_to == NULL_RTX
1399                  || ! modified_between_p (src, PREV_INSN (p), valid_to))
1400                 /* Reject hard registers because we don't usually want
1401                    to use them; we'd rather use a pseudo.  */
1402                 && (! (REG_P (src)
1403                       && REGNO (src) < FIRST_PSEUDO_REGISTER) || allow_hwreg))
1404               {
1405                 *pinsn = p;
1406                 return src;
1407               }
1408           }
1409
1410         /* If set in non-simple way, we don't have a value.  */
1411         if (reg_set_p (x, p))
1412           break;
1413       }
1414
1415   return x;
1416 }
1417 \f
1418 /* Return nonzero if register in range [REGNO, ENDREGNO)
1419    appears either explicitly or implicitly in X
1420    other than being stored into.
1421
1422    References contained within the substructure at LOC do not count.
1423    LOC may be zero, meaning don't ignore anything.  */
1424
1425 int
1426 refers_to_regno_p (unsigned int regno, unsigned int endregno, rtx x,
1427                    rtx *loc)
1428 {
1429   int i;
1430   unsigned int x_regno;
1431   RTX_CODE code;
1432   const char *fmt;
1433
1434  repeat:
1435   /* The contents of a REG_NONNEG note is always zero, so we must come here
1436      upon repeat in case the last REG_NOTE is a REG_NONNEG note.  */
1437   if (x == 0)
1438     return 0;
1439
1440   code = GET_CODE (x);
1441
1442   switch (code)
1443     {
1444     case REG:
1445       x_regno = REGNO (x);
1446
1447       /* If we modifying the stack, frame, or argument pointer, it will
1448          clobber a virtual register.  In fact, we could be more precise,
1449          but it isn't worth it.  */
1450       if ((x_regno == STACK_POINTER_REGNUM
1451 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1452            || x_regno == ARG_POINTER_REGNUM
1453 #endif
1454            || x_regno == FRAME_POINTER_REGNUM)
1455           && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
1456         return 1;
1457
1458       return (endregno > x_regno
1459               && regno < x_regno + (x_regno < FIRST_PSEUDO_REGISTER
1460                                     ? hard_regno_nregs[x_regno][GET_MODE (x)]
1461                               : 1));
1462
1463     case SUBREG:
1464       /* If this is a SUBREG of a hard reg, we can see exactly which
1465          registers are being modified.  Otherwise, handle normally.  */
1466       if (REG_P (SUBREG_REG (x))
1467           && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
1468         {
1469           unsigned int inner_regno = subreg_regno (x);
1470           unsigned int inner_endregno
1471             = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
1472                              ? hard_regno_nregs[inner_regno][GET_MODE (x)] : 1);
1473
1474           return endregno > inner_regno && regno < inner_endregno;
1475         }
1476       break;
1477
1478     case CLOBBER:
1479     case SET:
1480       if (&SET_DEST (x) != loc
1481           /* Note setting a SUBREG counts as referring to the REG it is in for
1482              a pseudo but not for hard registers since we can
1483              treat each word individually.  */
1484           && ((GET_CODE (SET_DEST (x)) == SUBREG
1485                && loc != &SUBREG_REG (SET_DEST (x))
1486                && REG_P (SUBREG_REG (SET_DEST (x)))
1487                && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
1488                && refers_to_regno_p (regno, endregno,
1489                                      SUBREG_REG (SET_DEST (x)), loc))
1490               || (!REG_P (SET_DEST (x))
1491                   && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
1492         return 1;
1493
1494       if (code == CLOBBER || loc == &SET_SRC (x))
1495         return 0;
1496       x = SET_SRC (x);
1497       goto repeat;
1498
1499     default:
1500       break;
1501     }
1502
1503   /* X does not match, so try its subexpressions.  */
1504
1505   fmt = GET_RTX_FORMAT (code);
1506   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1507     {
1508       if (fmt[i] == 'e' && loc != &XEXP (x, i))
1509         {
1510           if (i == 0)
1511             {
1512               x = XEXP (x, 0);
1513               goto repeat;
1514             }
1515           else
1516             if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
1517               return 1;
1518         }
1519       else if (fmt[i] == 'E')
1520         {
1521           int j;
1522           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1523             if (loc != &XVECEXP (x, i, j)
1524                 && refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
1525               return 1;
1526         }
1527     }
1528   return 0;
1529 }
1530
1531 /* Nonzero if modifying X will affect IN.  If X is a register or a SUBREG,
1532    we check if any register number in X conflicts with the relevant register
1533    numbers.  If X is a constant, return 0.  If X is a MEM, return 1 iff IN
1534    contains a MEM (we don't bother checking for memory addresses that can't
1535    conflict because we expect this to be a rare case.  */
1536
1537 int
1538 reg_overlap_mentioned_p (rtx x, rtx in)
1539 {
1540   unsigned int regno, endregno;
1541
1542   /* If either argument is a constant, then modifying X can not
1543      affect IN.  Here we look at IN, we can profitably combine
1544      CONSTANT_P (x) with the switch statement below.  */
1545   if (CONSTANT_P (in))
1546     return 0;
1547
1548  recurse:
1549   switch (GET_CODE (x))
1550     {
1551     case STRICT_LOW_PART:
1552     case ZERO_EXTRACT:
1553     case SIGN_EXTRACT:
1554       /* Overly conservative.  */
1555       x = XEXP (x, 0);
1556       goto recurse;
1557
1558     case SUBREG:
1559       regno = REGNO (SUBREG_REG (x));
1560       if (regno < FIRST_PSEUDO_REGISTER)
1561         regno = subreg_regno (x);
1562       goto do_reg;
1563
1564     case REG:
1565       regno = REGNO (x);
1566     do_reg:
1567       endregno = regno + (regno < FIRST_PSEUDO_REGISTER
1568                           ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
1569       return refers_to_regno_p (regno, endregno, in, (rtx*) 0);
1570
1571     case MEM:
1572       {
1573         const char *fmt;
1574         int i;
1575
1576         if (MEM_P (in))
1577           return 1;
1578
1579         fmt = GET_RTX_FORMAT (GET_CODE (in));
1580         for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
1581           if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
1582             return 1;
1583
1584         return 0;
1585       }
1586
1587     case SCRATCH:
1588     case PC:
1589     case CC0:
1590       return reg_mentioned_p (x, in);
1591
1592     case PARALLEL:
1593       {
1594         int i;
1595
1596         /* If any register in here refers to it we return true.  */
1597         for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1598           if (XEXP (XVECEXP (x, 0, i), 0) != 0
1599               && reg_overlap_mentioned_p (XEXP (XVECEXP (x, 0, i), 0), in))
1600             return 1;
1601         return 0;
1602       }
1603
1604     default:
1605 #ifdef ENABLE_CHECKING
1606       if (!CONSTANT_P (x))
1607         abort ();
1608 #endif
1609
1610       return 0;
1611     }
1612 }
1613 \f
1614 /* Call FUN on each register or MEM that is stored into or clobbered by X.
1615    (X would be the pattern of an insn).
1616    FUN receives two arguments:
1617      the REG, MEM, CC0 or PC being stored in or clobbered,
1618      the SET or CLOBBER rtx that does the store.
1619
1620   If the item being stored in or clobbered is a SUBREG of a hard register,
1621   the SUBREG will be passed.  */
1622
1623 void
1624 note_stores (rtx x, void (*fun) (rtx, rtx, void *), void *data)
1625 {
1626   int i;
1627
1628   if (GET_CODE (x) == COND_EXEC)
1629     x = COND_EXEC_CODE (x);
1630
1631   if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
1632     {
1633       rtx dest = SET_DEST (x);
1634
1635       while ((GET_CODE (dest) == SUBREG
1636               && (!REG_P (SUBREG_REG (dest))
1637                   || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1638              || GET_CODE (dest) == ZERO_EXTRACT
1639              || GET_CODE (dest) == SIGN_EXTRACT
1640              || GET_CODE (dest) == STRICT_LOW_PART)
1641         dest = XEXP (dest, 0);
1642
1643       /* If we have a PARALLEL, SET_DEST is a list of EXPR_LIST expressions,
1644          each of whose first operand is a register.  */
1645       if (GET_CODE (dest) == PARALLEL)
1646         {
1647           for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1648             if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
1649               (*fun) (XEXP (XVECEXP (dest, 0, i), 0), x, data);
1650         }
1651       else
1652         (*fun) (dest, x, data);
1653     }
1654
1655   else if (GET_CODE (x) == PARALLEL)
1656     for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1657       note_stores (XVECEXP (x, 0, i), fun, data);
1658 }
1659 \f
1660 /* Like notes_stores, but call FUN for each expression that is being
1661    referenced in PBODY, a pointer to the PATTERN of an insn.  We only call
1662    FUN for each expression, not any interior subexpressions.  FUN receives a
1663    pointer to the expression and the DATA passed to this function.
1664
1665    Note that this is not quite the same test as that done in reg_referenced_p
1666    since that considers something as being referenced if it is being
1667    partially set, while we do not.  */
1668
1669 void
1670 note_uses (rtx *pbody, void (*fun) (rtx *, void *), void *data)
1671 {
1672   rtx body = *pbody;
1673   int i;
1674
1675   switch (GET_CODE (body))
1676     {
1677     case COND_EXEC:
1678       (*fun) (&COND_EXEC_TEST (body), data);
1679       note_uses (&COND_EXEC_CODE (body), fun, data);
1680       return;
1681
1682     case PARALLEL:
1683       for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1684         note_uses (&XVECEXP (body, 0, i), fun, data);
1685       return;
1686
1687     case USE:
1688       (*fun) (&XEXP (body, 0), data);
1689       return;
1690
1691     case ASM_OPERANDS:
1692       for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
1693         (*fun) (&ASM_OPERANDS_INPUT (body, i), data);
1694       return;
1695
1696     case TRAP_IF:
1697       (*fun) (&TRAP_CONDITION (body), data);
1698       return;
1699
1700     case PREFETCH:
1701       (*fun) (&XEXP (body, 0), data);
1702       return;
1703
1704     case UNSPEC:
1705     case UNSPEC_VOLATILE:
1706       for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1707         (*fun) (&XVECEXP (body, 0, i), data);
1708       return;
1709
1710     case CLOBBER:
1711       if (MEM_P (XEXP (body, 0)))
1712         (*fun) (&XEXP (XEXP (body, 0), 0), data);
1713       return;
1714
1715     case SET:
1716       {
1717         rtx dest = SET_DEST (body);
1718
1719         /* For sets we replace everything in source plus registers in memory
1720            expression in store and operands of a ZERO_EXTRACT.  */
1721         (*fun) (&SET_SRC (body), data);
1722
1723         if (GET_CODE (dest) == ZERO_EXTRACT)
1724           {
1725             (*fun) (&XEXP (dest, 1), data);
1726             (*fun) (&XEXP (dest, 2), data);
1727           }
1728
1729         while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART)
1730           dest = XEXP (dest, 0);
1731
1732         if (MEM_P (dest))
1733           (*fun) (&XEXP (dest, 0), data);
1734       }
1735       return;
1736
1737     default:
1738       /* All the other possibilities never store.  */
1739       (*fun) (pbody, data);
1740       return;
1741     }
1742 }
1743 \f
1744 /* Return nonzero if X's old contents don't survive after INSN.
1745    This will be true if X is (cc0) or if X is a register and
1746    X dies in INSN or because INSN entirely sets X.
1747
1748    "Entirely set" means set directly and not through a SUBREG,
1749    ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
1750    Likewise, REG_INC does not count.
1751
1752    REG may be a hard or pseudo reg.  Renumbering is not taken into account,
1753    but for this use that makes no difference, since regs don't overlap
1754    during their lifetimes.  Therefore, this function may be used
1755    at any time after deaths have been computed (in flow.c).
1756
1757    If REG is a hard reg that occupies multiple machine registers, this
1758    function will only return 1 if each of those registers will be replaced
1759    by INSN.  */
1760
1761 int
1762 dead_or_set_p (rtx insn, rtx x)
1763 {
1764   unsigned int regno, last_regno;
1765   unsigned int i;
1766
1767   /* Can't use cc0_rtx below since this file is used by genattrtab.c.  */
1768   if (GET_CODE (x) == CC0)
1769     return 1;
1770
1771   if (!REG_P (x))
1772     abort ();
1773
1774   regno = REGNO (x);
1775   last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1776                 : regno + hard_regno_nregs[regno][GET_MODE (x)] - 1);
1777
1778   for (i = regno; i <= last_regno; i++)
1779     if (! dead_or_set_regno_p (insn, i))
1780       return 0;
1781
1782   return 1;
1783 }
1784
1785 /* Utility function for dead_or_set_p to check an individual register.  Also
1786    called from flow.c.  */
1787
1788 int
1789 dead_or_set_regno_p (rtx insn, unsigned int test_regno)
1790 {
1791   unsigned int regno, endregno;
1792   rtx pattern;
1793
1794   /* See if there is a death note for something that includes TEST_REGNO.  */
1795   if (find_regno_note (insn, REG_DEAD, test_regno))
1796     return 1;
1797
1798   if (GET_CODE (insn) == CALL_INSN
1799       && find_regno_fusage (insn, CLOBBER, test_regno))
1800     return 1;
1801
1802   pattern = PATTERN (insn);
1803
1804   if (GET_CODE (pattern) == COND_EXEC)
1805     pattern = COND_EXEC_CODE (pattern);
1806
1807   if (GET_CODE (pattern) == SET)
1808     {
1809       rtx dest = SET_DEST (pattern);
1810
1811       /* A value is totally replaced if it is the destination or the
1812          destination is a SUBREG of REGNO that does not change the number of
1813          words in it.  */
1814       if (GET_CODE (dest) == SUBREG
1815           && (((GET_MODE_SIZE (GET_MODE (dest))
1816                 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1817               == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1818                    + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1819         dest = SUBREG_REG (dest);
1820
1821       if (!REG_P (dest))
1822         return 0;
1823
1824       regno = REGNO (dest);
1825       endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1826                   : regno + hard_regno_nregs[regno][GET_MODE (dest)]);
1827
1828       return (test_regno >= regno && test_regno < endregno);
1829     }
1830   else if (GET_CODE (pattern) == PARALLEL)
1831     {
1832       int i;
1833
1834       for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
1835         {
1836           rtx body = XVECEXP (pattern, 0, i);
1837
1838           if (GET_CODE (body) == COND_EXEC)
1839             body = COND_EXEC_CODE (body);
1840
1841           if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1842             {
1843               rtx dest = SET_DEST (body);
1844
1845               if (GET_CODE (dest) == SUBREG
1846                   && (((GET_MODE_SIZE (GET_MODE (dest))
1847                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1848                       == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1849                            + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1850                 dest = SUBREG_REG (dest);
1851
1852               if (!REG_P (dest))
1853                 continue;
1854
1855               regno = REGNO (dest);
1856               endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1857                           : regno + hard_regno_nregs[regno][GET_MODE (dest)]);
1858
1859               if (test_regno >= regno && test_regno < endregno)
1860                 return 1;
1861             }
1862         }
1863     }
1864
1865   return 0;
1866 }
1867
1868 /* Return the reg-note of kind KIND in insn INSN, if there is one.
1869    If DATUM is nonzero, look for one whose datum is DATUM.  */
1870
1871 rtx
1872 find_reg_note (rtx insn, enum reg_note kind, rtx datum)
1873 {
1874   rtx link;
1875
1876   /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN.  */
1877   if (! INSN_P (insn))
1878     return 0;
1879   if (datum == 0)
1880     {
1881       for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1882         if (REG_NOTE_KIND (link) == kind)
1883           return link;
1884       return 0;
1885     }
1886
1887   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1888     if (REG_NOTE_KIND (link) == kind && datum == XEXP (link, 0))
1889       return link;
1890   return 0;
1891 }
1892
1893 /* Return the reg-note of kind KIND in insn INSN which applies to register
1894    number REGNO, if any.  Return 0 if there is no such reg-note.  Note that
1895    the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1896    it might be the case that the note overlaps REGNO.  */
1897
1898 rtx
1899 find_regno_note (rtx insn, enum reg_note kind, unsigned int regno)
1900 {
1901   rtx link;
1902
1903   /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN.  */
1904   if (! INSN_P (insn))
1905     return 0;
1906
1907   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1908     if (REG_NOTE_KIND (link) == kind
1909         /* Verify that it is a register, so that scratch and MEM won't cause a
1910            problem here.  */
1911         && REG_P (XEXP (link, 0))
1912         && REGNO (XEXP (link, 0)) <= regno
1913         && ((REGNO (XEXP (link, 0))
1914              + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1915                 : hard_regno_nregs[REGNO (XEXP (link, 0))]
1916                                   [GET_MODE (XEXP (link, 0))]))
1917             > regno))
1918       return link;
1919   return 0;
1920 }
1921
1922 /* Return a REG_EQUIV or REG_EQUAL note if insn has only a single set and
1923    has such a note.  */
1924
1925 rtx
1926 find_reg_equal_equiv_note (rtx insn)
1927 {
1928   rtx link;
1929
1930   if (!INSN_P (insn))
1931     return 0;
1932   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1933     if (REG_NOTE_KIND (link) == REG_EQUAL
1934         || REG_NOTE_KIND (link) == REG_EQUIV)
1935       {
1936         if (single_set (insn) == 0)
1937           return 0;
1938         return link;
1939       }
1940   return NULL;
1941 }
1942
1943 /* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1944    in the CALL_INSN_FUNCTION_USAGE information of INSN.  */
1945
1946 int
1947 find_reg_fusage (rtx insn, enum rtx_code code, rtx datum)
1948 {
1949   /* If it's not a CALL_INSN, it can't possibly have a
1950      CALL_INSN_FUNCTION_USAGE field, so don't bother checking.  */
1951   if (GET_CODE (insn) != CALL_INSN)
1952     return 0;
1953
1954   if (! datum)
1955     abort ();
1956
1957   if (!REG_P (datum))
1958     {
1959       rtx link;
1960
1961       for (link = CALL_INSN_FUNCTION_USAGE (insn);
1962            link;
1963            link = XEXP (link, 1))
1964         if (GET_CODE (XEXP (link, 0)) == code
1965             && rtx_equal_p (datum, XEXP (XEXP (link, 0), 0)))
1966           return 1;
1967     }
1968   else
1969     {
1970       unsigned int regno = REGNO (datum);
1971
1972       /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1973          to pseudo registers, so don't bother checking.  */
1974
1975       if (regno < FIRST_PSEUDO_REGISTER)
1976         {
1977           unsigned int end_regno
1978             = regno + hard_regno_nregs[regno][GET_MODE (datum)];
1979           unsigned int i;
1980
1981           for (i = regno; i < end_regno; i++)
1982             if (find_regno_fusage (insn, code, i))
1983               return 1;
1984         }
1985     }
1986
1987   return 0;
1988 }
1989
1990 /* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1991    in the CALL_INSN_FUNCTION_USAGE information of INSN.  */
1992
1993 int
1994 find_regno_fusage (rtx insn, enum rtx_code code, unsigned int regno)
1995 {
1996   rtx link;
1997
1998   /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1999      to pseudo registers, so don't bother checking.  */
2000
2001   if (regno >= FIRST_PSEUDO_REGISTER
2002       || GET_CODE (insn) != CALL_INSN )
2003     return 0;
2004
2005   for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
2006     {
2007       unsigned int regnote;
2008       rtx op, reg;
2009
2010       if (GET_CODE (op = XEXP (link, 0)) == code
2011           && REG_P (reg = XEXP (op, 0))
2012           && (regnote = REGNO (reg)) <= regno
2013           && regnote + hard_regno_nregs[regnote][GET_MODE (reg)] > regno)
2014         return 1;
2015     }
2016
2017   return 0;
2018 }
2019
2020 /* Return true if INSN is a call to a pure function.  */
2021
2022 int
2023 pure_call_p (rtx insn)
2024 {
2025   rtx link;
2026
2027   if (GET_CODE (insn) != CALL_INSN || ! CONST_OR_PURE_CALL_P (insn))
2028     return 0;
2029
2030   /* Look for the note that differentiates const and pure functions.  */
2031   for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
2032     {
2033       rtx u, m;
2034
2035       if (GET_CODE (u = XEXP (link, 0)) == USE
2036           && MEM_P (m = XEXP (u, 0)) && GET_MODE (m) == BLKmode
2037           && GET_CODE (XEXP (m, 0)) == SCRATCH)
2038         return 1;
2039     }
2040
2041   return 0;
2042 }
2043 \f
2044 /* Remove register note NOTE from the REG_NOTES of INSN.  */
2045
2046 void
2047 remove_note (rtx insn, rtx note)
2048 {
2049   rtx link;
2050
2051   if (note == NULL_RTX)
2052     return;
2053
2054   if (REG_NOTES (insn) == note)
2055     {
2056       REG_NOTES (insn) = XEXP (note, 1);
2057       return;
2058     }
2059
2060   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2061     if (XEXP (link, 1) == note)
2062       {
2063         XEXP (link, 1) = XEXP (note, 1);
2064         return;
2065       }
2066
2067   abort ();
2068 }
2069
2070 /* Search LISTP (an EXPR_LIST) for an entry whose first operand is NODE and
2071    return 1 if it is found.  A simple equality test is used to determine if
2072    NODE matches.  */
2073
2074 int
2075 in_expr_list_p (rtx listp, rtx node)
2076 {
2077   rtx x;
2078
2079   for (x = listp; x; x = XEXP (x, 1))
2080     if (node == XEXP (x, 0))
2081       return 1;
2082
2083   return 0;
2084 }
2085
2086 /* Search LISTP (an EXPR_LIST) for an entry whose first operand is NODE and
2087    remove that entry from the list if it is found.
2088
2089    A simple equality test is used to determine if NODE matches.  */
2090
2091 void
2092 remove_node_from_expr_list (rtx node, rtx *listp)
2093 {
2094   rtx temp = *listp;
2095   rtx prev = NULL_RTX;
2096
2097   while (temp)
2098     {
2099       if (node == XEXP (temp, 0))
2100         {
2101           /* Splice the node out of the list.  */
2102           if (prev)
2103             XEXP (prev, 1) = XEXP (temp, 1);
2104           else
2105             *listp = XEXP (temp, 1);
2106
2107           return;
2108         }
2109
2110       prev = temp;
2111       temp = XEXP (temp, 1);
2112     }
2113 }
2114 \f
2115 /* Nonzero if X contains any volatile instructions.  These are instructions
2116    which may cause unpredictable machine state instructions, and thus no
2117    instructions should be moved or combined across them.  This includes
2118    only volatile asms and UNSPEC_VOLATILE instructions.  */
2119
2120 int
2121 volatile_insn_p (rtx x)
2122 {
2123   RTX_CODE code;
2124
2125   code = GET_CODE (x);
2126   switch (code)
2127     {
2128     case LABEL_REF:
2129     case SYMBOL_REF:
2130     case CONST_INT:
2131     case CONST:
2132     case CONST_DOUBLE:
2133     case CONST_VECTOR:
2134     case CC0:
2135     case PC:
2136     case REG:
2137     case SCRATCH:
2138     case CLOBBER:
2139     case ADDR_VEC:
2140     case ADDR_DIFF_VEC:
2141     case CALL:
2142     case MEM:
2143       return 0;
2144
2145     case UNSPEC_VOLATILE:
2146  /* case TRAP_IF: This isn't clear yet.  */
2147       return 1;
2148
2149     case ASM_INPUT:
2150     case ASM_OPERANDS:
2151       if (MEM_VOLATILE_P (x))
2152         return 1;
2153
2154     default:
2155       break;
2156     }
2157
2158   /* Recursively scan the operands of this expression.  */
2159
2160   {
2161     const char *fmt = GET_RTX_FORMAT (code);
2162     int i;
2163
2164     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2165       {
2166         if (fmt[i] == 'e')
2167           {
2168             if (volatile_insn_p (XEXP (x, i)))
2169               return 1;
2170           }
2171         else if (fmt[i] == 'E')
2172           {
2173             int j;
2174             for (j = 0; j < XVECLEN (x, i); j++)
2175               if (volatile_insn_p (XVECEXP (x, i, j)))
2176                 return 1;
2177           }
2178       }
2179   }
2180   return 0;
2181 }
2182
2183 /* Nonzero if X contains any volatile memory references
2184    UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions.  */
2185
2186 int
2187 volatile_refs_p (rtx x)
2188 {
2189   RTX_CODE code;
2190
2191   code = GET_CODE (x);
2192   switch (code)
2193     {
2194     case LABEL_REF:
2195     case SYMBOL_REF:
2196     case CONST_INT:
2197     case CONST:
2198     case CONST_DOUBLE:
2199     case CONST_VECTOR:
2200     case CC0:
2201     case PC:
2202     case REG:
2203     case SCRATCH:
2204     case CLOBBER:
2205     case ADDR_VEC:
2206     case ADDR_DIFF_VEC:
2207       return 0;
2208
2209     case UNSPEC_VOLATILE:
2210       return 1;
2211
2212     case MEM:
2213     case ASM_INPUT:
2214     case ASM_OPERANDS:
2215       if (MEM_VOLATILE_P (x))
2216         return 1;
2217
2218     default:
2219       break;
2220     }
2221
2222   /* Recursively scan the operands of this expression.  */
2223
2224   {
2225     const char *fmt = GET_RTX_FORMAT (code);
2226     int i;
2227
2228     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2229       {
2230         if (fmt[i] == 'e')
2231           {
2232             if (volatile_refs_p (XEXP (x, i)))
2233               return 1;
2234           }
2235         else if (fmt[i] == 'E')
2236           {
2237             int j;
2238             for (j = 0; j < XVECLEN (x, i); j++)
2239               if (volatile_refs_p (XVECEXP (x, i, j)))
2240                 return 1;
2241           }
2242       }
2243   }
2244   return 0;
2245 }
2246
2247 /* Similar to above, except that it also rejects register pre- and post-
2248    incrementing.  */
2249
2250 int
2251 side_effects_p (rtx x)
2252 {
2253   RTX_CODE code;
2254
2255   code = GET_CODE (x);
2256   switch (code)
2257     {
2258     case LABEL_REF:
2259     case SYMBOL_REF:
2260     case CONST_INT:
2261     case CONST:
2262     case CONST_DOUBLE:
2263     case CONST_VECTOR:
2264     case CC0:
2265     case PC:
2266     case REG:
2267     case SCRATCH:
2268     case ADDR_VEC:
2269     case ADDR_DIFF_VEC:
2270       return 0;
2271
2272     case CLOBBER:
2273       /* Reject CLOBBER with a non-VOID mode.  These are made by combine.c
2274          when some combination can't be done.  If we see one, don't think
2275          that we can simplify the expression.  */
2276       return (GET_MODE (x) != VOIDmode);
2277
2278     case PRE_INC:
2279     case PRE_DEC:
2280     case POST_INC:
2281     case POST_DEC:
2282     case PRE_MODIFY:
2283     case POST_MODIFY:
2284     case CALL:
2285     case UNSPEC_VOLATILE:
2286  /* case TRAP_IF: This isn't clear yet.  */
2287       return 1;
2288
2289     case MEM:
2290     case ASM_INPUT:
2291     case ASM_OPERANDS:
2292       if (MEM_VOLATILE_P (x))
2293         return 1;
2294
2295     default:
2296       break;
2297     }
2298
2299   /* Recursively scan the operands of this expression.  */
2300
2301   {
2302     const char *fmt = GET_RTX_FORMAT (code);
2303     int i;
2304
2305     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2306       {
2307         if (fmt[i] == 'e')
2308           {
2309             if (side_effects_p (XEXP (x, i)))
2310               return 1;
2311           }
2312         else if (fmt[i] == 'E')
2313           {
2314             int j;
2315             for (j = 0; j < XVECLEN (x, i); j++)
2316               if (side_effects_p (XVECEXP (x, i, j)))
2317                 return 1;
2318           }
2319       }
2320   }
2321   return 0;
2322 }
2323 \f
2324 /* Return nonzero if evaluating rtx X might cause a trap.  */
2325
2326 int
2327 may_trap_p (rtx x)
2328 {
2329   int i;
2330   enum rtx_code code;
2331   const char *fmt;
2332
2333   if (x == 0)
2334     return 0;
2335   code = GET_CODE (x);
2336   switch (code)
2337     {
2338       /* Handle these cases quickly.  */
2339     case CONST_INT:
2340     case CONST_DOUBLE:
2341     case CONST_VECTOR:
2342     case SYMBOL_REF:
2343     case LABEL_REF:
2344     case CONST:
2345     case PC:
2346     case CC0:
2347     case REG:
2348     case SCRATCH:
2349       return 0;
2350
2351     case ASM_INPUT:
2352     case UNSPEC_VOLATILE:
2353     case TRAP_IF:
2354       return 1;
2355
2356     case ASM_OPERANDS:
2357       return MEM_VOLATILE_P (x);
2358
2359       /* Memory ref can trap unless it's a static var or a stack slot.  */
2360     case MEM:
2361       if (MEM_NOTRAP_P (x))
2362         return 0;
2363       return rtx_addr_can_trap_p (XEXP (x, 0));
2364
2365       /* Division by a non-constant might trap.  */
2366     case DIV:
2367     case MOD:
2368     case UDIV:
2369     case UMOD:
2370       if (HONOR_SNANS (GET_MODE (x)))
2371         return 1;
2372       if (! CONSTANT_P (XEXP (x, 1))
2373           || (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
2374               && flag_trapping_math))
2375         return 1;
2376       if (XEXP (x, 1) == const0_rtx)
2377         return 1;
2378       break;
2379
2380     case EXPR_LIST:
2381       /* An EXPR_LIST is used to represent a function call.  This
2382          certainly may trap.  */
2383       return 1;
2384
2385     case GE:
2386     case GT:
2387     case LE:
2388     case LT:
2389     case LTGT:
2390     case COMPARE:
2391       /* Some floating point comparisons may trap.  */
2392       if (!flag_trapping_math)
2393         break;
2394       /* ??? There is no machine independent way to check for tests that trap
2395          when COMPARE is used, though many targets do make this distinction.
2396          For instance, sparc uses CCFPE for compares which generate exceptions
2397          and CCFP for compares which do not generate exceptions.  */
2398       if (HONOR_NANS (GET_MODE (x)))
2399         return 1;
2400       /* But often the compare has some CC mode, so check operand
2401          modes as well.  */
2402       if (HONOR_NANS (GET_MODE (XEXP (x, 0)))
2403           || HONOR_NANS (GET_MODE (XEXP (x, 1))))
2404         return 1;
2405       break;
2406
2407     case EQ:
2408     case NE:
2409       if (HONOR_SNANS (GET_MODE (x)))
2410         return 1;
2411       /* Often comparison is CC mode, so check operand modes.  */
2412       if (HONOR_SNANS (GET_MODE (XEXP (x, 0)))
2413           || HONOR_SNANS (GET_MODE (XEXP (x, 1))))
2414         return 1;
2415       break;
2416
2417     case FIX:
2418       /* Conversion of floating point might trap.  */
2419       if (flag_trapping_math && HONOR_NANS (GET_MODE (XEXP (x, 0))))
2420         return 1;
2421       break;
2422
2423     case NEG:
2424     case ABS:
2425       /* These operations don't trap even with floating point.  */
2426       break;
2427
2428     default:
2429       /* Any floating arithmetic may trap.  */
2430       if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
2431           && flag_trapping_math)
2432         return 1;
2433     }
2434
2435   fmt = GET_RTX_FORMAT (code);
2436   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2437     {
2438       if (fmt[i] == 'e')
2439         {
2440           if (may_trap_p (XEXP (x, i)))
2441             return 1;
2442         }
2443       else if (fmt[i] == 'E')
2444         {
2445           int j;
2446           for (j = 0; j < XVECLEN (x, i); j++)
2447             if (may_trap_p (XVECEXP (x, i, j)))
2448               return 1;
2449         }
2450     }
2451   return 0;
2452 }
2453 \f
2454 /* Return nonzero if X contains a comparison that is not either EQ or NE,
2455    i.e., an inequality.  */
2456
2457 int
2458 inequality_comparisons_p (rtx x)
2459 {
2460   const char *fmt;
2461   int len, i;
2462   enum rtx_code code = GET_CODE (x);
2463
2464   switch (code)
2465     {
2466     case REG:
2467     case SCRATCH:
2468     case PC:
2469     case CC0:
2470     case CONST_INT:
2471     case CONST_DOUBLE:
2472     case CONST_VECTOR:
2473     case CONST:
2474     case LABEL_REF:
2475     case SYMBOL_REF:
2476       return 0;
2477
2478     case LT:
2479     case LTU:
2480     case GT:
2481     case GTU:
2482     case LE:
2483     case LEU:
2484     case GE:
2485     case GEU:
2486       return 1;
2487
2488     default:
2489       break;
2490     }
2491
2492   len = GET_RTX_LENGTH (code);
2493   fmt = GET_RTX_FORMAT (code);
2494
2495   for (i = 0; i < len; i++)
2496     {
2497       if (fmt[i] == 'e')
2498         {
2499           if (inequality_comparisons_p (XEXP (x, i)))
2500             return 1;
2501         }
2502       else if (fmt[i] == 'E')
2503         {
2504           int j;
2505           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2506             if (inequality_comparisons_p (XVECEXP (x, i, j)))
2507               return 1;
2508         }
2509     }
2510
2511   return 0;
2512 }
2513 \f
2514 /* Replace any occurrence of FROM in X with TO.  The function does
2515    not enter into CONST_DOUBLE for the replace.
2516
2517    Note that copying is not done so X must not be shared unless all copies
2518    are to be modified.  */
2519
2520 rtx
2521 replace_rtx (rtx x, rtx from, rtx to)
2522 {
2523   int i, j;
2524   const char *fmt;
2525
2526   /* The following prevents loops occurrence when we change MEM in
2527      CONST_DOUBLE onto the same CONST_DOUBLE.  */
2528   if (x != 0 && GET_CODE (x) == CONST_DOUBLE)
2529     return x;
2530
2531   if (x == from)
2532     return to;
2533
2534   /* Allow this function to make replacements in EXPR_LISTs.  */
2535   if (x == 0)
2536     return 0;
2537
2538   if (GET_CODE (x) == SUBREG)
2539     {
2540       rtx new = replace_rtx (SUBREG_REG (x), from, to);
2541
2542       if (GET_CODE (new) == CONST_INT)
2543         {
2544           x = simplify_subreg (GET_MODE (x), new,
2545                                GET_MODE (SUBREG_REG (x)),
2546                                SUBREG_BYTE (x));
2547           if (! x)
2548             abort ();
2549         }
2550       else
2551         SUBREG_REG (x) = new;
2552
2553       return x;
2554     }
2555   else if (GET_CODE (x) == ZERO_EXTEND)
2556     {
2557       rtx new = replace_rtx (XEXP (x, 0), from, to);
2558
2559       if (GET_CODE (new) == CONST_INT)
2560         {
2561           x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
2562                                         new, GET_MODE (XEXP (x, 0)));
2563           if (! x)
2564             abort ();
2565         }
2566       else
2567         XEXP (x, 0) = new;
2568
2569       return x;
2570     }
2571
2572   fmt = GET_RTX_FORMAT (GET_CODE (x));
2573   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2574     {
2575       if (fmt[i] == 'e')
2576         XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
2577       else if (fmt[i] == 'E')
2578         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2579           XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
2580     }
2581
2582   return x;
2583 }
2584 \f
2585 /* Throughout the rtx X, replace many registers according to REG_MAP.
2586    Return the replacement for X (which may be X with altered contents).
2587    REG_MAP[R] is the replacement for register R, or 0 for don't replace.
2588    NREGS is the length of REG_MAP; regs >= NREGS are not mapped.
2589
2590    We only support REG_MAP entries of REG or SUBREG.  Also, hard registers
2591    should not be mapped to pseudos or vice versa since validate_change
2592    is not called.
2593
2594    If REPLACE_DEST is 1, replacements are also done in destinations;
2595    otherwise, only sources are replaced.  */
2596
2597 rtx
2598 replace_regs (rtx x, rtx *reg_map, unsigned int nregs, int replace_dest)
2599 {
2600   enum rtx_code code;
2601   int i;
2602   const char *fmt;
2603
2604   if (x == 0)
2605     return x;
2606
2607   code = GET_CODE (x);
2608   switch (code)
2609     {
2610     case SCRATCH:
2611     case PC:
2612     case CC0:
2613     case CONST_INT:
2614     case CONST_DOUBLE:
2615     case CONST_VECTOR:
2616     case CONST:
2617     case SYMBOL_REF:
2618     case LABEL_REF:
2619       return x;
2620
2621     case REG:
2622       /* Verify that the register has an entry before trying to access it.  */
2623       if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
2624         {
2625           /* SUBREGs can't be shared.  Always return a copy to ensure that if
2626              this replacement occurs more than once then each instance will
2627              get distinct rtx.  */
2628           if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
2629             return copy_rtx (reg_map[REGNO (x)]);
2630           return reg_map[REGNO (x)];
2631         }
2632       return x;
2633
2634     case SUBREG:
2635       /* Prevent making nested SUBREGs.  */
2636       if (REG_P (SUBREG_REG (x)) && REGNO (SUBREG_REG (x)) < nregs
2637           && reg_map[REGNO (SUBREG_REG (x))] != 0
2638           && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
2639         {
2640           rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
2641           return simplify_gen_subreg (GET_MODE (x), map_val,
2642                                       GET_MODE (SUBREG_REG (x)),
2643                                       SUBREG_BYTE (x));
2644         }
2645       break;
2646
2647     case SET:
2648       if (replace_dest)
2649         SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
2650
2651       else if (MEM_P (SET_DEST (x))
2652                || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2653         /* Even if we are not to replace destinations, replace register if it
2654            is CONTAINED in destination (destination is memory or
2655            STRICT_LOW_PART).  */
2656         XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
2657                                                reg_map, nregs, 0);
2658       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2659         /* Similarly, for ZERO_EXTRACT we replace all operands.  */
2660         break;
2661
2662       SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
2663       return x;
2664
2665     default:
2666       break;
2667     }
2668
2669   fmt = GET_RTX_FORMAT (code);
2670   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2671     {
2672       if (fmt[i] == 'e')
2673         XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
2674       else if (fmt[i] == 'E')
2675         {
2676           int j;
2677           for (j = 0; j < XVECLEN (x, i); j++)
2678             XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
2679                                               nregs, replace_dest);
2680         }
2681     }
2682   return x;
2683 }
2684
2685 /* Replace occurrences of the old label in *X with the new one.
2686    DATA is a REPLACE_LABEL_DATA containing the old and new labels.  */
2687
2688 int
2689 replace_label (rtx *x, void *data)
2690 {
2691   rtx l = *x;
2692   rtx old_label = ((replace_label_data *) data)->r1;
2693   rtx new_label = ((replace_label_data *) data)->r2;
2694   bool update_label_nuses = ((replace_label_data *) data)->update_label_nuses;
2695
2696   if (l == NULL_RTX)
2697     return 0;
2698
2699   if (GET_CODE (l) == SYMBOL_REF
2700       && CONSTANT_POOL_ADDRESS_P (l))
2701     {
2702       rtx c = get_pool_constant (l);
2703       if (rtx_referenced_p (old_label, c))
2704         {
2705           rtx new_c, new_l;
2706           replace_label_data *d = (replace_label_data *) data;
2707
2708           /* Create a copy of constant C; replace the label inside
2709              but do not update LABEL_NUSES because uses in constant pool
2710              are not counted.  */
2711           new_c = copy_rtx (c);
2712           d->update_label_nuses = false;
2713           for_each_rtx (&new_c, replace_label, data);
2714           d->update_label_nuses = update_label_nuses;
2715
2716           /* Add the new constant NEW_C to constant pool and replace
2717              the old reference to constant by new reference.  */
2718           new_l = XEXP (force_const_mem (get_pool_mode (l), new_c), 0);
2719           *x = replace_rtx (l, l, new_l);
2720         }
2721       return 0;
2722     }
2723
2724   /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
2725      field.  This is not handled by for_each_rtx because it doesn't
2726      handle unprinted ('0') fields.  */
2727   if (GET_CODE (l) == JUMP_INSN && JUMP_LABEL (l) == old_label)
2728     JUMP_LABEL (l) = new_label;
2729
2730   if ((GET_CODE (l) == LABEL_REF
2731        || GET_CODE (l) == INSN_LIST)
2732       && XEXP (l, 0) == old_label)
2733     {
2734       XEXP (l, 0) = new_label;
2735       if (update_label_nuses)
2736         {
2737           ++LABEL_NUSES (new_label);
2738           --LABEL_NUSES (old_label);
2739         }
2740       return 0;
2741     }
2742
2743   return 0;
2744 }
2745
2746 /* When *BODY is equal to X or X is directly referenced by *BODY
2747    return nonzero, thus FOR_EACH_RTX stops traversing and returns nonzero
2748    too, otherwise FOR_EACH_RTX continues traversing *BODY.  */
2749
2750 static int
2751 rtx_referenced_p_1 (rtx *body, void *x)
2752 {
2753   rtx y = (rtx) x;
2754
2755   if (*body == NULL_RTX)
2756     return y == NULL_RTX;
2757
2758   /* Return true if a label_ref *BODY refers to label Y.  */
2759   if (GET_CODE (*body) == LABEL_REF && GET_CODE (y) == CODE_LABEL)
2760     return XEXP (*body, 0) == y;
2761
2762   /* If *BODY is a reference to pool constant traverse the constant.  */
2763   if (GET_CODE (*body) == SYMBOL_REF
2764       && CONSTANT_POOL_ADDRESS_P (*body))
2765     return rtx_referenced_p (y, get_pool_constant (*body));
2766
2767   /* By default, compare the RTL expressions.  */
2768   return rtx_equal_p (*body, y);
2769 }
2770
2771 /* Return true if X is referenced in BODY.  */
2772
2773 int
2774 rtx_referenced_p (rtx x, rtx body)
2775 {
2776   return for_each_rtx (&body, rtx_referenced_p_1, x);
2777 }
2778
2779 /* If INSN is a tablejump return true and store the label (before jump table) to
2780    *LABELP and the jump table to *TABLEP.  LABELP and TABLEP may be NULL.  */
2781
2782 bool
2783 tablejump_p (rtx insn, rtx *labelp, rtx *tablep)
2784 {
2785   rtx label, table;
2786
2787   if (GET_CODE (insn) == JUMP_INSN
2788       && (label = JUMP_LABEL (insn)) != NULL_RTX
2789       && (table = next_active_insn (label)) != NULL_RTX
2790       && GET_CODE (table) == JUMP_INSN
2791       && (GET_CODE (PATTERN (table)) == ADDR_VEC
2792           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
2793     {
2794       if (labelp)
2795         *labelp = label;
2796       if (tablep)
2797         *tablep = table;
2798       return true;
2799     }
2800   return false;
2801 }
2802
2803 /* A subroutine of computed_jump_p, return 1 if X contains a REG or MEM or
2804    constant that is not in the constant pool and not in the condition
2805    of an IF_THEN_ELSE.  */
2806
2807 static int
2808 computed_jump_p_1 (rtx x)
2809 {
2810   enum rtx_code code = GET_CODE (x);
2811   int i, j;
2812   const char *fmt;
2813
2814   switch (code)
2815     {
2816     case LABEL_REF:
2817     case PC:
2818       return 0;
2819
2820     case CONST:
2821     case CONST_INT:
2822     case CONST_DOUBLE:
2823     case CONST_VECTOR:
2824     case SYMBOL_REF:
2825     case REG:
2826       return 1;
2827
2828     case MEM:
2829       return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2830                 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
2831
2832     case IF_THEN_ELSE:
2833       return (computed_jump_p_1 (XEXP (x, 1))
2834               || computed_jump_p_1 (XEXP (x, 2)));
2835
2836     default:
2837       break;
2838     }
2839
2840   fmt = GET_RTX_FORMAT (code);
2841   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2842     {
2843       if (fmt[i] == 'e'
2844           && computed_jump_p_1 (XEXP (x, i)))
2845         return 1;
2846
2847       else if (fmt[i] == 'E')
2848         for (j = 0; j < XVECLEN (x, i); j++)
2849           if (computed_jump_p_1 (XVECEXP (x, i, j)))
2850             return 1;
2851     }
2852
2853   return 0;
2854 }
2855
2856 /* Return nonzero if INSN is an indirect jump (aka computed jump).
2857
2858    Tablejumps and casesi insns are not considered indirect jumps;
2859    we can recognize them by a (use (label_ref)).  */
2860
2861 int
2862 computed_jump_p (rtx insn)
2863 {
2864   int i;
2865   if (GET_CODE (insn) == JUMP_INSN)
2866     {
2867       rtx pat = PATTERN (insn);
2868
2869       if (find_reg_note (insn, REG_LABEL, NULL_RTX))
2870         return 0;
2871       else if (GET_CODE (pat) == PARALLEL)
2872         {
2873           int len = XVECLEN (pat, 0);
2874           int has_use_labelref = 0;
2875
2876           for (i = len - 1; i >= 0; i--)
2877             if (GET_CODE (XVECEXP (pat, 0, i)) == USE
2878                 && (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
2879                     == LABEL_REF))
2880               has_use_labelref = 1;
2881
2882           if (! has_use_labelref)
2883             for (i = len - 1; i >= 0; i--)
2884               if (GET_CODE (XVECEXP (pat, 0, i)) == SET
2885                   && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
2886                   && computed_jump_p_1 (SET_SRC (XVECEXP (pat, 0, i))))
2887                 return 1;
2888         }
2889       else if (GET_CODE (pat) == SET
2890                && SET_DEST (pat) == pc_rtx
2891                && computed_jump_p_1 (SET_SRC (pat)))
2892         return 1;
2893     }
2894   return 0;
2895 }
2896
2897 /* Traverse X via depth-first search, calling F for each
2898    sub-expression (including X itself).  F is also passed the DATA.
2899    If F returns -1, do not traverse sub-expressions, but continue
2900    traversing the rest of the tree.  If F ever returns any other
2901    nonzero value, stop the traversal, and return the value returned
2902    by F.  Otherwise, return 0.  This function does not traverse inside
2903    tree structure that contains RTX_EXPRs, or into sub-expressions
2904    whose format code is `0' since it is not known whether or not those
2905    codes are actually RTL.
2906
2907    This routine is very general, and could (should?) be used to
2908    implement many of the other routines in this file.  */
2909
2910 int
2911 for_each_rtx (rtx *x, rtx_function f, void *data)
2912 {
2913   int result;
2914   int length;
2915   const char *format;
2916   int i;
2917
2918   /* Call F on X.  */
2919   result = (*f) (x, data);
2920   if (result == -1)
2921     /* Do not traverse sub-expressions.  */
2922     return 0;
2923   else if (result != 0)
2924     /* Stop the traversal.  */
2925     return result;
2926
2927   if (*x == NULL_RTX)
2928     /* There are no sub-expressions.  */
2929     return 0;
2930
2931   length = GET_RTX_LENGTH (GET_CODE (*x));
2932   format = GET_RTX_FORMAT (GET_CODE (*x));
2933
2934   for (i = 0; i < length; ++i)
2935     {
2936       switch (format[i])
2937         {
2938         case 'e':
2939           result = for_each_rtx (&XEXP (*x, i), f, data);
2940           if (result != 0)
2941             return result;
2942           break;
2943
2944         case 'V':
2945         case 'E':
2946           if (XVEC (*x, i) != 0)
2947             {
2948               int j;
2949               for (j = 0; j < XVECLEN (*x, i); ++j)
2950                 {
2951                   result = for_each_rtx (&XVECEXP (*x, i, j), f, data);
2952                   if (result != 0)
2953                     return result;
2954                 }
2955             }
2956           break;
2957
2958         default:
2959           /* Nothing to do.  */
2960           break;
2961         }
2962
2963     }
2964
2965   return 0;
2966 }
2967
2968 /* Searches X for any reference to REGNO, returning the rtx of the
2969    reference found if any.  Otherwise, returns NULL_RTX.  */
2970
2971 rtx
2972 regno_use_in (unsigned int regno, rtx x)
2973 {
2974   const char *fmt;
2975   int i, j;
2976   rtx tem;
2977
2978   if (REG_P (x) && REGNO (x) == regno)
2979     return x;
2980
2981   fmt = GET_RTX_FORMAT (GET_CODE (x));
2982   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2983     {
2984       if (fmt[i] == 'e')
2985         {
2986           if ((tem = regno_use_in (regno, XEXP (x, i))))
2987             return tem;
2988         }
2989       else if (fmt[i] == 'E')
2990         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2991           if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
2992             return tem;
2993     }
2994
2995   return NULL_RTX;
2996 }
2997
2998 /* Return a value indicating whether OP, an operand of a commutative
2999    operation, is preferred as the first or second operand.  The higher
3000    the value, the stronger the preference for being the first operand.
3001    We use negative values to indicate a preference for the first operand
3002    and positive values for the second operand.  */
3003
3004 int
3005 commutative_operand_precedence (rtx op)
3006 {
3007   enum rtx_code code = GET_CODE (op);
3008   
3009   /* Constants always come the second operand.  Prefer "nice" constants.  */
3010   if (code == CONST_INT)
3011     return -7;
3012   if (code == CONST_DOUBLE)
3013     return -6;
3014   op = avoid_constant_pool_reference (op);
3015
3016   switch (GET_RTX_CLASS (code))
3017     {
3018     case RTX_CONST_OBJ:
3019       if (code == CONST_INT)
3020         return -5;
3021       if (code == CONST_DOUBLE)
3022         return -4;
3023       return -3;
3024
3025     case RTX_EXTRA:
3026       /* SUBREGs of objects should come second.  */
3027       if (code == SUBREG && OBJECT_P (SUBREG_REG (op)))
3028         return -2;
3029
3030       if (!CONSTANT_P (op))
3031         return 0;
3032       else
3033         /* As for RTX_CONST_OBJ.  */
3034         return -3;
3035
3036     case RTX_OBJ:
3037       /* Complex expressions should be the first, so decrease priority
3038          of objects.  */
3039       return -1;
3040
3041     case RTX_COMM_ARITH:
3042       /* Prefer operands that are themselves commutative to be first.
3043          This helps to make things linear.  In particular,
3044          (and (and (reg) (reg)) (not (reg))) is canonical.  */
3045       return 4;
3046
3047     case RTX_BIN_ARITH:
3048       /* If only one operand is a binary expression, it will be the first
3049          operand.  In particular,  (plus (minus (reg) (reg)) (neg (reg)))
3050          is canonical, although it will usually be further simplified.  */
3051       return 2;
3052   
3053     case RTX_UNARY:
3054       /* Then prefer NEG and NOT.  */
3055       if (code == NEG || code == NOT)
3056         return 1;
3057
3058     default:
3059       return 0;
3060     }
3061 }
3062
3063 /* Return 1 iff it is necessary to swap operands of commutative operation
3064    in order to canonicalize expression.  */
3065
3066 int
3067 swap_commutative_operands_p (rtx x, rtx y)
3068 {
3069   return (commutative_operand_precedence (x)
3070           < commutative_operand_precedence (y));
3071 }
3072
3073 /* Return 1 if X is an autoincrement side effect and the register is
3074    not the stack pointer.  */
3075 int
3076 auto_inc_p (rtx x)
3077 {
3078   switch (GET_CODE (x))
3079     {
3080     case PRE_INC:
3081     case POST_INC:
3082     case PRE_DEC:
3083     case POST_DEC:
3084     case PRE_MODIFY:
3085     case POST_MODIFY:
3086       /* There are no REG_INC notes for SP.  */
3087       if (XEXP (x, 0) != stack_pointer_rtx)
3088         return 1;
3089     default:
3090       break;
3091     }
3092   return 0;
3093 }
3094
3095 /* Return 1 if the sequence of instructions beginning with FROM and up
3096    to and including TO is safe to move.  If NEW_TO is non-NULL, and
3097    the sequence is not already safe to move, but can be easily
3098    extended to a sequence which is safe, then NEW_TO will point to the
3099    end of the extended sequence.
3100
3101    For now, this function only checks that the region contains whole
3102    exception regions, but it could be extended to check additional
3103    conditions as well.  */
3104
3105 int
3106 insns_safe_to_move_p (rtx from, rtx to, rtx *new_to)
3107 {
3108   int eh_region_count = 0;
3109   int past_to_p = 0;
3110   rtx r = from;
3111
3112   /* By default, assume the end of the region will be what was
3113      suggested.  */
3114   if (new_to)
3115     *new_to = to;
3116
3117   while (r)
3118     {
3119       if (GET_CODE (r) == NOTE)
3120         {
3121           switch (NOTE_LINE_NUMBER (r))
3122             {
3123             case NOTE_INSN_EH_REGION_BEG:
3124               ++eh_region_count;
3125               break;
3126
3127             case NOTE_INSN_EH_REGION_END:
3128               if (eh_region_count == 0)
3129                 /* This sequence of instructions contains the end of
3130                    an exception region, but not he beginning.  Moving
3131                    it will cause chaos.  */
3132                 return 0;
3133
3134               --eh_region_count;
3135               break;
3136
3137             default:
3138               break;
3139             }
3140         }
3141       else if (past_to_p)
3142         /* If we've passed TO, and we see a non-note instruction, we
3143            can't extend the sequence to a movable sequence.  */
3144         return 0;
3145
3146       if (r == to)
3147         {
3148           if (!new_to)
3149             /* It's OK to move the sequence if there were matched sets of
3150                exception region notes.  */
3151             return eh_region_count == 0;
3152
3153           past_to_p = 1;
3154         }
3155
3156       /* It's OK to move the sequence if there were matched sets of
3157          exception region notes.  */
3158       if (past_to_p && eh_region_count == 0)
3159         {
3160           *new_to = r;
3161           return 1;
3162         }
3163
3164       /* Go to the next instruction.  */
3165       r = NEXT_INSN (r);
3166     }
3167
3168   return 0;
3169 }
3170
3171 /* Return nonzero if IN contains a piece of rtl that has the address LOC.  */
3172 int
3173 loc_mentioned_in_p (rtx *loc, rtx in)
3174 {
3175   enum rtx_code code = GET_CODE (in);
3176   const char *fmt = GET_RTX_FORMAT (code);
3177   int i, j;
3178
3179   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3180     {
3181       if (loc == &in->u.fld[i].rtx)
3182         return 1;
3183       if (fmt[i] == 'e')
3184         {
3185           if (loc_mentioned_in_p (loc, XEXP (in, i)))
3186             return 1;
3187         }
3188       else if (fmt[i] == 'E')
3189         for (j = XVECLEN (in, i) - 1; j >= 0; j--)
3190           if (loc_mentioned_in_p (loc, XVECEXP (in, i, j)))
3191             return 1;
3192     }
3193   return 0;
3194 }
3195
3196 /* Helper function for subreg_lsb.  Given a subreg's OUTER_MODE, INNER_MODE,
3197    and SUBREG_BYTE, return the bit offset where the subreg begins
3198    (counting from the least significant bit of the operand).  */
3199
3200 unsigned int
3201 subreg_lsb_1 (enum machine_mode outer_mode,
3202               enum machine_mode inner_mode,
3203               unsigned int subreg_byte)
3204 {
3205   unsigned int bitpos;
3206   unsigned int byte;
3207   unsigned int word;
3208
3209   /* A paradoxical subreg begins at bit position 0.  */
3210   if (GET_MODE_BITSIZE (outer_mode) > GET_MODE_BITSIZE (inner_mode))
3211     return 0;
3212
3213   if (WORDS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
3214     /* If the subreg crosses a word boundary ensure that
3215        it also begins and ends on a word boundary.  */
3216     if ((subreg_byte % UNITS_PER_WORD
3217          + GET_MODE_SIZE (outer_mode)) > UNITS_PER_WORD
3218         && (subreg_byte % UNITS_PER_WORD
3219             || GET_MODE_SIZE (outer_mode) % UNITS_PER_WORD))
3220         abort ();
3221
3222   if (WORDS_BIG_ENDIAN)
3223     word = (GET_MODE_SIZE (inner_mode)
3224             - (subreg_byte + GET_MODE_SIZE (outer_mode))) / UNITS_PER_WORD;
3225   else
3226     word = subreg_byte / UNITS_PER_WORD;
3227   bitpos = word * BITS_PER_WORD;
3228
3229   if (BYTES_BIG_ENDIAN)
3230     byte = (GET_MODE_SIZE (inner_mode)
3231             - (subreg_byte + GET_MODE_SIZE (outer_mode))) % UNITS_PER_WORD;
3232   else
3233     byte = subreg_byte % UNITS_PER_WORD;
3234   bitpos += byte * BITS_PER_UNIT;
3235
3236   return bitpos;
3237 }
3238
3239 /* Given a subreg X, return the bit offset where the subreg begins
3240    (counting from the least significant bit of the reg).  */
3241
3242 unsigned int
3243 subreg_lsb (rtx x)
3244 {
3245   return subreg_lsb_1 (GET_MODE (x), GET_MODE (SUBREG_REG (x)),
3246                        SUBREG_BYTE (x));
3247 }
3248
3249 /* This function returns the regno offset of a subreg expression.
3250    xregno - A regno of an inner hard subreg_reg (or what will become one).
3251    xmode  - The mode of xregno.
3252    offset - The byte offset.
3253    ymode  - The mode of a top level SUBREG (or what may become one).
3254    RETURN - The regno offset which would be used.  */
3255 unsigned int
3256 subreg_regno_offset (unsigned int xregno, enum machine_mode xmode,
3257                      unsigned int offset, enum machine_mode ymode)
3258 {
3259   int nregs_xmode, nregs_ymode;
3260   int mode_multiple, nregs_multiple;
3261   int y_offset;
3262
3263   if (xregno >= FIRST_PSEUDO_REGISTER)
3264     abort ();
3265
3266   nregs_xmode = hard_regno_nregs[xregno][xmode];
3267   nregs_ymode = hard_regno_nregs[xregno][ymode];
3268
3269   /* If this is a big endian paradoxical subreg, which uses more actual
3270      hard registers than the original register, we must return a negative
3271      offset so that we find the proper highpart of the register.  */
3272   if (offset == 0
3273       && nregs_ymode > nregs_xmode
3274       && (GET_MODE_SIZE (ymode) > UNITS_PER_WORD
3275           ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
3276     return nregs_xmode - nregs_ymode;
3277
3278   if (offset == 0 || nregs_xmode == nregs_ymode)
3279     return 0;
3280
3281   /* size of ymode must not be greater than the size of xmode.  */
3282   mode_multiple = GET_MODE_SIZE (xmode) / GET_MODE_SIZE (ymode);
3283   if (mode_multiple == 0)
3284     abort ();
3285
3286   y_offset = offset / GET_MODE_SIZE (ymode);
3287   nregs_multiple =  nregs_xmode / nregs_ymode;
3288   return (y_offset / (mode_multiple / nregs_multiple)) * nregs_ymode;
3289 }
3290
3291 /* This function returns true when the offset is representable via
3292    subreg_offset in the given regno.
3293    xregno - A regno of an inner hard subreg_reg (or what will become one).
3294    xmode  - The mode of xregno.
3295    offset - The byte offset.
3296    ymode  - The mode of a top level SUBREG (or what may become one).
3297    RETURN - The regno offset which would be used.  */
3298 bool
3299 subreg_offset_representable_p (unsigned int xregno, enum machine_mode xmode,
3300                                unsigned int offset, enum machine_mode ymode)
3301 {
3302   int nregs_xmode, nregs_ymode;
3303   int mode_multiple, nregs_multiple;
3304   int y_offset;
3305
3306   if (xregno >= FIRST_PSEUDO_REGISTER)
3307     abort ();
3308
3309   nregs_xmode = hard_regno_nregs[xregno][xmode];
3310   nregs_ymode = hard_regno_nregs[xregno][ymode];
3311
3312   /* Paradoxical subregs are always valid.  */
3313   if (offset == 0
3314       && nregs_ymode > nregs_xmode
3315       && (GET_MODE_SIZE (ymode) > UNITS_PER_WORD
3316           ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
3317     return true;
3318
3319   /* Lowpart subregs are always valid.  */
3320   if (offset == subreg_lowpart_offset (ymode, xmode))
3321     return true;
3322
3323 #ifdef ENABLE_CHECKING
3324   /* This should always pass, otherwise we don't know how to verify the
3325      constraint.  These conditions may be relaxed but subreg_offset would
3326      need to be redesigned.  */
3327   if (GET_MODE_SIZE (xmode) % GET_MODE_SIZE (ymode)
3328       || GET_MODE_SIZE (ymode) % nregs_ymode
3329       || nregs_xmode % nregs_ymode)
3330     abort ();
3331 #endif
3332
3333   /* The XMODE value can be seen as a vector of NREGS_XMODE
3334      values.  The subreg must represent a lowpart of given field.
3335      Compute what field it is.  */
3336   offset -= subreg_lowpart_offset (ymode,
3337                                    mode_for_size (GET_MODE_BITSIZE (xmode)
3338                                                   / nregs_xmode,
3339                                                   MODE_INT, 0));
3340
3341   /* size of ymode must not be greater than the size of xmode.  */
3342   mode_multiple = GET_MODE_SIZE (xmode) / GET_MODE_SIZE (ymode);
3343   if (mode_multiple == 0)
3344     abort ();
3345
3346   y_offset = offset / GET_MODE_SIZE (ymode);
3347   nregs_multiple =  nregs_xmode / nregs_ymode;
3348 #ifdef ENABLE_CHECKING
3349   if (offset % GET_MODE_SIZE (ymode)
3350       || mode_multiple % nregs_multiple)
3351     abort ();
3352 #endif
3353   return (!(y_offset % (mode_multiple / nregs_multiple)));
3354 }
3355
3356 /* Return the final regno that a subreg expression refers to.  */
3357 unsigned int
3358 subreg_regno (rtx x)
3359 {
3360   unsigned int ret;
3361   rtx subreg = SUBREG_REG (x);
3362   int regno = REGNO (subreg);
3363
3364   ret = regno + subreg_regno_offset (regno,
3365                                      GET_MODE (subreg),
3366                                      SUBREG_BYTE (x),
3367                                      GET_MODE (x));
3368   return ret;
3369
3370 }
3371 struct parms_set_data
3372 {
3373   int nregs;
3374   HARD_REG_SET regs;
3375 };
3376
3377 /* Helper function for noticing stores to parameter registers.  */
3378 static void
3379 parms_set (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
3380 {
3381   struct parms_set_data *d = data;
3382   if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
3383       && TEST_HARD_REG_BIT (d->regs, REGNO (x)))
3384     {
3385       CLEAR_HARD_REG_BIT (d->regs, REGNO (x));
3386       d->nregs--;
3387     }
3388 }
3389
3390 /* Look backward for first parameter to be loaded.
3391    Do not skip BOUNDARY.  */
3392 rtx
3393 find_first_parameter_load (rtx call_insn, rtx boundary)
3394 {
3395   struct parms_set_data parm;
3396   rtx p, before;
3397
3398   /* Since different machines initialize their parameter registers
3399      in different orders, assume nothing.  Collect the set of all
3400      parameter registers.  */
3401   CLEAR_HARD_REG_SET (parm.regs);
3402   parm.nregs = 0;
3403   for (p = CALL_INSN_FUNCTION_USAGE (call_insn); p; p = XEXP (p, 1))
3404     if (GET_CODE (XEXP (p, 0)) == USE
3405         && REG_P (XEXP (XEXP (p, 0), 0)))
3406       {
3407         if (REGNO (XEXP (XEXP (p, 0), 0)) >= FIRST_PSEUDO_REGISTER)
3408           abort ();
3409
3410         /* We only care about registers which can hold function
3411            arguments.  */
3412         if (!FUNCTION_ARG_REGNO_P (REGNO (XEXP (XEXP (p, 0), 0))))
3413           continue;
3414
3415         SET_HARD_REG_BIT (parm.regs, REGNO (XEXP (XEXP (p, 0), 0)));
3416         parm.nregs++;
3417       }
3418   before = call_insn;
3419
3420   /* Search backward for the first set of a register in this set.  */
3421   while (parm.nregs && before != boundary)
3422     {
3423       before = PREV_INSN (before);
3424
3425       /* It is possible that some loads got CSEed from one call to
3426          another.  Stop in that case.  */
3427       if (GET_CODE (before) == CALL_INSN)
3428         break;
3429
3430       /* Our caller needs either ensure that we will find all sets
3431          (in case code has not been optimized yet), or take care
3432          for possible labels in a way by setting boundary to preceding
3433          CODE_LABEL.  */
3434       if (GET_CODE (before) == CODE_LABEL)
3435         {
3436           if (before != boundary)
3437             abort ();
3438           break;
3439         }
3440
3441       if (INSN_P (before))
3442         note_stores (PATTERN (before), parms_set, &parm);
3443     }
3444   return before;
3445 }
3446
3447 /* Return true if we should avoid inserting code between INSN and preceding
3448    call instruction.  */
3449
3450 bool
3451 keep_with_call_p (rtx insn)
3452 {
3453   rtx set;
3454
3455   if (INSN_P (insn) && (set = single_set (insn)) != NULL)
3456     {
3457       if (REG_P (SET_DEST (set))
3458           && REGNO (SET_DEST (set)) < FIRST_PSEUDO_REGISTER
3459           && fixed_regs[REGNO (SET_DEST (set))]
3460           && general_operand (SET_SRC (set), VOIDmode))
3461         return true;
3462       if (REG_P (SET_SRC (set))
3463           && FUNCTION_VALUE_REGNO_P (REGNO (SET_SRC (set)))
3464           && REG_P (SET_DEST (set))
3465           && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3466         return true;
3467       /* There may be a stack pop just after the call and before the store
3468          of the return register.  Search for the actual store when deciding
3469          if we can break or not.  */
3470       if (SET_DEST (set) == stack_pointer_rtx)
3471         {
3472           rtx i2 = next_nonnote_insn (insn);
3473           if (i2 && keep_with_call_p (i2))
3474             return true;
3475         }
3476     }
3477   return false;
3478 }
3479
3480 /* Return true when store to register X can be hoisted to the place
3481    with LIVE registers (can be NULL).  Value VAL contains destination
3482    whose value will be used.  */
3483
3484 static bool
3485 hoist_test_store (rtx x, rtx val, regset live)
3486 {
3487   if (GET_CODE (x) == SCRATCH)
3488     return true;
3489
3490   if (rtx_equal_p (x, val))
3491     return true;
3492
3493   /* Allow subreg of X in case it is not writing just part of multireg pseudo.
3494      Then we would need to update all users to care hoisting the store too.
3495      Caller may represent that by specifying whole subreg as val.  */
3496
3497   if (GET_CODE (x) == SUBREG && rtx_equal_p (SUBREG_REG (x), val))
3498     {
3499       if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) > UNITS_PER_WORD
3500           && GET_MODE_BITSIZE (GET_MODE (x)) <
3501           GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
3502         return false;
3503       return true;
3504     }
3505   if (GET_CODE (x) == SUBREG)
3506     x = SUBREG_REG (x);
3507
3508   /* Anything except register store is not hoistable.  This includes the
3509      partial stores to registers.  */
3510
3511   if (!REG_P (x))
3512     return false;
3513
3514   /* Pseudo registers can be always replaced by another pseudo to avoid
3515      the side effect, for hard register we must ensure that they are dead.
3516      Eventually we may want to add code to try turn pseudos to hards, but it
3517      is unlikely useful.  */
3518
3519   if (REGNO (x) < FIRST_PSEUDO_REGISTER)
3520     {
3521       int regno = REGNO (x);
3522       int n = hard_regno_nregs[regno][GET_MODE (x)];
3523
3524       if (!live)
3525         return false;
3526       if (REGNO_REG_SET_P (live, regno))
3527         return false;
3528       while (--n > 0)
3529         if (REGNO_REG_SET_P (live, regno + n))
3530           return false;
3531     }
3532   return true;
3533 }
3534
3535
3536 /* Return true if INSN can be hoisted to place with LIVE hard registers
3537    (LIVE can be NULL when unknown).  VAL is expected to be stored by the insn
3538    and used by the hoisting pass.  */
3539
3540 bool
3541 can_hoist_insn_p (rtx insn, rtx val, regset live)
3542 {
3543   rtx pat = PATTERN (insn);
3544   int i;
3545
3546   /* It probably does not worth the complexity to handle multiple
3547      set stores.  */
3548   if (!single_set (insn))
3549     return false;
3550   /* We can move CALL_INSN, but we need to check that all caller clobbered
3551      regs are dead.  */
3552   if (GET_CODE (insn) == CALL_INSN)
3553     return false;
3554   /* In future we will handle hoisting of libcall sequences, but
3555      give up for now.  */
3556   if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
3557     return false;
3558   switch (GET_CODE (pat))
3559     {
3560     case SET:
3561       if (!hoist_test_store (SET_DEST (pat), val, live))
3562         return false;
3563       break;
3564     case USE:
3565       /* USES do have sick semantics, so do not move them.  */
3566       return false;
3567       break;
3568     case CLOBBER:
3569       if (!hoist_test_store (XEXP (pat, 0), val, live))
3570         return false;
3571       break;
3572     case PARALLEL:
3573       for (i = 0; i < XVECLEN (pat, 0); i++)
3574         {
3575           rtx x = XVECEXP (pat, 0, i);
3576           switch (GET_CODE (x))
3577             {
3578             case SET:
3579               if (!hoist_test_store (SET_DEST (x), val, live))
3580                 return false;
3581               break;
3582             case USE:
3583               /* We need to fix callers to really ensure availability
3584                  of all values insn uses, but for now it is safe to prohibit
3585                  hoisting of any insn having such a hidden uses.  */
3586               return false;
3587               break;
3588             case CLOBBER:
3589               if (!hoist_test_store (SET_DEST (x), val, live))
3590                 return false;
3591               break;
3592             default:
3593               break;
3594             }
3595         }
3596       break;
3597     default:
3598       abort ();
3599     }
3600   return true;
3601 }
3602
3603 /* Update store after hoisting - replace all stores to pseudo registers
3604    by new ones to avoid clobbering of values except for store to VAL that will
3605    be updated to NEW.  */
3606
3607 static void
3608 hoist_update_store (rtx insn, rtx *xp, rtx val, rtx new)
3609 {
3610   rtx x = *xp;
3611
3612   if (GET_CODE (x) == SCRATCH)
3613     return;
3614
3615   if (GET_CODE (x) == SUBREG && SUBREG_REG (x) == val)
3616     validate_change (insn, xp,
3617                      simplify_gen_subreg (GET_MODE (x), new, GET_MODE (new),
3618                                           SUBREG_BYTE (x)), 1);
3619   if (rtx_equal_p (x, val))
3620     {
3621       validate_change (insn, xp, new, 1);
3622       return;
3623     }
3624   if (GET_CODE (x) == SUBREG)
3625     {
3626       xp = &SUBREG_REG (x);
3627       x = *xp;
3628     }
3629
3630   if (!REG_P (x))
3631     abort ();
3632
3633   /* We've verified that hard registers are dead, so we may keep the side
3634      effect.  Otherwise replace it by new pseudo.  */
3635   if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
3636     validate_change (insn, xp, gen_reg_rtx (GET_MODE (x)), 1);
3637   REG_NOTES (insn)
3638     = alloc_EXPR_LIST (REG_UNUSED, *xp, REG_NOTES (insn));
3639 }
3640
3641 /* Create a copy of INSN after AFTER replacing store of VAL to NEW
3642    and each other side effect to pseudo register by new pseudo register.  */
3643
3644 rtx
3645 hoist_insn_after (rtx insn, rtx after, rtx val, rtx new)
3646 {
3647   rtx pat;
3648   int i;
3649   rtx note;
3650
3651   insn = emit_copy_of_insn_after (insn, after);
3652   pat = PATTERN (insn);
3653
3654   /* Remove REG_UNUSED notes as we will re-emit them.  */
3655   while ((note = find_reg_note (insn, REG_UNUSED, NULL_RTX)))
3656     remove_note (insn, note);
3657
3658   /* To get this working callers must ensure to move everything referenced
3659      by REG_EQUAL/REG_EQUIV notes too.  Lets remove them, it is probably
3660      easier.  */
3661   while ((note = find_reg_note (insn, REG_EQUAL, NULL_RTX)))
3662     remove_note (insn, note);
3663   while ((note = find_reg_note (insn, REG_EQUIV, NULL_RTX)))
3664     remove_note (insn, note);
3665
3666   /* Remove REG_DEAD notes as they might not be valid anymore in case
3667      we create redundancy.  */
3668   while ((note = find_reg_note (insn, REG_DEAD, NULL_RTX)))
3669     remove_note (insn, note);
3670   switch (GET_CODE (pat))
3671     {
3672     case SET:
3673       hoist_update_store (insn, &SET_DEST (pat), val, new);
3674       break;
3675     case USE:
3676       break;
3677     case CLOBBER:
3678       hoist_update_store (insn, &XEXP (pat, 0), val, new);
3679       break;
3680     case PARALLEL:
3681       for (i = 0; i < XVECLEN (pat, 0); i++)
3682         {
3683           rtx x = XVECEXP (pat, 0, i);
3684           switch (GET_CODE (x))
3685             {
3686             case SET:
3687               hoist_update_store (insn, &SET_DEST (x), val, new);
3688               break;
3689             case USE:
3690               break;
3691             case CLOBBER:
3692               hoist_update_store (insn, &SET_DEST (x), val, new);
3693               break;
3694             default:
3695               break;
3696             }
3697         }
3698       break;
3699     default:
3700       abort ();
3701     }
3702   if (!apply_change_group ())
3703     abort ();
3704
3705   return insn;
3706 }
3707
3708 rtx
3709 hoist_insn_to_edge (rtx insn, edge e, rtx val, rtx new)
3710 {
3711   rtx new_insn;
3712
3713   /* We cannot insert instructions on an abnormal critical edge.
3714      It will be easier to find the culprit if we die now.  */
3715   if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
3716     abort ();
3717
3718   /* Do not use emit_insn_on_edge as we want to preserve notes and similar
3719      stuff.  We also emit CALL_INSNS and firends.  */
3720   if (e->insns.r == NULL_RTX)
3721     {
3722       start_sequence ();
3723       emit_note (NOTE_INSN_DELETED);
3724     }
3725   else
3726     push_to_sequence (e->insns.r);
3727
3728   new_insn = hoist_insn_after (insn, get_last_insn (), val, new);
3729
3730   e->insns.r = get_insns ();
3731   end_sequence ();
3732   return new_insn;
3733 }
3734
3735 /* Return true if LABEL is a target of JUMP_INSN.  This applies only
3736    to non-complex jumps.  That is, direct unconditional, conditional,
3737    and tablejumps, but not computed jumps or returns.  It also does
3738    not apply to the fallthru case of a conditional jump.  */
3739
3740 bool
3741 label_is_jump_target_p (rtx label, rtx jump_insn)
3742 {
3743   rtx tmp = JUMP_LABEL (jump_insn);
3744
3745   if (label == tmp)
3746     return true;
3747
3748   if (tablejump_p (jump_insn, NULL, &tmp))
3749     {
3750       rtvec vec = XVEC (PATTERN (tmp),
3751                         GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC);
3752       int i, veclen = GET_NUM_ELEM (vec);
3753
3754       for (i = 0; i < veclen; ++i)
3755         if (XEXP (RTVEC_ELT (vec, i), 0) == label)
3756           return true;
3757     }
3758
3759   return false;
3760 }
3761
3762 \f
3763 /* Return an estimate of the cost of computing rtx X.
3764    One use is in cse, to decide which expression to keep in the hash table.
3765    Another is in rtl generation, to pick the cheapest way to multiply.
3766    Other uses like the latter are expected in the future.  */
3767
3768 int
3769 rtx_cost (rtx x, enum rtx_code outer_code ATTRIBUTE_UNUSED)
3770 {
3771   int i, j;
3772   enum rtx_code code;
3773   const char *fmt;
3774   int total;
3775
3776   if (x == 0)
3777     return 0;
3778
3779   /* Compute the default costs of certain things.
3780      Note that targetm.rtx_costs can override the defaults.  */
3781
3782   code = GET_CODE (x);
3783   switch (code)
3784     {
3785     case MULT:
3786       total = COSTS_N_INSNS (5);
3787       break;
3788     case DIV:
3789     case UDIV:
3790     case MOD:
3791     case UMOD:
3792       total = COSTS_N_INSNS (7);
3793       break;
3794     case USE:
3795       /* Used in loop.c and combine.c as a marker.  */
3796       total = 0;
3797       break;
3798     default:
3799       total = COSTS_N_INSNS (1);
3800     }
3801
3802   switch (code)
3803     {
3804     case REG:
3805       return 0;
3806
3807     case SUBREG:
3808       /* If we can't tie these modes, make this expensive.  The larger
3809          the mode, the more expensive it is.  */
3810       if (! MODES_TIEABLE_P (GET_MODE (x), GET_MODE (SUBREG_REG (x))))
3811         return COSTS_N_INSNS (2
3812                               + GET_MODE_SIZE (GET_MODE (x)) / UNITS_PER_WORD);
3813       break;
3814
3815     default:
3816       if (targetm.rtx_costs (x, code, outer_code, &total))
3817         return total;
3818       break;
3819     }
3820
3821   /* Sum the costs of the sub-rtx's, plus cost of this operation,
3822      which is already in total.  */
3823
3824   fmt = GET_RTX_FORMAT (code);
3825   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3826     if (fmt[i] == 'e')
3827       total += rtx_cost (XEXP (x, i), code);
3828     else if (fmt[i] == 'E')
3829       for (j = 0; j < XVECLEN (x, i); j++)
3830         total += rtx_cost (XVECEXP (x, i, j), code);
3831
3832   return total;
3833 }
3834 \f
3835 /* Return cost of address expression X.
3836    Expect that X is properly formed address reference.  */
3837
3838 int
3839 address_cost (rtx x, enum machine_mode mode)
3840 {
3841   /* The address_cost target hook does not deal with ADDRESSOF nodes.  But,
3842      during CSE, such nodes are present.  Using an ADDRESSOF node which
3843      refers to the address of a REG is a good thing because we can then
3844      turn (MEM (ADDRESSOF (REG))) into just plain REG.  */
3845
3846   if (GET_CODE (x) == ADDRESSOF && REG_P (XEXP ((x), 0)))
3847     return -1;
3848
3849   /* We may be asked for cost of various unusual addresses, such as operands
3850      of push instruction.  It is not worthwhile to complicate writing
3851      of the target hook by such cases.  */
3852
3853   if (!memory_address_p (mode, x))
3854     return 1000;
3855
3856   return targetm.address_cost (x);
3857 }
3858
3859 /* If the target doesn't override, compute the cost as with arithmetic.  */
3860
3861 int
3862 default_address_cost (rtx x)
3863 {
3864   return rtx_cost (x, MEM);
3865 }
3866 \f
3867
3868 unsigned HOST_WIDE_INT
3869 nonzero_bits (rtx x, enum machine_mode mode)
3870 {
3871   return cached_nonzero_bits (x, mode, NULL_RTX, VOIDmode, 0);
3872 }
3873
3874 unsigned int
3875 num_sign_bit_copies (rtx x, enum machine_mode mode)
3876 {
3877   return cached_num_sign_bit_copies (x, mode, NULL_RTX, VOIDmode, 0);
3878 }
3879
3880 /* The function cached_nonzero_bits is a wrapper around nonzero_bits1.
3881    It avoids exponential behavior in nonzero_bits1 when X has
3882    identical subexpressions on the first or the second level.  */
3883
3884 static unsigned HOST_WIDE_INT
3885 cached_nonzero_bits (rtx x, enum machine_mode mode, rtx known_x,
3886                      enum machine_mode known_mode,
3887                      unsigned HOST_WIDE_INT known_ret)
3888 {
3889   if (x == known_x && mode == known_mode)
3890     return known_ret;
3891
3892   /* Try to find identical subexpressions.  If found call
3893      nonzero_bits1 on X with the subexpressions as KNOWN_X and the
3894      precomputed value for the subexpression as KNOWN_RET.  */
3895
3896   if (ARITHMETIC_P (x))
3897     {
3898       rtx x0 = XEXP (x, 0);
3899       rtx x1 = XEXP (x, 1);
3900
3901       /* Check the first level.  */
3902       if (x0 == x1)
3903         return nonzero_bits1 (x, mode, x0, mode,
3904                               cached_nonzero_bits (x0, mode, known_x,
3905                                                    known_mode, known_ret));
3906
3907       /* Check the second level.  */
3908       if (ARITHMETIC_P (x0)
3909           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
3910         return nonzero_bits1 (x, mode, x1, mode,
3911                               cached_nonzero_bits (x1, mode, known_x,
3912                                                    known_mode, known_ret));
3913
3914       if (ARITHMETIC_P (x1)
3915           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
3916         return nonzero_bits1 (x, mode, x0, mode,
3917                               cached_nonzero_bits (x0, mode, known_x,
3918                                                    known_mode, known_ret));
3919     }
3920
3921   return nonzero_bits1 (x, mode, known_x, known_mode, known_ret);
3922 }
3923
3924 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
3925    We don't let nonzero_bits recur into num_sign_bit_copies, because that
3926    is less useful.  We can't allow both, because that results in exponential
3927    run time recursion.  There is a nullstone testcase that triggered
3928    this.  This macro avoids accidental uses of num_sign_bit_copies.  */
3929 #define cached_num_sign_bit_copies sorry_i_am_preventing_exponential_behavior
3930
3931 /* Given an expression, X, compute which bits in X can be nonzero.
3932    We don't care about bits outside of those defined in MODE.
3933
3934    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
3935    an arithmetic operation, we can do better.  */
3936
3937 static unsigned HOST_WIDE_INT
3938 nonzero_bits1 (rtx x, enum machine_mode mode, rtx known_x,
3939                enum machine_mode known_mode,
3940                unsigned HOST_WIDE_INT known_ret)
3941 {
3942   unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
3943   unsigned HOST_WIDE_INT inner_nz;
3944   enum rtx_code code;
3945   unsigned int mode_width = GET_MODE_BITSIZE (mode);
3946
3947   /* For floating-point values, assume all bits are needed.  */
3948   if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
3949     return nonzero;
3950
3951   /* If X is wider than MODE, use its mode instead.  */
3952   if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
3953     {
3954       mode = GET_MODE (x);
3955       nonzero = GET_MODE_MASK (mode);
3956       mode_width = GET_MODE_BITSIZE (mode);
3957     }
3958
3959   if (mode_width > HOST_BITS_PER_WIDE_INT)
3960     /* Our only callers in this case look for single bit values.  So
3961        just return the mode mask.  Those tests will then be false.  */
3962     return nonzero;
3963
3964 #ifndef WORD_REGISTER_OPERATIONS
3965   /* If MODE is wider than X, but both are a single word for both the host
3966      and target machines, we can compute this from which bits of the
3967      object might be nonzero in its own mode, taking into account the fact
3968      that on many CISC machines, accessing an object in a wider mode
3969      causes the high-order bits to become undefined.  So they are
3970      not known to be zero.  */
3971
3972   if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
3973       && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
3974       && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
3975       && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
3976     {
3977       nonzero &= cached_nonzero_bits (x, GET_MODE (x),
3978                                       known_x, known_mode, known_ret);
3979       nonzero |= GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x));
3980       return nonzero;
3981     }
3982 #endif
3983
3984   code = GET_CODE (x);
3985   switch (code)
3986     {
3987     case REG:
3988 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
3989       /* If pointers extend unsigned and this is a pointer in Pmode, say that
3990          all the bits above ptr_mode are known to be zero.  */
3991       if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
3992           && REG_POINTER (x))
3993         nonzero &= GET_MODE_MASK (ptr_mode);
3994 #endif
3995
3996       /* Include declared information about alignment of pointers.  */
3997       /* ??? We don't properly preserve REG_POINTER changes across
3998          pointer-to-integer casts, so we can't trust it except for
3999          things that we know must be pointers.  See execute/960116-1.c.  */
4000       if ((x == stack_pointer_rtx
4001            || x == frame_pointer_rtx
4002            || x == arg_pointer_rtx)
4003           && REGNO_POINTER_ALIGN (REGNO (x)))
4004         {
4005           unsigned HOST_WIDE_INT alignment
4006             = REGNO_POINTER_ALIGN (REGNO (x)) / BITS_PER_UNIT;
4007
4008 #ifdef PUSH_ROUNDING
4009           /* If PUSH_ROUNDING is defined, it is possible for the
4010              stack to be momentarily aligned only to that amount,
4011              so we pick the least alignment.  */
4012           if (x == stack_pointer_rtx && PUSH_ARGS)
4013             alignment = MIN ((unsigned HOST_WIDE_INT) PUSH_ROUNDING (1),
4014                              alignment);
4015 #endif
4016
4017           nonzero &= ~(alignment - 1);
4018         }
4019
4020       {
4021         unsigned HOST_WIDE_INT nonzero_for_hook = nonzero;
4022         rtx new = rtl_hooks.reg_nonzero_bits (x, mode, known_x,
4023                                               known_mode, known_ret,
4024                                               &nonzero_for_hook);
4025
4026         if (new)
4027           nonzero_for_hook &= cached_nonzero_bits (new, mode, known_x,
4028                                                    known_mode, known_ret);
4029
4030         return nonzero_for_hook;
4031       }
4032
4033     case CONST_INT:
4034 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
4035       /* If X is negative in MODE, sign-extend the value.  */
4036       if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
4037           && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
4038         return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
4039 #endif
4040
4041       return INTVAL (x);
4042
4043     case MEM:
4044 #ifdef LOAD_EXTEND_OP
4045       /* In many, if not most, RISC machines, reading a byte from memory
4046          zeros the rest of the register.  Noticing that fact saves a lot
4047          of extra zero-extends.  */
4048       if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
4049         nonzero &= GET_MODE_MASK (GET_MODE (x));
4050 #endif
4051       break;
4052
4053     case EQ:  case NE:
4054     case UNEQ:  case LTGT:
4055     case GT:  case GTU:  case UNGT:
4056     case LT:  case LTU:  case UNLT:
4057     case GE:  case GEU:  case UNGE:
4058     case LE:  case LEU:  case UNLE:
4059     case UNORDERED: case ORDERED:
4060
4061       /* If this produces an integer result, we know which bits are set.
4062          Code here used to clear bits outside the mode of X, but that is
4063          now done above.  */
4064
4065       if (GET_MODE_CLASS (mode) == MODE_INT
4066           && mode_width <= HOST_BITS_PER_WIDE_INT)
4067         nonzero = STORE_FLAG_VALUE;
4068       break;
4069
4070     case NEG:
4071 #if 0
4072       /* Disabled to avoid exponential mutual recursion between nonzero_bits
4073          and num_sign_bit_copies.  */
4074       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
4075           == GET_MODE_BITSIZE (GET_MODE (x)))
4076         nonzero = 1;
4077 #endif
4078
4079       if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
4080         nonzero |= (GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x)));
4081       break;
4082
4083     case ABS:
4084 #if 0
4085       /* Disabled to avoid exponential mutual recursion between nonzero_bits
4086          and num_sign_bit_copies.  */
4087       if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
4088           == GET_MODE_BITSIZE (GET_MODE (x)))
4089         nonzero = 1;
4090 #endif
4091       break;
4092
4093     case TRUNCATE:
4094       nonzero &= (cached_nonzero_bits (XEXP (x, 0), mode,
4095                                        known_x, known_mode, known_ret)
4096                   & GET_MODE_MASK (mode));
4097       break;
4098
4099     case ZERO_EXTEND:
4100       nonzero &= cached_nonzero_bits (XEXP (x, 0), mode,
4101                                       known_x, known_mode, known_ret);
4102       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
4103         nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
4104       break;
4105
4106     case SIGN_EXTEND:
4107       /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
4108          Otherwise, show all the bits in the outer mode but not the inner
4109          may be nonzero.  */
4110       inner_nz = cached_nonzero_bits (XEXP (x, 0), mode,
4111                                       known_x, known_mode, known_ret);
4112       if (GET_MODE (XEXP (x, 0)) != VOIDmode)
4113         {
4114           inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
4115           if (inner_nz
4116               & (((HOST_WIDE_INT) 1
4117                   << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
4118             inner_nz |= (GET_MODE_MASK (mode)
4119                          & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
4120         }
4121
4122       nonzero &= inner_nz;
4123       break;
4124
4125     case AND:
4126       nonzero &= cached_nonzero_bits (XEXP (x, 0), mode,
4127                                        known_x, known_mode, known_ret)
4128                  & cached_nonzero_bits (XEXP (x, 1), mode,
4129                                         known_x, known_mode, known_ret);
4130       break;
4131
4132     case XOR:   case IOR:
4133     case UMIN:  case UMAX:  case SMIN:  case SMAX:
4134       {
4135         unsigned HOST_WIDE_INT nonzero0 =
4136           cached_nonzero_bits (XEXP (x, 0), mode,
4137                                known_x, known_mode, known_ret);
4138
4139         /* Don't call nonzero_bits for the second time if it cannot change
4140            anything.  */
4141         if ((nonzero & nonzero0) != nonzero)
4142           nonzero &= nonzero0
4143                      | cached_nonzero_bits (XEXP (x, 1), mode,
4144                                             known_x, known_mode, known_ret);
4145       }
4146       break;
4147
4148     case PLUS:  case MINUS:
4149     case MULT:
4150     case DIV:   case UDIV:
4151     case MOD:   case UMOD:
4152       /* We can apply the rules of arithmetic to compute the number of
4153          high- and low-order zero bits of these operations.  We start by
4154          computing the width (position of the highest-order nonzero bit)
4155          and the number of low-order zero bits for each value.  */
4156       {
4157         unsigned HOST_WIDE_INT nz0 =
4158           cached_nonzero_bits (XEXP (x, 0), mode,
4159                                known_x, known_mode, known_ret);
4160         unsigned HOST_WIDE_INT nz1 =
4161           cached_nonzero_bits (XEXP (x, 1), mode,
4162                                known_x, known_mode, known_ret);
4163         int sign_index = GET_MODE_BITSIZE (GET_MODE (x)) - 1;
4164         int width0 = floor_log2 (nz0) + 1;
4165         int width1 = floor_log2 (nz1) + 1;
4166         int low0 = floor_log2 (nz0 & -nz0);
4167         int low1 = floor_log2 (nz1 & -nz1);
4168         HOST_WIDE_INT op0_maybe_minusp
4169           = (nz0 & ((HOST_WIDE_INT) 1 << sign_index));
4170         HOST_WIDE_INT op1_maybe_minusp
4171           = (nz1 & ((HOST_WIDE_INT) 1 << sign_index));
4172         unsigned int result_width = mode_width;
4173         int result_low = 0;
4174
4175         switch (code)
4176           {
4177           case PLUS:
4178             result_width = MAX (width0, width1) + 1;
4179             result_low = MIN (low0, low1);
4180             break;
4181           case MINUS:
4182             result_low = MIN (low0, low1);
4183             break;
4184           case MULT:
4185             result_width = width0 + width1;
4186             result_low = low0 + low1;
4187             break;
4188           case DIV:
4189             if (width1 == 0)
4190               break;
4191             if (! op0_maybe_minusp && ! op1_maybe_minusp)
4192               result_width = width0;
4193             break;
4194           case UDIV:
4195             if (width1 == 0)
4196               break;
4197             result_width = width0;
4198             break;
4199           case MOD:
4200             if (width1 == 0)
4201               break;
4202             if (! op0_maybe_minusp && ! op1_maybe_minusp)
4203               result_width = MIN (width0, width1);
4204             result_low = MIN (low0, low1);
4205             break;
4206           case UMOD:
4207             if (width1 == 0)
4208               break;
4209             result_width = MIN (width0, width1);
4210             result_low = MIN (low0, low1);
4211             break;
4212           default:
4213             abort ();
4214           }
4215
4216         if (result_width < mode_width)
4217           nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
4218
4219         if (result_low > 0)
4220           nonzero &= ~(((HOST_WIDE_INT) 1 << result_low) - 1);
4221
4222 #ifdef POINTERS_EXTEND_UNSIGNED
4223         /* If pointers extend unsigned and this is an addition or subtraction
4224            to a pointer in Pmode, all the bits above ptr_mode are known to be
4225            zero.  */
4226         if (POINTERS_EXTEND_UNSIGNED > 0 && GET_MODE (x) == Pmode
4227             && (code == PLUS || code == MINUS)
4228             && REG_P (XEXP (x, 0)) && REG_POINTER (XEXP (x, 0)))
4229           nonzero &= GET_MODE_MASK (ptr_mode);
4230 #endif
4231       }
4232       break;
4233
4234     case ZERO_EXTRACT:
4235       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4236           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
4237         nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
4238       break;
4239
4240     case SUBREG:
4241       /* If this is a SUBREG formed for a promoted variable that has
4242          been zero-extended, we know that at least the high-order bits
4243          are zero, though others might be too.  */
4244
4245       if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x) > 0)
4246         nonzero = GET_MODE_MASK (GET_MODE (x))
4247                   & cached_nonzero_bits (SUBREG_REG (x), GET_MODE (x),
4248                                          known_x, known_mode, known_ret);
4249
4250       /* If the inner mode is a single word for both the host and target
4251          machines, we can compute this from which bits of the inner
4252          object might be nonzero.  */
4253       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
4254           && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
4255               <= HOST_BITS_PER_WIDE_INT))
4256         {
4257           nonzero &= cached_nonzero_bits (SUBREG_REG (x), mode,
4258                                           known_x, known_mode, known_ret);
4259
4260 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
4261           /* If this is a typical RISC machine, we only have to worry
4262              about the way loads are extended.  */
4263           if ((LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
4264                ? (((nonzero
4265                     & (((unsigned HOST_WIDE_INT) 1
4266                         << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1))))
4267                    != 0))
4268                : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
4269               || !MEM_P (SUBREG_REG (x)))
4270 #endif
4271             {
4272               /* On many CISC machines, accessing an object in a wider mode
4273                  causes the high-order bits to become undefined.  So they are
4274                  not known to be zero.  */
4275               if (GET_MODE_SIZE (GET_MODE (x))
4276                   > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
4277                 nonzero |= (GET_MODE_MASK (GET_MODE (x))
4278                             & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
4279             }
4280         }
4281       break;
4282
4283     case ASHIFTRT:
4284     case LSHIFTRT:
4285     case ASHIFT:
4286     case ROTATE:
4287       /* The nonzero bits are in two classes: any bits within MODE
4288          that aren't in GET_MODE (x) are always significant.  The rest of the
4289          nonzero bits are those that are significant in the operand of
4290          the shift when shifted the appropriate number of bits.  This
4291          shows that high-order bits are cleared by the right shift and
4292          low-order bits by left shifts.  */
4293       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4294           && INTVAL (XEXP (x, 1)) >= 0
4295           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
4296         {
4297           enum machine_mode inner_mode = GET_MODE (x);
4298           unsigned int width = GET_MODE_BITSIZE (inner_mode);
4299           int count = INTVAL (XEXP (x, 1));
4300           unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
4301           unsigned HOST_WIDE_INT op_nonzero =
4302             cached_nonzero_bits (XEXP (x, 0), mode,
4303                                  known_x, known_mode, known_ret);
4304           unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
4305           unsigned HOST_WIDE_INT outer = 0;
4306
4307           if (mode_width > width)
4308             outer = (op_nonzero & nonzero & ~mode_mask);
4309
4310           if (code == LSHIFTRT)
4311             inner >>= count;
4312           else if (code == ASHIFTRT)
4313             {
4314               inner >>= count;
4315
4316               /* If the sign bit may have been nonzero before the shift, we
4317                  need to mark all the places it could have been copied to
4318                  by the shift as possibly nonzero.  */
4319               if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
4320                 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
4321             }
4322           else if (code == ASHIFT)
4323             inner <<= count;
4324           else
4325             inner = ((inner << (count % width)
4326                       | (inner >> (width - (count % width)))) & mode_mask);
4327
4328           nonzero &= (outer | inner);
4329         }
4330       break;
4331
4332     case FFS:
4333     case POPCOUNT:
4334       /* This is at most the number of bits in the mode.  */
4335       nonzero = ((HOST_WIDE_INT) 2 << (floor_log2 (mode_width))) - 1;
4336       break;
4337
4338     case CLZ:
4339       /* If CLZ has a known value at zero, then the nonzero bits are
4340          that value, plus the number of bits in the mode minus one.  */
4341       if (CLZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
4342         nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
4343       else
4344         nonzero = -1;
4345       break;
4346
4347     case CTZ:
4348       /* If CTZ has a known value at zero, then the nonzero bits are
4349          that value, plus the number of bits in the mode minus one.  */
4350       if (CTZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
4351         nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
4352       else
4353         nonzero = -1;
4354       break;
4355
4356     case PARITY:
4357       nonzero = 1;
4358       break;
4359
4360     case IF_THEN_ELSE:
4361       {
4362         unsigned HOST_WIDE_INT nonzero_true =
4363           cached_nonzero_bits (XEXP (x, 1), mode,
4364                                known_x, known_mode, known_ret);
4365
4366         /* Don't call nonzero_bits for the second time if it cannot change
4367            anything.  */
4368         if ((nonzero & nonzero_true) != nonzero)
4369           nonzero &= nonzero_true
4370                      | cached_nonzero_bits (XEXP (x, 2), mode,
4371                                             known_x, known_mode, known_ret);
4372       }
4373       break;
4374
4375     default:
4376       break;
4377     }
4378
4379   return nonzero;
4380 }
4381
4382 /* See the macro definition above.  */
4383 #undef cached_num_sign_bit_copies
4384
4385 \f
4386 /* The function cached_num_sign_bit_copies is a wrapper around
4387    num_sign_bit_copies1.  It avoids exponential behavior in
4388    num_sign_bit_copies1 when X has identical subexpressions on the
4389    first or the second level.  */
4390
4391 static unsigned int
4392 cached_num_sign_bit_copies (rtx x, enum machine_mode mode, rtx known_x,
4393                             enum machine_mode known_mode,
4394                             unsigned int known_ret)
4395 {
4396   if (x == known_x && mode == known_mode)
4397     return known_ret;
4398
4399   /* Try to find identical subexpressions.  If found call
4400      num_sign_bit_copies1 on X with the subexpressions as KNOWN_X and
4401      the precomputed value for the subexpression as KNOWN_RET.  */
4402
4403   if (ARITHMETIC_P (x))
4404     {
4405       rtx x0 = XEXP (x, 0);
4406       rtx x1 = XEXP (x, 1);
4407
4408       /* Check the first level.  */
4409       if (x0 == x1)
4410         return
4411           num_sign_bit_copies1 (x, mode, x0, mode,
4412                                 cached_num_sign_bit_copies (x0, mode, known_x,
4413                                                             known_mode,
4414                                                             known_ret));
4415
4416       /* Check the second level.  */
4417       if (ARITHMETIC_P (x0)
4418           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
4419         return
4420           num_sign_bit_copies1 (x, mode, x1, mode,
4421                                 cached_num_sign_bit_copies (x1, mode, known_x,
4422                                                             known_mode,
4423                                                             known_ret));
4424
4425       if (ARITHMETIC_P (x1)
4426           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
4427         return
4428           num_sign_bit_copies1 (x, mode, x0, mode,
4429                                 cached_num_sign_bit_copies (x0, mode, known_x,
4430                                                             known_mode,
4431                                                             known_ret));
4432     }
4433
4434   return num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret);
4435 }
4436
4437 /* Return the number of bits at the high-order end of X that are known to
4438    be equal to the sign bit.  X will be used in mode MODE; if MODE is
4439    VOIDmode, X will be used in its own mode.  The returned value  will always
4440    be between 1 and the number of bits in MODE.  */
4441
4442 static unsigned int
4443 num_sign_bit_copies1 (rtx x, enum machine_mode mode, rtx known_x,
4444                       enum machine_mode known_mode,
4445                       unsigned int known_ret)
4446 {
4447   enum rtx_code code = GET_CODE (x);
4448   unsigned int bitwidth = GET_MODE_BITSIZE (mode);
4449   int num0, num1, result;
4450   unsigned HOST_WIDE_INT nonzero;
4451
4452   /* If we weren't given a mode, use the mode of X.  If the mode is still
4453      VOIDmode, we don't know anything.  Likewise if one of the modes is
4454      floating-point.  */
4455
4456   if (mode == VOIDmode)
4457     mode = GET_MODE (x);
4458
4459   if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
4460     return 1;
4461
4462   /* For a smaller object, just ignore the high bits.  */
4463   if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
4464     {
4465       num0 = cached_num_sign_bit_copies (x, GET_MODE (x),
4466                                          known_x, known_mode, known_ret);
4467       return MAX (1,
4468                   num0 - (int) (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth));
4469     }
4470
4471   if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
4472     {
4473 #ifndef WORD_REGISTER_OPERATIONS
4474   /* If this machine does not do all register operations on the entire
4475      register and MODE is wider than the mode of X, we can say nothing
4476      at all about the high-order bits.  */
4477       return 1;
4478 #else
4479       /* Likewise on machines that do, if the mode of the object is smaller
4480          than a word and loads of that size don't sign extend, we can say
4481          nothing about the high order bits.  */
4482       if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
4483 #ifdef LOAD_EXTEND_OP
4484           && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
4485 #endif
4486           )
4487         return 1;
4488 #endif
4489     }
4490
4491   switch (code)
4492     {
4493     case REG:
4494
4495 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
4496       /* If pointers extend signed and this is a pointer in Pmode, say that
4497          all the bits above ptr_mode are known to be sign bit copies.  */
4498       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
4499           && REG_POINTER (x))
4500         return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
4501 #endif
4502
4503       {
4504         unsigned int copies_for_hook = 1, copies = 1;
4505         rtx new = rtl_hooks.reg_num_sign_bit_copies (x, mode, known_x,
4506                                                      known_mode, known_ret,
4507                                                      &copies_for_hook);
4508
4509         if (new)
4510           copies = cached_num_sign_bit_copies (new, mode, known_x,
4511                                                known_mode, known_ret);
4512
4513         if (copies > 1 || copies_for_hook > 1)
4514           return MAX (copies, copies_for_hook);
4515
4516         /* Else, use nonzero_bits to guess num_sign_bit_copies (see below).  */
4517       }
4518       break;
4519
4520     case MEM:
4521 #ifdef LOAD_EXTEND_OP
4522       /* Some RISC machines sign-extend all loads of smaller than a word.  */
4523       if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
4524         return MAX (1, ((int) bitwidth
4525                         - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1));
4526 #endif
4527       break;
4528
4529     case CONST_INT:
4530       /* If the constant is negative, take its 1's complement and remask.
4531          Then see how many zero bits we have.  */
4532       nonzero = INTVAL (x) & GET_MODE_MASK (mode);
4533       if (bitwidth <= HOST_BITS_PER_WIDE_INT
4534           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
4535         nonzero = (~nonzero) & GET_MODE_MASK (mode);
4536
4537       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
4538
4539     case SUBREG:
4540       /* If this is a SUBREG for a promoted object that is sign-extended
4541          and we are looking at it in a wider mode, we know that at least the
4542          high-order bits are known to be sign bit copies.  */
4543
4544       if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
4545         {
4546           num0 = cached_num_sign_bit_copies (SUBREG_REG (x), mode,
4547                                              known_x, known_mode, known_ret);
4548           return MAX ((int) bitwidth
4549                       - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1,
4550                       num0);
4551         }
4552
4553       /* For a smaller object, just ignore the high bits.  */
4554       if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
4555         {
4556           num0 = cached_num_sign_bit_copies (SUBREG_REG (x), VOIDmode,
4557                                              known_x, known_mode, known_ret);
4558           return MAX (1, (num0
4559                           - (int) (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
4560                                    - bitwidth)));
4561         }
4562
4563 #ifdef WORD_REGISTER_OPERATIONS
4564 #ifdef LOAD_EXTEND_OP
4565       /* For paradoxical SUBREGs on machines where all register operations
4566          affect the entire register, just look inside.  Note that we are
4567          passing MODE to the recursive call, so the number of sign bit copies
4568          will remain relative to that mode, not the inner mode.  */
4569
4570       /* This works only if loads sign extend.  Otherwise, if we get a
4571          reload for the inner part, it may be loaded from the stack, and
4572          then we lose all sign bit copies that existed before the store
4573          to the stack.  */
4574
4575       if ((GET_MODE_SIZE (GET_MODE (x))
4576            > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
4577           && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
4578           && MEM_P (SUBREG_REG (x)))
4579         return cached_num_sign_bit_copies (SUBREG_REG (x), mode,
4580                                            known_x, known_mode, known_ret);
4581 #endif
4582 #endif
4583       break;
4584
4585     case SIGN_EXTRACT:
4586       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4587         return MAX (1, (int) bitwidth - INTVAL (XEXP (x, 1)));
4588       break;
4589
4590     case SIGN_EXTEND:
4591       return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4592               + cached_num_sign_bit_copies (XEXP (x, 0), VOIDmode,
4593                                             known_x, known_mode, known_ret));
4594
4595     case TRUNCATE:
4596       /* For a smaller object, just ignore the high bits.  */
4597       num0 = cached_num_sign_bit_copies (XEXP (x, 0), VOIDmode,
4598                                          known_x, known_mode, known_ret);
4599       return MAX (1, (num0 - (int) (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4600                                     - bitwidth)));
4601
4602     case NOT:
4603       return cached_num_sign_bit_copies (XEXP (x, 0), mode,
4604                                          known_x, known_mode, known_ret);
4605
4606     case ROTATE:       case ROTATERT:
4607       /* If we are rotating left by a number of bits less than the number
4608          of sign bit copies, we can just subtract that amount from the
4609          number.  */
4610       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4611           && INTVAL (XEXP (x, 1)) >= 0
4612           && INTVAL (XEXP (x, 1)) < (int) bitwidth)
4613         {
4614           num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4615                                              known_x, known_mode, known_ret);
4616           return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
4617                                  : (int) bitwidth - INTVAL (XEXP (x, 1))));
4618         }
4619       break;
4620
4621     case NEG:
4622       /* In general, this subtracts one sign bit copy.  But if the value
4623          is known to be positive, the number of sign bit copies is the
4624          same as that of the input.  Finally, if the input has just one bit
4625          that might be nonzero, all the bits are copies of the sign bit.  */
4626       num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4627                                          known_x, known_mode, known_ret);
4628       if (bitwidth > HOST_BITS_PER_WIDE_INT)
4629         return num0 > 1 ? num0 - 1 : 1;
4630
4631       nonzero = nonzero_bits (XEXP (x, 0), mode);
4632       if (nonzero == 1)
4633         return bitwidth;
4634
4635       if (num0 > 1
4636           && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
4637         num0--;
4638
4639       return num0;
4640
4641     case IOR:   case AND:   case XOR:
4642     case SMIN:  case SMAX:  case UMIN:  case UMAX:
4643       /* Logical operations will preserve the number of sign-bit copies.
4644          MIN and MAX operations always return one of the operands.  */
4645       num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4646                                          known_x, known_mode, known_ret);
4647       num1 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4648                                          known_x, known_mode, known_ret);
4649       return MIN (num0, num1);
4650
4651     case PLUS:  case MINUS:
4652       /* For addition and subtraction, we can have a 1-bit carry.  However,
4653          if we are subtracting 1 from a positive number, there will not
4654          be such a carry.  Furthermore, if the positive number is known to
4655          be 0 or 1, we know the result is either -1 or 0.  */
4656
4657       if (code == PLUS && XEXP (x, 1) == constm1_rtx
4658           && bitwidth <= HOST_BITS_PER_WIDE_INT)
4659         {
4660           nonzero = nonzero_bits (XEXP (x, 0), mode);
4661           if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
4662             return (nonzero == 1 || nonzero == 0 ? bitwidth
4663                     : bitwidth - floor_log2 (nonzero) - 1);
4664         }
4665
4666       num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4667                                          known_x, known_mode, known_ret);
4668       num1 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4669                                          known_x, known_mode, known_ret);
4670       result = MAX (1, MIN (num0, num1) - 1);
4671
4672 #ifdef POINTERS_EXTEND_UNSIGNED
4673       /* If pointers extend signed and this is an addition or subtraction
4674          to a pointer in Pmode, all the bits above ptr_mode are known to be
4675          sign bit copies.  */
4676       if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
4677           && (code == PLUS || code == MINUS)
4678           && REG_P (XEXP (x, 0)) && REG_POINTER (XEXP (x, 0)))
4679         result = MAX ((int) (GET_MODE_BITSIZE (Pmode)
4680                              - GET_MODE_BITSIZE (ptr_mode) + 1),
4681                       result);
4682 #endif
4683       return result;
4684
4685     case MULT:
4686       /* The number of bits of the product is the sum of the number of
4687          bits of both terms.  However, unless one of the terms if known
4688          to be positive, we must allow for an additional bit since negating
4689          a negative number can remove one sign bit copy.  */
4690
4691       num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4692                                          known_x, known_mode, known_ret);
4693       num1 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4694                                          known_x, known_mode, known_ret);
4695
4696       result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
4697       if (result > 0
4698           && (bitwidth > HOST_BITS_PER_WIDE_INT
4699               || (((nonzero_bits (XEXP (x, 0), mode)
4700                     & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
4701                   && ((nonzero_bits (XEXP (x, 1), mode)
4702                        & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
4703         result--;
4704
4705       return MAX (1, result);
4706
4707     case UDIV:
4708       /* The result must be <= the first operand.  If the first operand
4709          has the high bit set, we know nothing about the number of sign
4710          bit copies.  */
4711       if (bitwidth > HOST_BITS_PER_WIDE_INT)
4712         return 1;
4713       else if ((nonzero_bits (XEXP (x, 0), mode)
4714                 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
4715         return 1;
4716       else
4717         return cached_num_sign_bit_copies (XEXP (x, 0), mode,
4718                                            known_x, known_mode, known_ret);
4719
4720     case UMOD:
4721       /* The result must be <= the second operand.  */
4722       return cached_num_sign_bit_copies (XEXP (x, 1), mode,
4723                                            known_x, known_mode, known_ret);
4724
4725     case DIV:
4726       /* Similar to unsigned division, except that we have to worry about
4727          the case where the divisor is negative, in which case we have
4728          to add 1.  */
4729       result = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4730                                            known_x, known_mode, known_ret);
4731       if (result > 1
4732           && (bitwidth > HOST_BITS_PER_WIDE_INT
4733               || (nonzero_bits (XEXP (x, 1), mode)
4734                   & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
4735         result--;
4736
4737       return result;
4738
4739     case MOD:
4740       result = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4741                                            known_x, known_mode, known_ret);
4742       if (result > 1
4743           && (bitwidth > HOST_BITS_PER_WIDE_INT
4744               || (nonzero_bits (XEXP (x, 1), mode)
4745                   & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
4746         result--;
4747
4748       return result;
4749
4750     case ASHIFTRT:
4751       /* Shifts by a constant add to the number of bits equal to the
4752          sign bit.  */
4753       num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4754                                          known_x, known_mode, known_ret);
4755       if (GET_CODE (XEXP (x, 1)) == CONST_INT
4756           && INTVAL (XEXP (x, 1)) > 0)
4757         num0 = MIN ((int) bitwidth, num0 + INTVAL (XEXP (x, 1)));
4758
4759       return num0;
4760
4761     case ASHIFT:
4762       /* Left shifts destroy copies.  */
4763       if (GET_CODE (XEXP (x, 1)) != CONST_INT
4764           || INTVAL (XEXP (x, 1)) < 0
4765           || INTVAL (XEXP (x, 1)) >= (int) bitwidth)
4766         return 1;
4767
4768       num0 = cached_num_sign_bit_copies (XEXP (x, 0), mode,
4769                                          known_x, known_mode, known_ret);
4770       return MAX (1, num0 - INTVAL (XEXP (x, 1)));
4771
4772     case IF_THEN_ELSE:
4773       num0 = cached_num_sign_bit_copies (XEXP (x, 1), mode,
4774                                          known_x, known_mode, known_ret);
4775       num1 = cached_num_sign_bit_copies (XEXP (x, 2), mode,
4776                                          known_x, known_mode, known_ret);
4777       return MIN (num0, num1);
4778
4779     case EQ:  case NE:  case GE:  case GT:  case LE:  case LT:
4780     case UNEQ:  case LTGT:  case UNGE:  case UNGT:  case UNLE:  case UNLT:
4781     case GEU: case GTU: case LEU: case LTU:
4782     case UNORDERED: case ORDERED:
4783       /* If the constant is negative, take its 1's complement and remask.
4784          Then see how many zero bits we have.  */
4785       nonzero = STORE_FLAG_VALUE;
4786       if (bitwidth <= HOST_BITS_PER_WIDE_INT
4787           && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
4788         nonzero = (~nonzero) & GET_MODE_MASK (mode);
4789
4790       return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
4791
4792     default:
4793       break;
4794     }
4795
4796   /* If we haven't been able to figure it out by one of the above rules,
4797      see if some of the high-order bits are known to be zero.  If so,
4798      count those bits and return one less than that amount.  If we can't
4799      safely compute the mask for this mode, always return BITWIDTH.  */
4800
4801   bitwidth = GET_MODE_BITSIZE (mode);
4802   if (bitwidth > HOST_BITS_PER_WIDE_INT)
4803     return 1;
4804
4805   nonzero = nonzero_bits (x, mode);
4806   return nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
4807          ? 1 : bitwidth - floor_log2 (nonzero) - 1;
4808 }