OSDN Git Service

* sparc.c (output_restore_regs): Prototype.
[pf3gnuchains/gcc-fork.git] / gcc / simplify-rtx.c
1 /* Common subexpression elimination for GNU compiler.
2    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 #include "config.h"
24 /* stdio.h must precede rtl.h for FFS.  */
25 #include "system.h"
26 #include <setjmp.h>
27
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "flags.h"
33 #include "real.h"
34 #include "insn-config.h"
35 #include "recog.h"
36 #include "function.h"
37 #include "expr.h"
38 #include "toplev.h"
39 #include "output.h"
40 #include "ggc.h"
41 #include "obstack.h"
42 #include "hashtab.h"
43 #include "cselib.h"
44
45 /* Simplification and canonicalization of RTL.  */
46
47 /* Nonzero if X has the form (PLUS frame-pointer integer).  We check for
48    virtual regs here because the simplify_*_operation routines are called
49    by integrate.c, which is called before virtual register instantiation.
50
51    ?!? FIXED_BASE_PLUS_P and NONZERO_BASE_PLUS_P need to move into 
52    a header file so that their definitions can be shared with the
53    simplification routines in simplify-rtx.c.  Until then, do not
54    change these macros without also changing the copy in simplify-rtx.c.  */
55
56 #define FIXED_BASE_PLUS_P(X)                                    \
57   ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx    \
58    || ((X) == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])\
59    || (X) == virtual_stack_vars_rtx                             \
60    || (X) == virtual_incoming_args_rtx                          \
61    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
62        && (XEXP (X, 0) == frame_pointer_rtx                     \
63            || XEXP (X, 0) == hard_frame_pointer_rtx             \
64            || ((X) == arg_pointer_rtx                           \
65                && fixed_regs[ARG_POINTER_REGNUM])               \
66            || XEXP (X, 0) == virtual_stack_vars_rtx             \
67            || XEXP (X, 0) == virtual_incoming_args_rtx))        \
68    || GET_CODE (X) == ADDRESSOF)
69
70 /* Similar, but also allows reference to the stack pointer.
71
72    This used to include FIXED_BASE_PLUS_P, however, we can't assume that
73    arg_pointer_rtx by itself is nonzero, because on at least one machine,
74    the i960, the arg pointer is zero when it is unused.  */
75
76 #define NONZERO_BASE_PLUS_P(X)                                  \
77   ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx    \
78    || (X) == virtual_stack_vars_rtx                             \
79    || (X) == virtual_incoming_args_rtx                          \
80    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
81        && (XEXP (X, 0) == frame_pointer_rtx                     \
82            || XEXP (X, 0) == hard_frame_pointer_rtx             \
83            || ((X) == arg_pointer_rtx                           \
84                && fixed_regs[ARG_POINTER_REGNUM])               \
85            || XEXP (X, 0) == virtual_stack_vars_rtx             \
86            || XEXP (X, 0) == virtual_incoming_args_rtx))        \
87    || (X) == stack_pointer_rtx                                  \
88    || (X) == virtual_stack_dynamic_rtx                          \
89    || (X) == virtual_outgoing_args_rtx                          \
90    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
91        && (XEXP (X, 0) == stack_pointer_rtx                     \
92            || XEXP (X, 0) == virtual_stack_dynamic_rtx          \
93            || XEXP (X, 0) == virtual_outgoing_args_rtx))        \
94    || GET_CODE (X) == ADDRESSOF)
95
96
97 static rtx simplify_plus_minus  PARAMS ((enum rtx_code, enum machine_mode,
98                                        rtx, rtx));
99 static void check_fold_consts   PARAMS ((PTR));
100
101 /* Make a binary operation by properly ordering the operands and 
102    seeing if the expression folds.  */
103
104 rtx
105 simplify_gen_binary (code, mode, op0, op1)
106      enum rtx_code code;
107      enum machine_mode mode;
108      rtx op0, op1;
109 {
110   rtx tem;
111
112   /* Put complex operands first and constants second if commutative.  */
113   if (GET_RTX_CLASS (code) == 'c'
114       && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
115           || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
116               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
117           || (GET_CODE (op0) == SUBREG
118               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
119               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
120     tem = op0, op0 = op1, op1 = tem;
121
122   /* If this simplifies, do it.  */
123   tem = simplify_binary_operation (code, mode, op0, op1);
124
125   if (tem)
126     return tem;
127
128   /* Handle addition and subtraction of CONST_INT specially.  Otherwise,
129      just form the operation.  */
130
131   if (code == PLUS && GET_CODE (op1) == CONST_INT
132       && GET_MODE (op0) != VOIDmode)
133     return plus_constant (op0, INTVAL (op1));
134   else if (code == MINUS && GET_CODE (op1) == CONST_INT
135            && GET_MODE (op0) != VOIDmode)
136     return plus_constant (op0, - INTVAL (op1));
137   else
138     return gen_rtx_fmt_ee (code, mode, op0, op1);
139 }
140 \f
141 /* Try to simplify a unary operation CODE whose output mode is to be
142    MODE with input operand OP whose mode was originally OP_MODE.
143    Return zero if no simplification can be made.  */
144
145 rtx
146 simplify_unary_operation (code, mode, op, op_mode)
147      enum rtx_code code;
148      enum machine_mode mode;
149      rtx op;
150      enum machine_mode op_mode;
151 {
152   unsigned int width = GET_MODE_BITSIZE (mode);
153
154   /* The order of these tests is critical so that, for example, we don't
155      check the wrong mode (input vs. output) for a conversion operation,
156      such as FIX.  At some point, this should be simplified.  */
157
158 #if !defined(REAL_IS_NOT_DOUBLE) || defined(REAL_ARITHMETIC)
159
160   if (code == FLOAT && GET_MODE (op) == VOIDmode
161       && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
162     {
163       HOST_WIDE_INT hv, lv;
164       REAL_VALUE_TYPE d;
165
166       if (GET_CODE (op) == CONST_INT)
167         lv = INTVAL (op), hv = INTVAL (op) < 0 ? -1 : 0;
168       else
169         lv = CONST_DOUBLE_LOW (op),  hv = CONST_DOUBLE_HIGH (op);
170
171 #ifdef REAL_ARITHMETIC
172       REAL_VALUE_FROM_INT (d, lv, hv, mode);
173 #else
174       if (hv < 0)
175         {
176           d = (double) (~ hv);
177           d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
178                 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
179           d += (double) (unsigned HOST_WIDE_INT) (~ lv);
180           d = (- d - 1.0);
181         }
182       else
183         {
184           d = (double) hv;
185           d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
186                 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
187           d += (double) (unsigned HOST_WIDE_INT) lv;
188         }
189 #endif  /* REAL_ARITHMETIC */
190       d = real_value_truncate (mode, d);
191       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
192     }
193   else if (code == UNSIGNED_FLOAT && GET_MODE (op) == VOIDmode
194            && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
195     {
196       HOST_WIDE_INT hv, lv;
197       REAL_VALUE_TYPE d;
198
199       if (GET_CODE (op) == CONST_INT)
200         lv = INTVAL (op), hv = INTVAL (op) < 0 ? -1 : 0;
201       else
202         lv = CONST_DOUBLE_LOW (op),  hv = CONST_DOUBLE_HIGH (op);
203
204       if (op_mode == VOIDmode)
205         {
206           /* We don't know how to interpret negative-looking numbers in
207              this case, so don't try to fold those.  */
208           if (hv < 0)
209             return 0;
210         }
211       else if (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT * 2)
212         ;
213       else
214         hv = 0, lv &= GET_MODE_MASK (op_mode);
215
216 #ifdef REAL_ARITHMETIC
217       REAL_VALUE_FROM_UNSIGNED_INT (d, lv, hv, mode);
218 #else
219
220       d = (double) (unsigned HOST_WIDE_INT) hv;
221       d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
222             * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
223       d += (double) (unsigned HOST_WIDE_INT) lv;
224 #endif  /* REAL_ARITHMETIC */
225       d = real_value_truncate (mode, d);
226       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
227     }
228 #endif
229
230   if (GET_CODE (op) == CONST_INT
231       && width <= HOST_BITS_PER_WIDE_INT && width > 0)
232     {
233       register HOST_WIDE_INT arg0 = INTVAL (op);
234       register HOST_WIDE_INT val;
235
236       switch (code)
237         {
238         case NOT:
239           val = ~ arg0;
240           break;
241
242         case NEG:
243           val = - arg0;
244           break;
245
246         case ABS:
247           val = (arg0 >= 0 ? arg0 : - arg0);
248           break;
249
250         case FFS:
251           /* Don't use ffs here.  Instead, get low order bit and then its
252              number.  If arg0 is zero, this will return 0, as desired.  */
253           arg0 &= GET_MODE_MASK (mode);
254           val = exact_log2 (arg0 & (- arg0)) + 1;
255           break;
256
257         case TRUNCATE:
258           val = arg0;
259           break;
260
261         case ZERO_EXTEND:
262           if (op_mode == VOIDmode)
263             op_mode = mode;
264           if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
265             {
266               /* If we were really extending the mode,
267                  we would have to distinguish between zero-extension
268                  and sign-extension.  */
269               if (width != GET_MODE_BITSIZE (op_mode))
270                 abort ();
271               val = arg0;
272             }
273           else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
274             val = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
275           else
276             return 0;
277           break;
278
279         case SIGN_EXTEND:
280           if (op_mode == VOIDmode)
281             op_mode = mode;
282           if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
283             {
284               /* If we were really extending the mode,
285                  we would have to distinguish between zero-extension
286                  and sign-extension.  */
287               if (width != GET_MODE_BITSIZE (op_mode))
288                 abort ();
289               val = arg0;
290             }
291           else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
292             {
293               val
294                 = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
295               if (val
296                   & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (op_mode) - 1)))
297                 val -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
298             }
299           else
300             return 0;
301           break;
302
303         case SQRT:
304           return 0;
305
306         default:
307           abort ();
308         }
309
310       val = trunc_int_for_mode (val, mode);
311
312       return GEN_INT (val);
313     }
314
315   /* We can do some operations on integer CONST_DOUBLEs.  Also allow
316      for a DImode operation on a CONST_INT.  */
317   else if (GET_MODE (op) == VOIDmode && width <= HOST_BITS_PER_INT * 2
318            && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
319     {
320       HOST_WIDE_INT l1, h1, lv, hv;
321
322       if (GET_CODE (op) == CONST_DOUBLE)
323         l1 = CONST_DOUBLE_LOW (op), h1 = CONST_DOUBLE_HIGH (op);
324       else
325         l1 = INTVAL (op), h1 = l1 < 0 ? -1 : 0;
326
327       switch (code)
328         {
329         case NOT:
330           lv = ~ l1;
331           hv = ~ h1;
332           break;
333
334         case NEG:
335           neg_double (l1, h1, &lv, &hv);
336           break;
337
338         case ABS:
339           if (h1 < 0)
340             neg_double (l1, h1, &lv, &hv);
341           else
342             lv = l1, hv = h1;
343           break;
344
345         case FFS:
346           hv = 0;
347           if (l1 == 0)
348             lv = HOST_BITS_PER_WIDE_INT + exact_log2 (h1 & (-h1)) + 1;
349           else
350             lv = exact_log2 (l1 & (-l1)) + 1;
351           break;
352
353         case TRUNCATE:
354           /* This is just a change-of-mode, so do nothing.  */
355           lv = l1, hv = h1;
356           break;
357
358         case ZERO_EXTEND:
359           if (op_mode == VOIDmode
360               || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
361             return 0;
362
363           hv = 0;
364           lv = l1 & GET_MODE_MASK (op_mode);
365           break;
366
367         case SIGN_EXTEND:
368           if (op_mode == VOIDmode
369               || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
370             return 0;
371           else
372             {
373               lv = l1 & GET_MODE_MASK (op_mode);
374               if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
375                   && (lv & ((HOST_WIDE_INT) 1
376                             << (GET_MODE_BITSIZE (op_mode) - 1))) != 0)
377                 lv -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
378
379               hv = (lv < 0) ? ~ (HOST_WIDE_INT) 0 : 0;
380             }
381           break;
382
383         case SQRT:
384           return 0;
385
386         default:
387           return 0;
388         }
389
390       return immed_double_const (lv, hv, mode);
391     }
392
393 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
394   else if (GET_CODE (op) == CONST_DOUBLE
395            && GET_MODE_CLASS (mode) == MODE_FLOAT)
396     {
397       REAL_VALUE_TYPE d;
398       jmp_buf handler;
399       rtx x;
400
401       if (setjmp (handler))
402         /* There used to be a warning here, but that is inadvisable.
403            People may want to cause traps, and the natural way
404            to do it should not get a warning.  */
405         return 0;
406
407       set_float_handler (handler);
408
409       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
410
411       switch (code)
412         {
413         case NEG:
414           d = REAL_VALUE_NEGATE (d);
415           break;
416
417         case ABS:
418           if (REAL_VALUE_NEGATIVE (d))
419             d = REAL_VALUE_NEGATE (d);
420           break;
421
422         case FLOAT_TRUNCATE:
423           d = real_value_truncate (mode, d);
424           break;
425
426         case FLOAT_EXTEND:
427           /* All this does is change the mode.  */
428           break;
429
430         case FIX:
431           d = REAL_VALUE_RNDZINT (d);
432           break;
433
434         case UNSIGNED_FIX:
435           d = REAL_VALUE_UNSIGNED_RNDZINT (d);
436           break;
437
438         case SQRT:
439           return 0;
440
441         default:
442           abort ();
443         }
444
445       x = CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
446       set_float_handler (NULL_PTR);
447       return x;
448     }
449
450   else if (GET_CODE (op) == CONST_DOUBLE
451            && GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT
452            && GET_MODE_CLASS (mode) == MODE_INT
453            && width <= HOST_BITS_PER_WIDE_INT && width > 0)
454     {
455       REAL_VALUE_TYPE d;
456       jmp_buf handler;
457       HOST_WIDE_INT val;
458
459       if (setjmp (handler))
460         return 0;
461
462       set_float_handler (handler);
463
464       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
465
466       switch (code)
467         {
468         case FIX:
469           val = REAL_VALUE_FIX (d);
470           break;
471
472         case UNSIGNED_FIX:
473           val = REAL_VALUE_UNSIGNED_FIX (d);
474           break;
475
476         default:
477           abort ();
478         }
479
480       set_float_handler (NULL_PTR);
481
482       val = trunc_int_for_mode (val, mode);
483
484       return GEN_INT (val);
485     }
486 #endif
487   /* This was formerly used only for non-IEEE float.
488      eggert@twinsun.com says it is safe for IEEE also.  */
489   else
490     {
491       /* There are some simplifications we can do even if the operands
492          aren't constant.  */
493       switch (code)
494         {
495         case NEG:
496         case NOT:
497           /* (not (not X)) == X, similarly for NEG.  */
498           if (GET_CODE (op) == code)
499             return XEXP (op, 0);
500           break;
501
502         case SIGN_EXTEND:
503           /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
504              becomes just the MINUS if its mode is MODE.  This allows
505              folding switch statements on machines using casesi (such as
506              the Vax).  */
507           if (GET_CODE (op) == TRUNCATE
508               && GET_MODE (XEXP (op, 0)) == mode
509               && GET_CODE (XEXP (op, 0)) == MINUS
510               && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
511               && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
512             return XEXP (op, 0);
513
514 #ifdef POINTERS_EXTEND_UNSIGNED
515           if (! POINTERS_EXTEND_UNSIGNED
516               && mode == Pmode && GET_MODE (op) == ptr_mode
517               && CONSTANT_P (op))
518             return convert_memory_address (Pmode, op);
519 #endif
520           break;
521
522 #ifdef POINTERS_EXTEND_UNSIGNED
523         case ZERO_EXTEND:
524           if (POINTERS_EXTEND_UNSIGNED
525               && mode == Pmode && GET_MODE (op) == ptr_mode
526               && CONSTANT_P (op))
527             return convert_memory_address (Pmode, op);
528           break;
529 #endif
530           
531         default:
532           break;
533         }
534
535       return 0;
536     }
537 }
538 \f
539 /* Simplify a binary operation CODE with result mode MODE, operating on OP0
540    and OP1.  Return 0 if no simplification is possible.
541
542    Don't use this for relational operations such as EQ or LT.
543    Use simplify_relational_operation instead.  */
544
545 rtx
546 simplify_binary_operation (code, mode, op0, op1)
547      enum rtx_code code;
548      enum machine_mode mode;
549      rtx op0, op1;
550 {
551   register HOST_WIDE_INT arg0, arg1, arg0s, arg1s;
552   HOST_WIDE_INT val;
553   unsigned int width = GET_MODE_BITSIZE (mode);
554   rtx tem;
555
556   /* Relational operations don't work here.  We must know the mode
557      of the operands in order to do the comparison correctly.
558      Assuming a full word can give incorrect results.
559      Consider comparing 128 with -128 in QImode.  */
560
561   if (GET_RTX_CLASS (code) == '<')
562     abort ();
563
564 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
565   if (GET_MODE_CLASS (mode) == MODE_FLOAT
566       && GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE
567       && mode == GET_MODE (op0) && mode == GET_MODE (op1))
568     {
569       REAL_VALUE_TYPE f0, f1, value;
570       jmp_buf handler;
571
572       if (setjmp (handler))
573         return 0;
574
575       set_float_handler (handler);
576
577       REAL_VALUE_FROM_CONST_DOUBLE (f0, op0);
578       REAL_VALUE_FROM_CONST_DOUBLE (f1, op1);
579       f0 = real_value_truncate (mode, f0);
580       f1 = real_value_truncate (mode, f1);
581
582 #ifdef REAL_ARITHMETIC
583 #ifndef REAL_INFINITY
584       if (code == DIV && REAL_VALUES_EQUAL (f1, dconst0))
585         return 0;
586 #endif
587       REAL_ARITHMETIC (value, rtx_to_tree_code (code), f0, f1);
588 #else
589       switch (code)
590         {
591         case PLUS:
592           value = f0 + f1;
593           break;
594         case MINUS:
595           value = f0 - f1;
596           break;
597         case MULT:
598           value = f0 * f1;
599           break;
600         case DIV:
601 #ifndef REAL_INFINITY
602           if (f1 == 0)
603             return 0;
604 #endif
605           value = f0 / f1;
606           break;
607         case SMIN:
608           value = MIN (f0, f1);
609           break;
610         case SMAX:
611           value = MAX (f0, f1);
612           break;
613         default:
614           abort ();
615         }
616 #endif
617
618       value = real_value_truncate (mode, value);
619       set_float_handler (NULL_PTR);
620       return CONST_DOUBLE_FROM_REAL_VALUE (value, mode);
621     }
622 #endif  /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
623
624   /* We can fold some multi-word operations.  */
625   if (GET_MODE_CLASS (mode) == MODE_INT
626       && width == HOST_BITS_PER_WIDE_INT * 2
627       && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT)
628       && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT))
629     {
630       HOST_WIDE_INT l1, l2, h1, h2, lv, hv;
631
632       if (GET_CODE (op0) == CONST_DOUBLE)
633         l1 = CONST_DOUBLE_LOW (op0), h1 = CONST_DOUBLE_HIGH (op0);
634       else
635         l1 = INTVAL (op0), h1 = l1 < 0 ? -1 : 0;
636
637       if (GET_CODE (op1) == CONST_DOUBLE)
638         l2 = CONST_DOUBLE_LOW (op1), h2 = CONST_DOUBLE_HIGH (op1);
639       else
640         l2 = INTVAL (op1), h2 = l2 < 0 ? -1 : 0;
641
642       switch (code)
643         {
644         case MINUS:
645           /* A - B == A + (-B).  */
646           neg_double (l2, h2, &lv, &hv);
647           l2 = lv, h2 = hv;
648
649           /* .. fall through ...  */
650
651         case PLUS:
652           add_double (l1, h1, l2, h2, &lv, &hv);
653           break;
654
655         case MULT:
656           mul_double (l1, h1, l2, h2, &lv, &hv);
657           break;
658
659         case DIV:  case MOD:   case UDIV:  case UMOD:
660           /* We'd need to include tree.h to do this and it doesn't seem worth
661              it.  */
662           return 0;
663
664         case AND:
665           lv = l1 & l2, hv = h1 & h2;
666           break;
667
668         case IOR:
669           lv = l1 | l2, hv = h1 | h2;
670           break;
671
672         case XOR:
673           lv = l1 ^ l2, hv = h1 ^ h2;
674           break;
675
676         case SMIN:
677           if (h1 < h2
678               || (h1 == h2
679                   && ((unsigned HOST_WIDE_INT) l1
680                       < (unsigned HOST_WIDE_INT) l2)))
681             lv = l1, hv = h1;
682           else
683             lv = l2, hv = h2;
684           break;
685
686         case SMAX:
687           if (h1 > h2
688               || (h1 == h2
689                   && ((unsigned HOST_WIDE_INT) l1
690                       > (unsigned HOST_WIDE_INT) l2)))
691             lv = l1, hv = h1;
692           else
693             lv = l2, hv = h2;
694           break;
695
696         case UMIN:
697           if ((unsigned HOST_WIDE_INT) h1 < (unsigned HOST_WIDE_INT) h2
698               || (h1 == h2
699                   && ((unsigned HOST_WIDE_INT) l1
700                       < (unsigned HOST_WIDE_INT) l2)))
701             lv = l1, hv = h1;
702           else
703             lv = l2, hv = h2;
704           break;
705
706         case UMAX:
707           if ((unsigned HOST_WIDE_INT) h1 > (unsigned HOST_WIDE_INT) h2
708               || (h1 == h2
709                   && ((unsigned HOST_WIDE_INT) l1
710                       > (unsigned HOST_WIDE_INT) l2)))
711             lv = l1, hv = h1;
712           else
713             lv = l2, hv = h2;
714           break;
715
716         case LSHIFTRT:   case ASHIFTRT:
717         case ASHIFT:
718         case ROTATE:     case ROTATERT:
719 #ifdef SHIFT_COUNT_TRUNCATED
720           if (SHIFT_COUNT_TRUNCATED)
721             l2 &= (GET_MODE_BITSIZE (mode) - 1), h2 = 0;
722 #endif
723
724           if (h2 != 0 || l2 < 0 || l2 >= GET_MODE_BITSIZE (mode))
725             return 0;
726
727           if (code == LSHIFTRT || code == ASHIFTRT)
728             rshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv,
729                            code == ASHIFTRT);
730           else if (code == ASHIFT)
731             lshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv, 1);
732           else if (code == ROTATE)
733             lrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
734           else /* code == ROTATERT */
735             rrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
736           break;
737
738         default:
739           return 0;
740         }
741
742       return immed_double_const (lv, hv, mode);
743     }
744
745   if (GET_CODE (op0) != CONST_INT || GET_CODE (op1) != CONST_INT
746       || width > HOST_BITS_PER_WIDE_INT || width == 0)
747     {
748       /* Even if we can't compute a constant result,
749          there are some cases worth simplifying.  */
750
751       switch (code)
752         {
753         case PLUS:
754           /* In IEEE floating point, x+0 is not the same as x.  Similarly
755              for the other optimizations below.  */
756           if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
757               && FLOAT_MODE_P (mode) && ! flag_fast_math)
758             break;
759
760           if (op1 == CONST0_RTX (mode))
761             return op0;
762
763           /* ((-a) + b) -> (b - a) and similarly for (a + (-b)) */
764           if (GET_CODE (op0) == NEG)
765             return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
766           else if (GET_CODE (op1) == NEG)
767             return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
768
769           /* Handle both-operands-constant cases.  We can only add
770              CONST_INTs to constants since the sum of relocatable symbols
771              can't be handled by most assemblers.  Don't add CONST_INT
772              to CONST_INT since overflow won't be computed properly if wider
773              than HOST_BITS_PER_WIDE_INT.  */
774
775           if (CONSTANT_P (op0) && GET_MODE (op0) != VOIDmode
776               && GET_CODE (op1) == CONST_INT)
777             return plus_constant (op0, INTVAL (op1));
778           else if (CONSTANT_P (op1) && GET_MODE (op1) != VOIDmode
779                    && GET_CODE (op0) == CONST_INT)
780             return plus_constant (op1, INTVAL (op0));
781
782           /* See if this is something like X * C - X or vice versa or
783              if the multiplication is written as a shift.  If so, we can
784              distribute and make a new multiply, shift, or maybe just
785              have X (if C is 2 in the example above).  But don't make
786              real multiply if we didn't have one before.  */
787
788           if (! FLOAT_MODE_P (mode))
789             {
790               HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
791               rtx lhs = op0, rhs = op1;
792               int had_mult = 0;
793
794               if (GET_CODE (lhs) == NEG)
795                 coeff0 = -1, lhs = XEXP (lhs, 0);
796               else if (GET_CODE (lhs) == MULT
797                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
798                 {
799                   coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
800                   had_mult = 1;
801                 }
802               else if (GET_CODE (lhs) == ASHIFT
803                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT
804                        && INTVAL (XEXP (lhs, 1)) >= 0
805                        && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
806                 {
807                   coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
808                   lhs = XEXP (lhs, 0);
809                 }
810
811               if (GET_CODE (rhs) == NEG)
812                 coeff1 = -1, rhs = XEXP (rhs, 0);
813               else if (GET_CODE (rhs) == MULT
814                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
815                 {
816                   coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
817                   had_mult = 1;
818                 }
819               else if (GET_CODE (rhs) == ASHIFT
820                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT
821                        && INTVAL (XEXP (rhs, 1)) >= 0
822                        && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
823                 {
824                   coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
825                   rhs = XEXP (rhs, 0);
826                 }
827
828               if (rtx_equal_p (lhs, rhs))
829                 {
830                   tem = simplify_gen_binary (MULT, mode, lhs,
831                                         GEN_INT (coeff0 + coeff1));
832                   return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
833                 }
834             }
835
836           /* If one of the operands is a PLUS or a MINUS, see if we can
837              simplify this by the associative law. 
838              Don't use the associative law for floating point.
839              The inaccuracy makes it nonassociative,
840              and subtle programs can break if operations are associated.  */
841
842           if (INTEGRAL_MODE_P (mode)
843               && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
844                   || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS)
845               && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
846             return tem;
847           break;
848
849         case COMPARE:
850 #ifdef HAVE_cc0
851           /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
852              using cc0, in which case we want to leave it as a COMPARE
853              so we can distinguish it from a register-register-copy.
854
855              In IEEE floating point, x-0 is not the same as x.  */
856
857           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
858                || ! FLOAT_MODE_P (mode) || flag_fast_math)
859               && op1 == CONST0_RTX (mode))
860             return op0;
861 #else
862           /* Do nothing here.  */
863 #endif
864           break;
865               
866         case MINUS:
867           /* None of these optimizations can be done for IEEE
868              floating point.  */
869           if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
870               && FLOAT_MODE_P (mode) && ! flag_fast_math)
871             break;
872
873           /* We can't assume x-x is 0 even with non-IEEE floating point,
874              but since it is zero except in very strange circumstances, we
875              will treat it as zero with -ffast-math.  */
876           if (rtx_equal_p (op0, op1)
877               && ! side_effects_p (op0)
878               && (! FLOAT_MODE_P (mode) || flag_fast_math))
879             return CONST0_RTX (mode);
880
881           /* Change subtraction from zero into negation.  */
882           if (op0 == CONST0_RTX (mode))
883             return gen_rtx_NEG (mode, op1);
884
885           /* (-1 - a) is ~a.  */
886           if (op0 == constm1_rtx)
887             return gen_rtx_NOT (mode, op1);
888
889           /* Subtracting 0 has no effect.  */
890           if (op1 == CONST0_RTX (mode))
891             return op0;
892
893           /* See if this is something like X * C - X or vice versa or
894              if the multiplication is written as a shift.  If so, we can
895              distribute and make a new multiply, shift, or maybe just
896              have X (if C is 2 in the example above).  But don't make
897              real multiply if we didn't have one before.  */
898
899           if (! FLOAT_MODE_P (mode))
900             {
901               HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
902               rtx lhs = op0, rhs = op1;
903               int had_mult = 0;
904
905               if (GET_CODE (lhs) == NEG)
906                 coeff0 = -1, lhs = XEXP (lhs, 0);
907               else if (GET_CODE (lhs) == MULT
908                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
909                 {
910                   coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
911                   had_mult = 1;
912                 }
913               else if (GET_CODE (lhs) == ASHIFT
914                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT
915                        && INTVAL (XEXP (lhs, 1)) >= 0
916                        && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
917                 {
918                   coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
919                   lhs = XEXP (lhs, 0);
920                 }
921
922               if (GET_CODE (rhs) == NEG)
923                 coeff1 = - 1, rhs = XEXP (rhs, 0);
924               else if (GET_CODE (rhs) == MULT
925                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
926                 {
927                   coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
928                   had_mult = 1;
929                 }
930               else if (GET_CODE (rhs) == ASHIFT
931                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT
932                        && INTVAL (XEXP (rhs, 1)) >= 0
933                        && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
934                 {
935                   coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
936                   rhs = XEXP (rhs, 0);
937                 }
938
939               if (rtx_equal_p (lhs, rhs))
940                 {
941                   tem = simplify_gen_binary (MULT, mode, lhs,
942                                              GEN_INT (coeff0 - coeff1));
943                   return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
944                 }
945             }
946
947           /* (a - (-b)) -> (a + b).  */
948           if (GET_CODE (op1) == NEG)
949             return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
950
951           /* If one of the operands is a PLUS or a MINUS, see if we can
952              simplify this by the associative law. 
953              Don't use the associative law for floating point.
954              The inaccuracy makes it nonassociative,
955              and subtle programs can break if operations are associated.  */
956
957           if (INTEGRAL_MODE_P (mode)
958               && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
959                   || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS)
960               && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
961             return tem;
962
963           /* Don't let a relocatable value get a negative coeff.  */
964           if (GET_CODE (op1) == CONST_INT && GET_MODE (op0) != VOIDmode)
965             return plus_constant (op0, - INTVAL (op1));
966
967           /* (x - (x & y)) -> (x & ~y) */
968           if (GET_CODE (op1) == AND)
969             {
970              if (rtx_equal_p (op0, XEXP (op1, 0)))
971                return simplify_gen_binary (AND, mode, op0,
972                                            gen_rtx_NOT (mode, XEXP (op1, 1)));
973              if (rtx_equal_p (op0, XEXP (op1, 1)))
974                return simplify_gen_binary (AND, mode, op0,
975                                            gen_rtx_NOT (mode, XEXP (op1, 0)));
976            }
977           break;
978
979         case MULT:
980           if (op1 == constm1_rtx)
981             {
982               tem = simplify_unary_operation (NEG, mode, op0, mode);
983
984               return tem ? tem : gen_rtx_NEG (mode, op0);
985             }
986
987           /* In IEEE floating point, x*0 is not always 0.  */
988           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
989                || ! FLOAT_MODE_P (mode) || flag_fast_math)
990               && op1 == CONST0_RTX (mode)
991               && ! side_effects_p (op0))
992             return op1;
993
994           /* In IEEE floating point, x*1 is not equivalent to x for nans.
995              However, ANSI says we can drop signals,
996              so we can do this anyway.  */
997           if (op1 == CONST1_RTX (mode))
998             return op0;
999
1000           /* Convert multiply by constant power of two into shift unless
1001              we are still generating RTL.  This test is a kludge.  */
1002           if (GET_CODE (op1) == CONST_INT
1003               && (val = exact_log2 (INTVAL (op1))) >= 0
1004               /* If the mode is larger than the host word size, and the
1005                  uppermost bit is set, then this isn't a power of two due
1006                  to implicit sign extension.  */
1007               && (width <= HOST_BITS_PER_WIDE_INT
1008                   || val != HOST_BITS_PER_WIDE_INT - 1)
1009               && ! rtx_equal_function_value_matters)
1010             return gen_rtx_ASHIFT (mode, op0, GEN_INT (val));
1011
1012           if (GET_CODE (op1) == CONST_DOUBLE
1013               && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT)
1014             {
1015               REAL_VALUE_TYPE d;
1016               jmp_buf handler;
1017               int op1is2, op1ism1;
1018
1019               if (setjmp (handler))
1020                 return 0;
1021
1022               set_float_handler (handler);
1023               REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
1024               op1is2 = REAL_VALUES_EQUAL (d, dconst2);
1025               op1ism1 = REAL_VALUES_EQUAL (d, dconstm1);
1026               set_float_handler (NULL_PTR);
1027
1028               /* x*2 is x+x and x*(-1) is -x */
1029               if (op1is2 && GET_MODE (op0) == mode)
1030                 return gen_rtx_PLUS (mode, op0, copy_rtx (op0));
1031
1032               else if (op1ism1 && GET_MODE (op0) == mode)
1033                 return gen_rtx_NEG (mode, op0);
1034             }
1035           break;
1036
1037         case IOR:
1038           if (op1 == const0_rtx)
1039             return op0;
1040           if (GET_CODE (op1) == CONST_INT
1041               && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1042             return op1;
1043           if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1044             return op0;
1045           /* A | (~A) -> -1 */
1046           if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
1047                || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
1048               && ! side_effects_p (op0)
1049               && GET_MODE_CLASS (mode) != MODE_CC)
1050             return constm1_rtx;
1051           break;
1052
1053         case XOR:
1054           if (op1 == const0_rtx)
1055             return op0;
1056           if (GET_CODE (op1) == CONST_INT
1057               && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1058             return gen_rtx_NOT (mode, op0);
1059           if (op0 == op1 && ! side_effects_p (op0)
1060               && GET_MODE_CLASS (mode) != MODE_CC)
1061             return const0_rtx;
1062           break;
1063
1064         case AND:
1065           if (op1 == const0_rtx && ! side_effects_p (op0))
1066             return const0_rtx;
1067           if (GET_CODE (op1) == CONST_INT
1068               && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1069             return op0;
1070           if (op0 == op1 && ! side_effects_p (op0)
1071               && GET_MODE_CLASS (mode) != MODE_CC)
1072             return op0;
1073           /* A & (~A) -> 0 */
1074           if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
1075                || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
1076               && ! side_effects_p (op0)
1077               && GET_MODE_CLASS (mode) != MODE_CC)
1078             return const0_rtx;
1079           break;
1080
1081         case UDIV:
1082           /* Convert divide by power of two into shift (divide by 1 handled
1083              below).  */
1084           if (GET_CODE (op1) == CONST_INT
1085               && (arg1 = exact_log2 (INTVAL (op1))) > 0)
1086             return gen_rtx_LSHIFTRT (mode, op0, GEN_INT (arg1));
1087
1088           /* ... fall through ...  */
1089
1090         case DIV:
1091           if (op1 == CONST1_RTX (mode))
1092             return op0;
1093
1094           /* In IEEE floating point, 0/x is not always 0.  */
1095           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1096                || ! FLOAT_MODE_P (mode) || flag_fast_math)
1097               && op0 == CONST0_RTX (mode)
1098               && ! side_effects_p (op1))
1099             return op0;
1100
1101 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1102           /* Change division by a constant into multiplication.  Only do
1103              this with -ffast-math until an expert says it is safe in
1104              general.  */
1105           else if (GET_CODE (op1) == CONST_DOUBLE
1106                    && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT
1107                    && op1 != CONST0_RTX (mode)
1108                    && flag_fast_math)
1109             {
1110               REAL_VALUE_TYPE d;
1111               REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
1112
1113               if (! REAL_VALUES_EQUAL (d, dconst0))
1114                 {
1115 #if defined (REAL_ARITHMETIC)
1116                   REAL_ARITHMETIC (d, rtx_to_tree_code (DIV), dconst1, d);
1117                   return gen_rtx_MULT (mode, op0, 
1118                                        CONST_DOUBLE_FROM_REAL_VALUE (d, mode));
1119 #else
1120                   return
1121                     gen_rtx_MULT (mode, op0, 
1122                                   CONST_DOUBLE_FROM_REAL_VALUE (1./d, mode));
1123 #endif
1124                 }
1125             }
1126 #endif
1127           break;
1128
1129         case UMOD:
1130           /* Handle modulus by power of two (mod with 1 handled below).  */
1131           if (GET_CODE (op1) == CONST_INT
1132               && exact_log2 (INTVAL (op1)) > 0)
1133             return gen_rtx_AND (mode, op0, GEN_INT (INTVAL (op1) - 1));
1134
1135           /* ... fall through ...  */
1136
1137         case MOD:
1138           if ((op0 == const0_rtx || op1 == const1_rtx)
1139               && ! side_effects_p (op0) && ! side_effects_p (op1))
1140             return const0_rtx;
1141           break;
1142
1143         case ROTATERT:
1144         case ROTATE:
1145           /* Rotating ~0 always results in ~0.  */
1146           if (GET_CODE (op0) == CONST_INT && width <= HOST_BITS_PER_WIDE_INT
1147               && (unsigned HOST_WIDE_INT) INTVAL (op0) == GET_MODE_MASK (mode)
1148               && ! side_effects_p (op1))
1149             return op0;
1150
1151           /* ... fall through ...  */
1152
1153         case ASHIFT:
1154         case ASHIFTRT:
1155         case LSHIFTRT:
1156           if (op1 == const0_rtx)
1157             return op0;
1158           if (op0 == const0_rtx && ! side_effects_p (op1))
1159             return op0;
1160           break;
1161
1162         case SMIN:
1163           if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT 
1164               && INTVAL (op1) == (HOST_WIDE_INT) 1 << (width -1)
1165               && ! side_effects_p (op0))
1166             return op1;
1167           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1168             return op0;
1169           break;
1170            
1171         case SMAX:
1172           if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT
1173               && ((unsigned HOST_WIDE_INT) INTVAL (op1)
1174                   == (unsigned HOST_WIDE_INT) GET_MODE_MASK (mode) >> 1)
1175               && ! side_effects_p (op0))
1176             return op1;
1177           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1178             return op0;
1179           break;
1180
1181         case UMIN:
1182           if (op1 == const0_rtx && ! side_effects_p (op0))
1183             return op1;
1184           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1185             return op0;
1186           break;
1187             
1188         case UMAX:
1189           if (op1 == constm1_rtx && ! side_effects_p (op0))
1190             return op1;
1191           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1192             return op0;
1193           break;
1194
1195         default:
1196           abort ();
1197         }
1198       
1199       return 0;
1200     }
1201
1202   /* Get the integer argument values in two forms:
1203      zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S.  */
1204
1205   arg0 = INTVAL (op0);
1206   arg1 = INTVAL (op1);
1207
1208   if (width < HOST_BITS_PER_WIDE_INT)
1209     {
1210       arg0 &= ((HOST_WIDE_INT) 1 << width) - 1;
1211       arg1 &= ((HOST_WIDE_INT) 1 << width) - 1;
1212
1213       arg0s = arg0;
1214       if (arg0s & ((HOST_WIDE_INT) 1 << (width - 1)))
1215         arg0s |= ((HOST_WIDE_INT) (-1) << width);
1216
1217       arg1s = arg1;
1218       if (arg1s & ((HOST_WIDE_INT) 1 << (width - 1)))
1219         arg1s |= ((HOST_WIDE_INT) (-1) << width);
1220     }
1221   else
1222     {
1223       arg0s = arg0;
1224       arg1s = arg1;
1225     }
1226
1227   /* Compute the value of the arithmetic.  */
1228
1229   switch (code)
1230     {
1231     case PLUS:
1232       val = arg0s + arg1s;
1233       break;
1234
1235     case MINUS:
1236       val = arg0s - arg1s;
1237       break;
1238
1239     case MULT:
1240       val = arg0s * arg1s;
1241       break;
1242
1243     case DIV:
1244       if (arg1s == 0)
1245         return 0;
1246       val = arg0s / arg1s;
1247       break;
1248
1249     case MOD:
1250       if (arg1s == 0)
1251         return 0;
1252       val = arg0s % arg1s;
1253       break;
1254
1255     case UDIV:
1256       if (arg1 == 0)
1257         return 0;
1258       val = (unsigned HOST_WIDE_INT) arg0 / arg1;
1259       break;
1260
1261     case UMOD:
1262       if (arg1 == 0)
1263         return 0;
1264       val = (unsigned HOST_WIDE_INT) arg0 % arg1;
1265       break;
1266
1267     case AND:
1268       val = arg0 & arg1;
1269       break;
1270
1271     case IOR:
1272       val = arg0 | arg1;
1273       break;
1274
1275     case XOR:
1276       val = arg0 ^ arg1;
1277       break;
1278
1279     case LSHIFTRT:
1280       /* If shift count is undefined, don't fold it; let the machine do
1281          what it wants.  But truncate it if the machine will do that.  */
1282       if (arg1 < 0)
1283         return 0;
1284
1285 #ifdef SHIFT_COUNT_TRUNCATED
1286       if (SHIFT_COUNT_TRUNCATED)
1287         arg1 %= width;
1288 #endif
1289
1290       val = ((unsigned HOST_WIDE_INT) arg0) >> arg1;
1291       break;
1292
1293     case ASHIFT:
1294       if (arg1 < 0)
1295         return 0;
1296
1297 #ifdef SHIFT_COUNT_TRUNCATED
1298       if (SHIFT_COUNT_TRUNCATED)
1299         arg1 %= width;
1300 #endif
1301
1302       val = ((unsigned HOST_WIDE_INT) arg0) << arg1;
1303       break;
1304
1305     case ASHIFTRT:
1306       if (arg1 < 0)
1307         return 0;
1308
1309 #ifdef SHIFT_COUNT_TRUNCATED
1310       if (SHIFT_COUNT_TRUNCATED)
1311         arg1 %= width;
1312 #endif
1313
1314       val = arg0s >> arg1;
1315
1316       /* Bootstrap compiler may not have sign extended the right shift.
1317          Manually extend the sign to insure bootstrap cc matches gcc.  */
1318       if (arg0s < 0 && arg1 > 0)
1319         val |= ((HOST_WIDE_INT) -1) << (HOST_BITS_PER_WIDE_INT - arg1);
1320
1321       break;
1322
1323     case ROTATERT:
1324       if (arg1 < 0)
1325         return 0;
1326
1327       arg1 %= width;
1328       val = ((((unsigned HOST_WIDE_INT) arg0) << (width - arg1))
1329              | (((unsigned HOST_WIDE_INT) arg0) >> arg1));
1330       break;
1331
1332     case ROTATE:
1333       if (arg1 < 0)
1334         return 0;
1335
1336       arg1 %= width;
1337       val = ((((unsigned HOST_WIDE_INT) arg0) << arg1)
1338              | (((unsigned HOST_WIDE_INT) arg0) >> (width - arg1)));
1339       break;
1340
1341     case COMPARE:
1342       /* Do nothing here.  */
1343       return 0;
1344
1345     case SMIN:
1346       val = arg0s <= arg1s ? arg0s : arg1s;
1347       break;
1348
1349     case UMIN:
1350       val = ((unsigned HOST_WIDE_INT) arg0
1351              <= (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1352       break;
1353
1354     case SMAX:
1355       val = arg0s > arg1s ? arg0s : arg1s;
1356       break;
1357
1358     case UMAX:
1359       val = ((unsigned HOST_WIDE_INT) arg0
1360              > (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1361       break;
1362
1363     default:
1364       abort ();
1365     }
1366
1367   val = trunc_int_for_mode (val, mode);
1368
1369   return GEN_INT (val);
1370 }
1371 \f
1372 /* Simplify a PLUS or MINUS, at least one of whose operands may be another
1373    PLUS or MINUS.
1374
1375    Rather than test for specific case, we do this by a brute-force method
1376    and do all possible simplifications until no more changes occur.  Then
1377    we rebuild the operation.  */
1378
1379 static rtx
1380 simplify_plus_minus (code, mode, op0, op1)
1381      enum rtx_code code;
1382      enum machine_mode mode;
1383      rtx op0, op1;
1384 {
1385   rtx ops[8];
1386   int negs[8];
1387   rtx result, tem;
1388   int n_ops = 2, input_ops = 2, input_consts = 0, n_consts = 0;
1389   int first = 1, negate = 0, changed;
1390   int i, j;
1391
1392   bzero ((char *) ops, sizeof ops);
1393   
1394   /* Set up the two operands and then expand them until nothing has been
1395      changed.  If we run out of room in our array, give up; this should
1396      almost never happen.  */
1397
1398   ops[0] = op0, ops[1] = op1, negs[0] = 0, negs[1] = (code == MINUS);
1399
1400   changed = 1;
1401   while (changed)
1402     {
1403       changed = 0;
1404
1405       for (i = 0; i < n_ops; i++)
1406         switch (GET_CODE (ops[i]))
1407           {
1408           case PLUS:
1409           case MINUS:
1410             if (n_ops == 7)
1411               return 0;
1412
1413             ops[n_ops] = XEXP (ops[i], 1);
1414             negs[n_ops++] = GET_CODE (ops[i]) == MINUS ? !negs[i] : negs[i];
1415             ops[i] = XEXP (ops[i], 0);
1416             input_ops++;
1417             changed = 1;
1418             break;
1419
1420           case NEG:
1421             ops[i] = XEXP (ops[i], 0);
1422             negs[i] = ! negs[i];
1423             changed = 1;
1424             break;
1425
1426           case CONST:
1427             ops[i] = XEXP (ops[i], 0);
1428             input_consts++;
1429             changed = 1;
1430             break;
1431
1432           case NOT:
1433             /* ~a -> (-a - 1) */
1434             if (n_ops != 7)
1435               {
1436                 ops[n_ops] = constm1_rtx;
1437                 negs[n_ops++] = negs[i];
1438                 ops[i] = XEXP (ops[i], 0);
1439                 negs[i] = ! negs[i];
1440                 changed = 1;
1441               }
1442             break;
1443
1444           case CONST_INT:
1445             if (negs[i])
1446               ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0, changed = 1;
1447             break;
1448
1449           default:
1450             break;
1451           }
1452     }
1453
1454   /* If we only have two operands, we can't do anything.  */
1455   if (n_ops <= 2)
1456     return 0;
1457
1458   /* Now simplify each pair of operands until nothing changes.  The first
1459      time through just simplify constants against each other.  */
1460
1461   changed = 1;
1462   while (changed)
1463     {
1464       changed = first;
1465
1466       for (i = 0; i < n_ops - 1; i++)
1467         for (j = i + 1; j < n_ops; j++)
1468           if (ops[i] != 0 && ops[j] != 0
1469               && (! first || (CONSTANT_P (ops[i]) && CONSTANT_P (ops[j]))))
1470             {
1471               rtx lhs = ops[i], rhs = ops[j];
1472               enum rtx_code ncode = PLUS;
1473
1474               if (negs[i] && ! negs[j])
1475                 lhs = ops[j], rhs = ops[i], ncode = MINUS;
1476               else if (! negs[i] && negs[j])
1477                 ncode = MINUS;
1478
1479               tem = simplify_binary_operation (ncode, mode, lhs, rhs);
1480               if (tem)
1481                 {
1482                   ops[i] = tem, ops[j] = 0;
1483                   negs[i] = negs[i] && negs[j];
1484                   if (GET_CODE (tem) == NEG)
1485                     ops[i] = XEXP (tem, 0), negs[i] = ! negs[i];
1486
1487                   if (GET_CODE (ops[i]) == CONST_INT && negs[i])
1488                     ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0;
1489                   changed = 1;
1490                 }
1491             }
1492
1493       first = 0;
1494     }
1495
1496   /* Pack all the operands to the lower-numbered entries and give up if
1497      we didn't reduce the number of operands we had.  Make sure we
1498      count a CONST as two operands.  If we have the same number of
1499      operands, but have made more CONSTs than we had, this is also
1500      an improvement, so accept it.  */
1501
1502   for (i = 0, j = 0; j < n_ops; j++)
1503     if (ops[j] != 0)
1504       {
1505         ops[i] = ops[j], negs[i++] = negs[j];
1506         if (GET_CODE (ops[j]) == CONST)
1507           n_consts++;
1508       }
1509
1510   if (i + n_consts > input_ops
1511       || (i + n_consts == input_ops && n_consts <= input_consts))
1512     return 0;
1513
1514   n_ops = i;
1515
1516   /* If we have a CONST_INT, put it last.  */
1517   for (i = 0; i < n_ops - 1; i++)
1518     if (GET_CODE (ops[i]) == CONST_INT)
1519       {
1520         tem = ops[n_ops - 1], ops[n_ops - 1] = ops[i] , ops[i] = tem;
1521         j = negs[n_ops - 1], negs[n_ops - 1] = negs[i], negs[i] = j;
1522       }
1523
1524   /* Put a non-negated operand first.  If there aren't any, make all
1525      operands positive and negate the whole thing later.  */
1526   for (i = 0; i < n_ops && negs[i]; i++)
1527     ;
1528
1529   if (i == n_ops)
1530     {
1531       for (i = 0; i < n_ops; i++)
1532         negs[i] = 0;
1533       negate = 1;
1534     }
1535   else if (i != 0)
1536     {
1537       tem = ops[0], ops[0] = ops[i], ops[i] = tem;
1538       j = negs[0], negs[0] = negs[i], negs[i] = j;
1539     }
1540
1541   /* Now make the result by performing the requested operations.  */
1542   result = ops[0];
1543   for (i = 1; i < n_ops; i++)
1544     result = simplify_gen_binary (negs[i] ? MINUS : PLUS, mode, result, ops[i]);
1545
1546   return negate ? gen_rtx_NEG (mode, result) : result;
1547 }
1548
1549 struct cfc_args
1550 {
1551   rtx op0, op1;                 /* Input */
1552   int equal, op0lt, op1lt;      /* Output */
1553 };
1554
1555 static void
1556 check_fold_consts (data)
1557   PTR data;
1558 {
1559   struct cfc_args *args = (struct cfc_args *) data;
1560   REAL_VALUE_TYPE d0, d1;
1561
1562   REAL_VALUE_FROM_CONST_DOUBLE (d0, args->op0);
1563   REAL_VALUE_FROM_CONST_DOUBLE (d1, args->op1);
1564   args->equal = REAL_VALUES_EQUAL (d0, d1);
1565   args->op0lt = REAL_VALUES_LESS (d0, d1);
1566   args->op1lt = REAL_VALUES_LESS (d1, d0);
1567 }
1568
1569 /* Like simplify_binary_operation except used for relational operators.
1570    MODE is the mode of the operands, not that of the result.  If MODE
1571    is VOIDmode, both operands must also be VOIDmode and we compare the
1572    operands in "infinite precision".
1573
1574    If no simplification is possible, this function returns zero.  Otherwise,
1575    it returns either const_true_rtx or const0_rtx.  */
1576
1577 rtx
1578 simplify_relational_operation (code, mode, op0, op1)
1579      enum rtx_code code;
1580      enum machine_mode mode;
1581      rtx op0, op1;
1582 {
1583   int equal, op0lt, op0ltu, op1lt, op1ltu;
1584   rtx tem;
1585
1586   /* If op0 is a compare, extract the comparison arguments from it.  */
1587   if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
1588     op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
1589
1590   /* We can't simplify MODE_CC values since we don't know what the
1591      actual comparison is.  */
1592   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC
1593 #ifdef HAVE_cc0
1594       || op0 == cc0_rtx
1595 #endif
1596       )
1597     return 0;
1598
1599   /* Make sure the constant is second.  */
1600   if ((CONSTANT_P (op0) && ! CONSTANT_P (op1))
1601       || (GET_CODE (op0) == CONST_INT && GET_CODE (op1) != CONST_INT))
1602     {
1603       tem = op0, op0 = op1, op1 = tem;
1604       code = swap_condition (code);
1605     }
1606
1607   /* For integer comparisons of A and B maybe we can simplify A - B and can
1608      then simplify a comparison of that with zero.  If A and B are both either
1609      a register or a CONST_INT, this can't help; testing for these cases will
1610      prevent infinite recursion here and speed things up.
1611
1612      If CODE is an unsigned comparison, then we can never do this optimization,
1613      because it gives an incorrect result if the subtraction wraps around zero.
1614      ANSI C defines unsigned operations such that they never overflow, and
1615      thus such cases can not be ignored.  */
1616
1617   if (INTEGRAL_MODE_P (mode) && op1 != const0_rtx
1618       && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == CONST_INT)
1619             && (GET_CODE (op1) == REG || GET_CODE (op1) == CONST_INT))
1620       && 0 != (tem = simplify_binary_operation (MINUS, mode, op0, op1))
1621       && code != GTU && code != GEU && code != LTU && code != LEU)
1622     return simplify_relational_operation (signed_condition (code),
1623                                           mode, tem, const0_rtx);
1624
1625   /* For non-IEEE floating-point, if the two operands are equal, we know the
1626      result.  */
1627   if (rtx_equal_p (op0, op1)
1628       && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1629           || ! FLOAT_MODE_P (GET_MODE (op0)) || flag_fast_math))
1630     equal = 1, op0lt = 0, op0ltu = 0, op1lt = 0, op1ltu = 0;
1631
1632   /* If the operands are floating-point constants, see if we can fold
1633      the result.  */
1634 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1635   else if (GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE
1636            && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
1637     {
1638       struct cfc_args args;
1639
1640       /* Setup input for check_fold_consts() */
1641       args.op0 = op0;
1642       args.op1 = op1;
1643       
1644       if (do_float_handler(check_fold_consts, (PTR) &args) == 0)
1645         /* We got an exception from check_fold_consts() */
1646         return 0;
1647
1648       /* Receive output from check_fold_consts() */
1649       equal = args.equal;
1650       op0lt = op0ltu = args.op0lt;
1651       op1lt = op1ltu = args.op1lt;
1652     }
1653 #endif  /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1654
1655   /* Otherwise, see if the operands are both integers.  */
1656   else if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
1657            && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT)
1658            && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT))
1659     {
1660       int width = GET_MODE_BITSIZE (mode);
1661       HOST_WIDE_INT l0s, h0s, l1s, h1s;
1662       unsigned HOST_WIDE_INT l0u, h0u, l1u, h1u;
1663
1664       /* Get the two words comprising each integer constant.  */
1665       if (GET_CODE (op0) == CONST_DOUBLE)
1666         {
1667           l0u = l0s = CONST_DOUBLE_LOW (op0);
1668           h0u = h0s = CONST_DOUBLE_HIGH (op0);
1669         }
1670       else
1671         {
1672           l0u = l0s = INTVAL (op0);
1673           h0u = h0s = l0s < 0 ? -1 : 0;
1674         }
1675           
1676       if (GET_CODE (op1) == CONST_DOUBLE)
1677         {
1678           l1u = l1s = CONST_DOUBLE_LOW (op1);
1679           h1u = h1s = CONST_DOUBLE_HIGH (op1);
1680         }
1681       else
1682         {
1683           l1u = l1s = INTVAL (op1);
1684           h1u = h1s = l1s < 0 ? -1 : 0;
1685         }
1686
1687       /* If WIDTH is nonzero and smaller than HOST_BITS_PER_WIDE_INT,
1688          we have to sign or zero-extend the values.  */
1689       if (width != 0 && width <= HOST_BITS_PER_WIDE_INT)
1690         h0u = h1u = 0, h0s = l0s < 0 ? -1 : 0, h1s = l1s < 0 ? -1 : 0;
1691
1692       if (width != 0 && width < HOST_BITS_PER_WIDE_INT)
1693         {
1694           l0u &= ((HOST_WIDE_INT) 1 << width) - 1;
1695           l1u &= ((HOST_WIDE_INT) 1 << width) - 1;
1696
1697           if (l0s & ((HOST_WIDE_INT) 1 << (width - 1)))
1698             l0s |= ((HOST_WIDE_INT) (-1) << width);
1699
1700           if (l1s & ((HOST_WIDE_INT) 1 << (width - 1)))
1701             l1s |= ((HOST_WIDE_INT) (-1) << width);
1702         }
1703
1704       equal = (h0u == h1u && l0u == l1u);
1705       op0lt = (h0s < h1s || (h0s == h1s && l0s < l1s));
1706       op1lt = (h1s < h0s || (h1s == h0s && l1s < l0s));
1707       op0ltu = (h0u < h1u || (h0u == h1u && l0u < l1u));
1708       op1ltu = (h1u < h0u || (h1u == h0u && l1u < l0u));
1709     }
1710
1711   /* Otherwise, there are some code-specific tests we can make.  */
1712   else
1713     {
1714       switch (code)
1715         {
1716         case EQ:
1717           /* References to the frame plus a constant or labels cannot
1718              be zero, but a SYMBOL_REF can due to #pragma weak.  */
1719           if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx)
1720                || GET_CODE (op0) == LABEL_REF)
1721 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1722               /* On some machines, the ap reg can be 0 sometimes.  */
1723               && op0 != arg_pointer_rtx
1724 #endif
1725                 )
1726             return const0_rtx;
1727           break;
1728
1729         case NE:
1730           if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx)
1731                || GET_CODE (op0) == LABEL_REF)
1732 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1733               && op0 != arg_pointer_rtx
1734 #endif
1735               )
1736             return const_true_rtx;
1737           break;
1738
1739         case GEU:
1740           /* Unsigned values are never negative.  */
1741           if (op1 == const0_rtx)
1742             return const_true_rtx;
1743           break;
1744
1745         case LTU:
1746           if (op1 == const0_rtx)
1747             return const0_rtx;
1748           break;
1749
1750         case LEU:
1751           /* Unsigned values are never greater than the largest
1752              unsigned value.  */
1753           if (GET_CODE (op1) == CONST_INT
1754               && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
1755             && INTEGRAL_MODE_P (mode))
1756           return const_true_rtx;
1757           break;
1758
1759         case GTU:
1760           if (GET_CODE (op1) == CONST_INT
1761               && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
1762               && INTEGRAL_MODE_P (mode))
1763             return const0_rtx;
1764           break;
1765           
1766         default:
1767           break;
1768         }
1769
1770       return 0;
1771     }
1772
1773   /* If we reach here, EQUAL, OP0LT, OP0LTU, OP1LT, and OP1LTU are set
1774      as appropriate.  */
1775   switch (code)
1776     {
1777     case EQ:
1778       return equal ? const_true_rtx : const0_rtx;
1779     case NE:
1780       return ! equal ? const_true_rtx : const0_rtx;
1781     case LT:
1782       return op0lt ? const_true_rtx : const0_rtx;
1783     case GT:
1784       return op1lt ? const_true_rtx : const0_rtx;
1785     case LTU:
1786       return op0ltu ? const_true_rtx : const0_rtx;
1787     case GTU:
1788       return op1ltu ? const_true_rtx : const0_rtx;
1789     case LE:
1790       return equal || op0lt ? const_true_rtx : const0_rtx;
1791     case GE:
1792       return equal || op1lt ? const_true_rtx : const0_rtx;
1793     case LEU:
1794       return equal || op0ltu ? const_true_rtx : const0_rtx;
1795     case GEU:
1796       return equal || op1ltu ? const_true_rtx : const0_rtx;
1797     default:
1798       abort ();
1799     }
1800 }
1801 \f
1802 /* Simplify CODE, an operation with result mode MODE and three operands,
1803    OP0, OP1, and OP2.  OP0_MODE was the mode of OP0 before it became
1804    a constant.  Return 0 if no simplifications is possible.  */
1805
1806 rtx
1807 simplify_ternary_operation (code, mode, op0_mode, op0, op1, op2)
1808      enum rtx_code code;
1809      enum machine_mode mode, op0_mode;
1810      rtx op0, op1, op2;
1811 {
1812   int width = GET_MODE_BITSIZE (mode);
1813
1814   /* VOIDmode means "infinite" precision.  */
1815   if (width == 0)
1816     width = HOST_BITS_PER_WIDE_INT;
1817
1818   switch (code)
1819     {
1820     case SIGN_EXTRACT:
1821     case ZERO_EXTRACT:
1822       if (GET_CODE (op0) == CONST_INT
1823           && GET_CODE (op1) == CONST_INT
1824           && GET_CODE (op2) == CONST_INT
1825           && INTVAL (op1) + INTVAL (op2) <= GET_MODE_BITSIZE (op0_mode)
1826           && width <= HOST_BITS_PER_WIDE_INT)
1827         {
1828           /* Extracting a bit-field from a constant */
1829           HOST_WIDE_INT val = INTVAL (op0);
1830
1831           if (BITS_BIG_ENDIAN)
1832             val >>= (GET_MODE_BITSIZE (op0_mode)
1833                      - INTVAL (op2) - INTVAL (op1));
1834           else
1835             val >>= INTVAL (op2);
1836
1837           if (HOST_BITS_PER_WIDE_INT != INTVAL (op1))
1838             {
1839               /* First zero-extend.  */
1840               val &= ((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1;
1841               /* If desired, propagate sign bit.  */
1842               if (code == SIGN_EXTRACT
1843                   && (val & ((HOST_WIDE_INT) 1 << (INTVAL (op1) - 1))))
1844                 val |= ~ (((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1);
1845             }
1846
1847           /* Clear the bits that don't belong in our mode,
1848              unless they and our sign bit are all one.
1849              So we get either a reasonable negative value or a reasonable
1850              unsigned value for this mode.  */
1851           if (width < HOST_BITS_PER_WIDE_INT
1852               && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
1853                   != ((HOST_WIDE_INT) (-1) << (width - 1))))
1854             val &= ((HOST_WIDE_INT) 1 << width) - 1;
1855
1856           return GEN_INT (val);
1857         }
1858       break;
1859
1860     case IF_THEN_ELSE:
1861       if (GET_CODE (op0) == CONST_INT)
1862         return op0 != const0_rtx ? op1 : op2;
1863
1864       /* Convert a == b ? b : a to "a".  */
1865       if (GET_CODE (op0) == NE && ! side_effects_p (op0)
1866           && rtx_equal_p (XEXP (op0, 0), op1)
1867           && rtx_equal_p (XEXP (op0, 1), op2))
1868         return op1;
1869       else if (GET_CODE (op0) == EQ && ! side_effects_p (op0)
1870           && rtx_equal_p (XEXP (op0, 1), op1)
1871           && rtx_equal_p (XEXP (op0, 0), op2))
1872         return op2;
1873       else if (GET_RTX_CLASS (GET_CODE (op0)) == '<' && ! side_effects_p (op0))
1874         {
1875           rtx temp;
1876           temp = simplify_relational_operation (GET_CODE (op0), op0_mode,
1877                                                 XEXP (op0, 0), XEXP (op0, 1));
1878           /* See if any simplifications were possible.  */
1879           if (temp == const0_rtx)
1880             return op2;
1881           else if (temp == const1_rtx)
1882             return op1;
1883         }
1884       break;
1885
1886     default:
1887       abort ();
1888     }
1889
1890   return 0;
1891 }
1892
1893 /* Simplify X, an rtx expression.
1894
1895    Return the simplified expression or NULL if no simplifications
1896    were possible.
1897
1898    This is the preferred entry point into the simplification routines;
1899    however, we still allow passes to call the more specific routines.
1900
1901    Right now GCC has three (yes, three) major bodies of RTL simplficiation
1902    code that need to be unified.
1903
1904         1. fold_rtx in cse.c.  This code uses various CSE specific
1905            information to aid in RTL simplification.
1906
1907         2. simplify_rtx in combine.c.  Similar to fold_rtx, except that
1908            it uses combine specific information to aid in RTL
1909            simplification.
1910
1911         3. The routines in this file.
1912
1913
1914    Long term we want to only have one body of simplification code; to
1915    get to that state I recommend the following steps:
1916
1917         1. Pour over fold_rtx & simplify_rtx and move any simplifications
1918            which are not pass dependent state into these routines.
1919
1920         2. As code is moved by #1, change fold_rtx & simplify_rtx to
1921            use this routine whenever possible.
1922
1923         3. Allow for pass dependent state to be provided to these
1924            routines and add simplifications based on the pass dependent
1925            state.  Remove code from cse.c & combine.c that becomes
1926            redundant/dead.
1927
1928     It will take time, but ultimately the compiler will be easier to
1929     maintain and improve.  It's totally silly that when we add a
1930     simplification that it needs to be added to 4 places (3 for RTL
1931     simplification and 1 for tree simplification.  */
1932            
1933 rtx
1934 simplify_rtx (x)
1935      rtx x;
1936 {
1937   enum rtx_code code;
1938   enum machine_mode mode;
1939
1940   mode = GET_MODE (x);
1941   code = GET_CODE (x);
1942
1943   switch (GET_RTX_CLASS (code))
1944     {
1945     case '1':
1946       return simplify_unary_operation (code, mode,
1947                                        XEXP (x, 0), GET_MODE (XEXP (x, 0)));
1948     case '2':
1949     case 'c':
1950       return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
1951
1952     case '3':
1953     case 'b':
1954       return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
1955                                          XEXP (x, 0), XEXP (x, 1), XEXP (x, 2));
1956
1957     case '<':
1958       return simplify_relational_operation (code, GET_MODE (XEXP (x, 0)),
1959                                             XEXP (x, 0), XEXP (x, 1));
1960     default:
1961       return NULL;
1962     }
1963 }
1964 \f
1965 static int entry_and_rtx_equal_p        PARAMS ((const void *, const void *));
1966 static unsigned int get_value_hash      PARAMS ((const void *));
1967 static struct elt_list *new_elt_list    PARAMS ((struct elt_list *, cselib_val *));
1968 static struct elt_loc_list *new_elt_loc_list    PARAMS ((struct elt_loc_list *, rtx));
1969 static void unchain_one_value           PARAMS ((cselib_val *));
1970 static void unchain_one_elt_list        PARAMS ((struct elt_list **));
1971 static void unchain_one_elt_loc_list    PARAMS ((struct elt_loc_list **));
1972 static void clear_table                 PARAMS ((void));
1973 static int check_value_useless          PARAMS ((cselib_val *));
1974 static int discard_useless_locs         PARAMS ((void **, void *));
1975 static int discard_useless_values       PARAMS ((void **, void *));
1976 static void remove_useless_values       PARAMS ((void));
1977 static unsigned int hash_rtx            PARAMS ((rtx, enum machine_mode, int));
1978 static cselib_val *new_cselib_val       PARAMS ((unsigned int,
1979                                                  enum machine_mode));
1980 static void add_mem_for_addr            PARAMS ((cselib_val *, cselib_val *,
1981                                                  rtx));
1982 static cselib_val *cselib_lookup_mem    PARAMS ((rtx, int));
1983 static rtx cselib_subst_to_values       PARAMS ((rtx));
1984 static void cselib_invalidate_regno     PARAMS ((unsigned int,
1985                                                  enum machine_mode));
1986 static int cselib_mem_conflict_p        PARAMS ((rtx, rtx));
1987 static int cselib_invalidate_mem_1      PARAMS ((void **, void *));
1988 static void cselib_invalidate_mem       PARAMS ((rtx));
1989 static void cselib_invalidate_rtx       PARAMS ((rtx, rtx, void *));
1990 static void cselib_record_set           PARAMS ((rtx, cselib_val *,
1991                                                  cselib_val *));
1992 static void cselib_record_sets          PARAMS ((rtx));
1993
1994 /* There are three ways in which cselib can look up an rtx:
1995    - for a REG, the reg_values table (which is indexed by regno) is used
1996    - for a MEM, we recursively look up its address and then follow the
1997      addr_list of that value
1998    - for everything else, we compute a hash value and go through the hash
1999      table.  Since different rtx's can still have the same hash value,
2000      this involves walking the table entries for a given value and comparing
2001      the locations of the entries with the rtx we are looking up.  */
2002
2003 /* A table that enables us to look up elts by their value.  */
2004 static htab_t hash_table;
2005
2006 /* This is a global so we don't have to pass this through every function.
2007    It is used in new_elt_loc_list to set SETTING_INSN.  */
2008 static rtx cselib_current_insn;
2009
2010 /* Every new unknown value gets a unique number.  */
2011 static unsigned int next_unknown_value;
2012
2013 /* The number of registers we had when the varrays were last resized.  */
2014 static int cselib_nregs;
2015
2016 /* Count values without known locations.  Whenever this grows too big, we
2017    remove these useless values from the table.  */
2018 static int n_useless_values;
2019
2020 /* Number of useless values before we remove them from the hash table.  */
2021 #define MAX_USELESS_VALUES 32
2022
2023 /* This table maps from register number to values.  It does not contain
2024    pointers to cselib_val structures, but rather elt_lists.  The purpose is
2025    to be able to refer to the same register in different modes.  */
2026 static varray_type reg_values;
2027 #define REG_VALUES(I) VARRAY_ELT_LIST (reg_values, (I))
2028
2029 /* We pass this to cselib_invalidate_mem to invalidate all of
2030    memory for a non-const call instruction.  */
2031 static rtx callmem;
2032
2033 /* Memory for our structures is allocated from this obstack.  */
2034 static struct obstack cselib_obstack;
2035
2036 /* Used to quickly free all memory.  */
2037 static char *cselib_startobj;
2038
2039 /* Caches for unused structures.  */
2040 static cselib_val *empty_vals;
2041 static struct elt_list *empty_elt_lists;
2042 static struct elt_loc_list *empty_elt_loc_lists;
2043
2044 /* Allocate a struct elt_list and fill in its two elements with the
2045    arguments.  */
2046 static struct elt_list *
2047 new_elt_list (next, elt)
2048      struct elt_list *next;
2049      cselib_val *elt;
2050 {
2051   struct elt_list *el = empty_elt_lists;
2052   if (el)
2053     empty_elt_lists = el->next;
2054   else
2055     el = (struct elt_list *) obstack_alloc (&cselib_obstack,
2056                                             sizeof (struct elt_list));
2057   el->next = next;
2058   el->elt = elt;
2059   return el;
2060 }
2061
2062 /* Allocate a struct elt_loc_list and fill in its two elements with the
2063    arguments.  */
2064 static struct elt_loc_list *
2065 new_elt_loc_list (next, loc)
2066      struct elt_loc_list *next;
2067      rtx loc;
2068 {
2069   struct elt_loc_list *el = empty_elt_loc_lists;
2070   if (el)
2071     empty_elt_loc_lists = el->next;
2072   else
2073     el = (struct elt_loc_list *) obstack_alloc (&cselib_obstack,
2074                                                 sizeof (struct elt_loc_list));
2075   el->next = next;
2076   el->loc = loc;
2077   el->setting_insn = cselib_current_insn;
2078   return el;
2079 }
2080
2081 /* The elt_list at *PL is no longer needed.  Unchain it and free its
2082    storage.  */
2083 static void
2084 unchain_one_elt_list (pl)
2085      struct elt_list **pl;
2086 {
2087   struct elt_list *l = *pl;
2088   *pl = l->next;
2089   l->next = empty_elt_lists;
2090   empty_elt_lists = l;
2091 }
2092
2093 /* Likewise for elt_loc_lists.  */
2094 static void
2095 unchain_one_elt_loc_list (pl)
2096      struct elt_loc_list **pl;
2097 {
2098   struct elt_loc_list *l = *pl;
2099   *pl = l->next;
2100   l->next = empty_elt_loc_lists;
2101   empty_elt_loc_lists = l;
2102 }
2103
2104 /* Likewise for cselib_vals.  This also frees the addr_list associated with
2105    V.  */
2106 static void
2107 unchain_one_value (v)
2108      cselib_val *v;
2109 {
2110   while (v->addr_list)
2111     unchain_one_elt_list (&v->addr_list);
2112
2113   v->u.next_free = empty_vals;
2114   empty_vals = v;
2115 }
2116
2117 /* Remove all entries from the hash table.  Also used during
2118    initialization.  */
2119 static void
2120 clear_table ()
2121 {
2122   int i;
2123   for (i = 0; i < cselib_nregs; i++)
2124     REG_VALUES (i) = 0;
2125
2126   htab_empty (hash_table);
2127   obstack_free (&cselib_obstack, cselib_startobj);
2128
2129   empty_vals = 0;
2130   empty_elt_lists = 0;
2131   empty_elt_loc_lists = 0;
2132   n_useless_values = 0;
2133
2134   next_unknown_value = 0;
2135 }
2136
2137 /* The equality test for our hash table.  The first argument ENTRY is a table
2138    element (i.e. a cselib_val), while the second arg X is an rtx.  */
2139 static int
2140 entry_and_rtx_equal_p (entry, x_arg)
2141      const void *entry, *x_arg;
2142 {
2143   struct elt_loc_list *l;
2144   const cselib_val *v = (const cselib_val *)entry;
2145   rtx x = (rtx)x_arg;
2146
2147   /* We don't guarantee that distinct rtx's have different hash values,
2148      so we need to do a comparison.  */
2149   for (l = v->locs; l; l = l->next)
2150     if (rtx_equal_for_cselib_p (l->loc, x))
2151       return 1;
2152   return 0;
2153 }
2154
2155 /* The hash function for our hash table.  The value is always computed with
2156    hash_rtx when adding an element; this function just extracts the hash
2157    value from a cselib_val structure.  */
2158 static unsigned int
2159 get_value_hash (entry)
2160      const void *entry;
2161 {
2162   const cselib_val *v = (const cselib_val *) entry;
2163   return v->value;
2164 }
2165
2166 /* If there are no more locations that hold a value, the value has become
2167    useless.  See whether that is the case for V.  Return 1 if this has
2168    just become useless.  */
2169 static int
2170 check_value_useless (v)
2171      cselib_val *v;
2172 {
2173   if (v->locs != 0)
2174     return 0;
2175
2176   if (v->value == 0)
2177     return 0;
2178
2179   /* This is a marker to indicate that the value will be reclaimed.  */
2180   v->value = 0;
2181   n_useless_values++;
2182   return 1;
2183 }
2184
2185 /* Return true if X contains a VALUE rtx.  If ONLY_USELESS is set, we
2186    only return true for values which point to a cselib_val whose value
2187    element has been set to zero, which implies the cselib_val will be
2188    removed.  */
2189 int
2190 references_value_p (x, only_useless)
2191      rtx x;
2192      int only_useless;
2193 {
2194   enum rtx_code code = GET_CODE (x);
2195   const char *fmt = GET_RTX_FORMAT (code);
2196   int i;
2197
2198   if (GET_CODE (x) == VALUE
2199       && (! only_useless || CSELIB_VAL_PTR (x)->value == 0))
2200     return 1;
2201
2202   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2203     {
2204       if (fmt[i] == 'e')
2205         {
2206           if (references_value_p (XEXP (x, i), only_useless))
2207             return 1;
2208         }
2209       else if (fmt[i] == 'E')
2210         {
2211           int j;
2212
2213           for (j = 0; j < XVECLEN (x, i); j++)
2214             if (references_value_p (XVECEXP (x, i, j), only_useless))
2215               return 1;
2216         }
2217     }
2218
2219   return 0;
2220 }
2221
2222 /* Set by discard_useless_locs if it deleted the last location of any
2223    value.  */
2224 static int values_became_useless;
2225
2226 /* For all locations found in X, delete locations that reference useless
2227    values (i.e. values without any location).  Called through
2228    htab_traverse.  */
2229 static int
2230 discard_useless_locs (x, info)
2231      void **x;
2232      void *info ATTRIBUTE_UNUSED;
2233 {
2234   cselib_val *v = (cselib_val *)*x;
2235   struct elt_loc_list **p = &v->locs;
2236
2237   while (*p)
2238     {
2239       if (references_value_p ((*p)->loc, 1))
2240         unchain_one_elt_loc_list (p);
2241       else
2242         p = &(*p)->next;
2243     }
2244   if (check_value_useless (v))
2245     values_became_useless = 1;
2246
2247   return 1;
2248 }
2249
2250 /* If X is a value with no locations, remove it from the hashtable.  */
2251
2252 static int
2253 discard_useless_values (x, info)
2254      void **x;
2255      void *info ATTRIBUTE_UNUSED;
2256 {
2257   cselib_val *v = (cselib_val *)*x;
2258
2259   if (v->value == 0)
2260     {
2261       htab_clear_slot (hash_table, x);
2262       unchain_one_value (v);
2263       n_useless_values--;
2264     }
2265   return 1;
2266 }
2267
2268 /* Clean out useless values (i.e. those which no longer have locations
2269    associated with them) from the hash table.  */
2270 static void
2271 remove_useless_values ()
2272 {
2273   /* First pass: eliminate locations that reference the value.  That in
2274      turn can make more values useless.  */
2275   do
2276     {
2277       values_became_useless = 0;
2278       htab_traverse (hash_table, discard_useless_locs, 0);
2279     }
2280   while (values_became_useless);
2281
2282   /* Second pass: actually remove the values.  */
2283   htab_traverse (hash_table, discard_useless_values, 0);
2284
2285   if (n_useless_values != 0)
2286     abort ();
2287 }
2288
2289 /* Return nonzero if we can prove that X and Y contain the same value, taking
2290    our gathered information into account.  */
2291 int
2292 rtx_equal_for_cselib_p (x, y)
2293      rtx x, y;
2294 {
2295   enum rtx_code code;
2296   const char *fmt;
2297   int i;
2298   
2299   if (GET_CODE (x) == REG || GET_CODE (x) == MEM)
2300     {
2301       cselib_val *e = cselib_lookup (x, VOIDmode, 0);
2302       if (e)
2303         x = e->u.val_rtx;
2304     }
2305   if (GET_CODE (y) == REG || GET_CODE (y) == MEM)
2306     {
2307       cselib_val *e = cselib_lookup (y, VOIDmode, 0);
2308       if (e)
2309         y = e->u.val_rtx;
2310     }
2311
2312   if (x == y)
2313     return 1;
2314
2315   if (GET_CODE (x) == VALUE && GET_CODE (y) == VALUE)
2316     return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
2317
2318   if (GET_CODE (x) == VALUE)
2319     {
2320       cselib_val *e = CSELIB_VAL_PTR (x);
2321       struct elt_loc_list *l;
2322
2323       for (l = e->locs; l; l = l->next)
2324         {
2325           rtx t = l->loc;
2326
2327           /* Avoid infinite recursion.  */
2328           if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
2329             continue;
2330
2331           if (rtx_equal_for_cselib_p (t, y))
2332             return 1;
2333         }
2334       
2335       return 0;
2336     }
2337
2338   if (GET_CODE (y) == VALUE)
2339     {
2340       cselib_val *e = CSELIB_VAL_PTR (y);
2341       struct elt_loc_list *l;
2342
2343       for (l = e->locs; l; l = l->next)
2344         {
2345           rtx t = l->loc;
2346
2347           if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
2348             continue;
2349
2350           if (rtx_equal_for_cselib_p (x, t))
2351             return 1;
2352         }
2353       
2354       return 0;
2355     }
2356
2357   if (GET_CODE (x) != GET_CODE (y)
2358       || GET_MODE (x) != GET_MODE (y))
2359     return 0;
2360
2361   /* This won't be handled correctly by the code below.  */
2362   if (GET_CODE (x) == LABEL_REF)
2363     return XEXP (x, 0) == XEXP (y, 0);
2364   
2365   code = GET_CODE (x);
2366   fmt = GET_RTX_FORMAT (code);
2367
2368   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2369     {
2370       int j;
2371       switch (fmt[i])
2372         {
2373         case 'w':
2374           if (XWINT (x, i) != XWINT (y, i))
2375             return 0;
2376           break;
2377
2378         case 'n':
2379         case 'i':
2380           if (XINT (x, i) != XINT (y, i))
2381             return 0;
2382           break;
2383
2384         case 'V':
2385         case 'E':
2386           /* Two vectors must have the same length.  */
2387           if (XVECLEN (x, i) != XVECLEN (y, i))
2388             return 0;
2389
2390           /* And the corresponding elements must match.  */
2391           for (j = 0; j < XVECLEN (x, i); j++)
2392             if (! rtx_equal_for_cselib_p (XVECEXP (x, i, j),
2393                                           XVECEXP (y, i, j)))
2394               return 0;
2395           break;
2396
2397         case 'e':
2398           if (! rtx_equal_for_cselib_p (XEXP (x, i), XEXP (y, i)))
2399             return 0;
2400           break;
2401
2402         case 'S':
2403         case 's':
2404           if (strcmp (XSTR (x, i), XSTR (y, i)))
2405             return 0;
2406           break;
2407
2408         case 'u':
2409           /* These are just backpointers, so they don't matter.  */
2410           break;
2411
2412         case '0':
2413         case 't':
2414           break;
2415
2416           /* It is believed that rtx's at this level will never
2417              contain anything but integers and other rtx's,
2418              except for within LABEL_REFs and SYMBOL_REFs.  */
2419         default:
2420           abort ();
2421         }
2422     }
2423   return 1;
2424 }
2425
2426 /* Hash an rtx.  Return 0 if we couldn't hash the rtx.
2427    For registers and memory locations, we look up their cselib_val structure
2428    and return its VALUE element.
2429    Possible reasons for return 0 are: the object is volatile, or we couldn't
2430    find a register or memory location in the table and CREATE is zero.  If
2431    CREATE is nonzero, table elts are created for regs and mem.
2432    MODE is used in hashing for CONST_INTs only;
2433    otherwise the mode of X is used.  */
2434 static unsigned int
2435 hash_rtx (x, mode, create)
2436      rtx x;
2437      enum machine_mode mode;
2438      int create;
2439 {
2440   cselib_val *e;
2441   int i, j;
2442   enum rtx_code code;
2443   const char *fmt;
2444   unsigned int hash = 0;
2445
2446   /* repeat is used to turn tail-recursion into iteration.  */
2447  repeat:
2448   code = GET_CODE (x);
2449   hash += (unsigned) code + (unsigned) GET_MODE (x);
2450
2451   switch (code)
2452     {
2453     case MEM:
2454     case REG:
2455       e = cselib_lookup (x, GET_MODE (x), create);
2456       if (! e)
2457         return 0;
2458       hash += e->value;
2459       return hash;
2460
2461     case CONST_INT:
2462       {
2463         unsigned HOST_WIDE_INT tem = INTVAL (x);
2464         hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + tem;
2465         return hash ? hash : CONST_INT;
2466       }
2467
2468     case CONST_DOUBLE:
2469       /* This is like the general case, except that it only counts
2470          the integers representing the constant.  */
2471       hash += (unsigned) code + (unsigned) GET_MODE (x);
2472       if (GET_MODE (x) != VOIDmode)
2473         for (i = 2; i < GET_RTX_LENGTH (CONST_DOUBLE); i++)
2474           {
2475             unsigned HOST_WIDE_INT tem = XWINT (x, i);
2476             hash += tem;
2477           }
2478       else
2479         hash += ((unsigned) CONST_DOUBLE_LOW (x)
2480                  + (unsigned) CONST_DOUBLE_HIGH (x));
2481       return hash ? hash : CONST_DOUBLE;
2482
2483       /* Assume there is only one rtx object for any given label.  */
2484     case LABEL_REF:
2485       hash
2486         += ((unsigned) LABEL_REF << 7) + (unsigned long) XEXP (x, 0);
2487       return hash ? hash : LABEL_REF;
2488
2489     case SYMBOL_REF:
2490       hash
2491         += ((unsigned) SYMBOL_REF << 7) + (unsigned long) XSTR (x, 0);
2492       return hash ? hash : SYMBOL_REF;
2493
2494     case PRE_DEC:
2495     case PRE_INC:
2496     case POST_DEC:
2497     case POST_INC:
2498     case PC:
2499     case CC0:
2500     case CALL:
2501     case UNSPEC_VOLATILE:
2502       return 0;
2503
2504     case ASM_OPERANDS:
2505       if (MEM_VOLATILE_P (x))
2506         return 0;
2507
2508       break;
2509       
2510     default:
2511       break;
2512     }
2513
2514   i = GET_RTX_LENGTH (code) - 1;
2515   fmt = GET_RTX_FORMAT (code);
2516   for (; i >= 0; i--)
2517     {
2518       if (fmt[i] == 'e')
2519         {
2520           unsigned int tem_hash;
2521           rtx tem = XEXP (x, i);
2522
2523           /* If we are about to do the last recursive call
2524              needed at this level, change it into iteration.
2525              This function  is called enough to be worth it.  */
2526           if (i == 0)
2527             {
2528               x = tem;
2529               goto repeat;
2530             }
2531           tem_hash = hash_rtx (tem, 0, create);
2532           if (tem_hash == 0)
2533             return 0;
2534           hash += tem_hash;
2535         }
2536       else if (fmt[i] == 'E')
2537         for (j = 0; j < XVECLEN (x, i); j++)
2538           {
2539             unsigned int tem_hash = hash_rtx (XVECEXP (x, i, j), 0, create);
2540             if (tem_hash == 0)
2541               return 0;
2542             hash += tem_hash;
2543           }
2544       else if (fmt[i] == 's')
2545         {
2546           const unsigned char *p = (const unsigned char *) XSTR (x, i);
2547           if (p)
2548             while (*p)
2549               hash += *p++;
2550         }
2551       else if (fmt[i] == 'i')
2552         {
2553           unsigned int tem = XINT (x, i);
2554           hash += tem;
2555         }
2556       else if (fmt[i] == '0' || fmt[i] == 't')
2557         /* unused */;
2558       else
2559         abort ();
2560     }
2561   return hash ? hash : 1 + GET_CODE (x);
2562 }
2563
2564 /* Create a new value structure for VALUE and initialize it.  The mode of the
2565    value is MODE.  */
2566 static cselib_val *
2567 new_cselib_val (value, mode)
2568      unsigned int value;
2569      enum machine_mode mode;
2570 {
2571   cselib_val *e = empty_vals;
2572   if (e)
2573     empty_vals = e->u.next_free;
2574   else
2575     e = (cselib_val *) obstack_alloc (&cselib_obstack, sizeof (cselib_val));
2576   if (value == 0)
2577     abort ();
2578   e->value = value;
2579   e->u.val_rtx = gen_rtx_VALUE (mode);
2580   CSELIB_VAL_PTR (e->u.val_rtx) = e;
2581
2582   e->addr_list = 0;
2583   e->locs = 0;
2584   return e;
2585 }
2586
2587 /* ADDR_ELT is a value that is used as address.  MEM_ELT is the value that
2588    contains the data at this address.  X is a MEM that represents the
2589    value.  Update the two value structures to represent this situation.  */
2590 static void
2591 add_mem_for_addr (addr_elt, mem_elt, x)
2592      cselib_val *addr_elt, *mem_elt;
2593      rtx x;
2594 {
2595   rtx new;
2596   struct elt_loc_list *l;
2597
2598   /* Avoid duplicates.  */
2599   for (l = mem_elt->locs; l; l = l->next)
2600     if (GET_CODE (l->loc) == MEM
2601         && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
2602       return;
2603
2604   new = gen_rtx_MEM (GET_MODE (x), addr_elt->u.val_rtx);
2605   addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
2606
2607   RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
2608   MEM_COPY_ATTRIBUTES (new, x);
2609
2610   mem_elt->locs = new_elt_loc_list (mem_elt->locs, new);
2611 }
2612
2613 /* Subroutine of cselib_lookup.  Return a value for X, which is a MEM rtx.
2614    If CREATE, make a new one if we haven't seen it before.  */
2615 static cselib_val *
2616 cselib_lookup_mem (x, create)
2617      rtx x;
2618      int create;
2619 {
2620   void **slot;
2621   cselib_val *addr;
2622   cselib_val *mem_elt;
2623   struct elt_list *l;
2624
2625   if (MEM_VOLATILE_P (x) || GET_MODE (x) == BLKmode)
2626     return 0;
2627   if (FLOAT_MODE_P (GET_MODE (x)) && flag_float_store)
2628     return 0;
2629
2630   /* Look up the value for the address.  */
2631   addr = cselib_lookup (XEXP (x, 0), GET_MODE (x), create);
2632   if (! addr)
2633     return 0;
2634
2635   /* Find a value that describes a value of our mode at that address.  */
2636   for (l = addr->addr_list; l; l = l->next)
2637     if (GET_MODE (l->elt->u.val_rtx) == GET_MODE (x))
2638       return l->elt;
2639   if (! create)
2640     return 0;
2641   mem_elt = new_cselib_val (++next_unknown_value, GET_MODE (x));
2642   add_mem_for_addr (addr, mem_elt, x);
2643   slot = htab_find_slot_with_hash (hash_table, x, mem_elt->value, 1);
2644   *slot = mem_elt;
2645   return mem_elt;
2646 }
2647
2648 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
2649    with VALUE expressions.  This way, it becomes independent of changes
2650    to registers and memory.
2651    X isn't actually modified; if modifications are needed, new rtl is
2652    allocated.  However, the return value can share rtl with X.  */
2653 static rtx
2654 cselib_subst_to_values (x)
2655      rtx x;
2656 {
2657   enum rtx_code code = GET_CODE (x);
2658   const char *fmt = GET_RTX_FORMAT (code);
2659   cselib_val *e;
2660   struct elt_list *l;
2661   rtx copy = x;
2662   int i;
2663
2664   switch (code)
2665     {
2666     case REG:
2667       i = REGNO (x);
2668       for (l = REG_VALUES (i); l; l = l->next)
2669         if (GET_MODE (l->elt->u.val_rtx) == GET_MODE (x))
2670           return l->elt->u.val_rtx;
2671       abort ();
2672
2673     case MEM:
2674       e = cselib_lookup_mem (x, 0);
2675       if (! e)
2676         abort ();
2677       return e->u.val_rtx;
2678
2679       /* CONST_DOUBLEs must be special-cased here so that we won't try to
2680          look up the CONST_DOUBLE_MEM inside.  */
2681     case CONST_DOUBLE:
2682     case CONST_INT:
2683       return x;
2684
2685     default:
2686       break;
2687     }
2688
2689   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2690     {
2691       if (fmt[i] == 'e')
2692         {
2693           rtx t = cselib_subst_to_values (XEXP (x, i));
2694           if (t != XEXP (x, i) && x == copy)
2695             copy = shallow_copy_rtx (x);
2696           XEXP (copy, i) = t;
2697         }
2698       else if (fmt[i] == 'E')
2699         {
2700           int j, k;
2701
2702           for (j = 0; j < XVECLEN (x, i); j++)
2703             {
2704               rtx t = cselib_subst_to_values (XVECEXP (x, i, j));
2705               if (t != XVECEXP (x, i, j) && XVEC (x, i) == XVEC (copy, i))
2706                 {
2707                   if (x == copy)
2708                     copy = shallow_copy_rtx (x);
2709                   XVEC (copy, i) = rtvec_alloc (XVECLEN (x, i));
2710                   for (k = 0; k < j; k++)
2711                     XVECEXP (copy, i, k) = XVECEXP (x, i, k);
2712                 }
2713               XVECEXP (copy, i, j) = t;
2714             }
2715         }
2716     }
2717   return copy;
2718 }
2719
2720 /* Look up the rtl expression X in our tables and return the value it has.
2721    If CREATE is zero, we return NULL if we don't know the value.  Otherwise,
2722    we create a new one if possible, using mode MODE if X doesn't have a mode
2723    (i.e. because it's a constant).  */
2724 cselib_val *
2725 cselib_lookup (x, mode, create)
2726      rtx x;
2727      enum machine_mode mode;
2728      int create;
2729 {
2730   void **slot;
2731   cselib_val *e;
2732   unsigned int hashval;
2733
2734   if (GET_MODE (x) != VOIDmode)
2735     mode = GET_MODE (x);
2736
2737   if (GET_CODE (x) == VALUE)
2738     return CSELIB_VAL_PTR (x);
2739
2740   if (GET_CODE (x) == REG)
2741     {
2742       struct elt_list *l;
2743       int i = REGNO (x);
2744       for (l = REG_VALUES (i); l; l = l->next)
2745         if (mode == GET_MODE (l->elt->u.val_rtx))
2746           return l->elt;
2747       if (! create)
2748         return 0;
2749       e = new_cselib_val (++next_unknown_value, GET_MODE (x));
2750       e->locs = new_elt_loc_list (e->locs, x);
2751       REG_VALUES (i) = new_elt_list (REG_VALUES (i), e);
2752       slot = htab_find_slot_with_hash (hash_table, x, e->value, 1);
2753       *slot = e;
2754       return e;
2755     }
2756
2757   if (GET_CODE (x) == MEM)
2758     return cselib_lookup_mem (x, create);
2759
2760   hashval = hash_rtx (x, mode, create);
2761   /* Can't even create if hashing is not possible.  */
2762   if (! hashval)
2763     return 0;
2764
2765   slot = htab_find_slot_with_hash (hash_table, x, hashval, create);
2766   if (slot == 0)
2767     return 0;
2768   e = (cselib_val *) *slot;
2769   if (e)
2770     return e;
2771
2772   e = new_cselib_val (hashval, mode);
2773   /* We have to fill the slot before calling cselib_subst_to_values:
2774      the hash table is inconsistent until we do so, and
2775      cselib_subst_to_values will need to do lookups.  */
2776   *slot = (void *) e;
2777   e->locs = new_elt_loc_list (e->locs, cselib_subst_to_values (x));
2778   return e;
2779 }
2780
2781 /* Invalidate any entries in reg_values that overlap REGNO.  This is called
2782    if REGNO is changing.  MODE is the mode of the assignment to REGNO, which
2783    is used to determine how many hard registers are being changed.  If MODE
2784    is VOIDmode, then only REGNO is being changed; this is used when
2785    invalidating call clobbered registers across a call.  */
2786
2787 static void
2788 cselib_invalidate_regno (regno, mode)
2789      unsigned int regno;
2790      enum machine_mode mode;
2791 {
2792   unsigned int endregno;
2793   unsigned int i;
2794
2795   /* If we see pseudos after reload, something is _wrong_.  */
2796   if (reload_completed && regno >= FIRST_PSEUDO_REGISTER
2797       && reg_renumber[regno] >= 0)
2798     abort ();
2799
2800   /* Determine the range of registers that must be invalidated.  For
2801      pseudos, only REGNO is affected.  For hard regs, we must take MODE
2802      into account, and we must also invalidate lower register numbers
2803      if they contain values that overlap REGNO.  */
2804   endregno = regno + 1;
2805   if (regno < FIRST_PSEUDO_REGISTER && mode != VOIDmode) 
2806     endregno = regno + HARD_REGNO_NREGS (regno, mode);
2807
2808   for (i = 0; i < endregno; i++)
2809     {
2810       struct elt_list **l = &REG_VALUES (i);
2811
2812       /* Go through all known values for this reg; if it overlaps the range
2813          we're invalidating, remove the value.  */
2814       while (*l)
2815         {
2816           cselib_val *v = (*l)->elt;
2817           struct elt_loc_list **p;
2818           unsigned int this_last = i;
2819
2820           if (i < FIRST_PSEUDO_REGISTER)
2821             this_last += HARD_REGNO_NREGS (i, GET_MODE (v->u.val_rtx)) - 1;
2822
2823           if (this_last < regno)
2824             {
2825               l = &(*l)->next;
2826               continue;
2827             }
2828
2829           /* We have an overlap.  */
2830           unchain_one_elt_list (l);
2831
2832           /* Now, we clear the mapping from value to reg.  It must exist, so
2833              this code will crash intentionally if it doesn't.  */
2834           for (p = &v->locs; ; p = &(*p)->next)
2835             {
2836               rtx x = (*p)->loc;
2837
2838               if (GET_CODE (x) == REG && REGNO (x) == i)
2839                 {
2840                   unchain_one_elt_loc_list (p);
2841                   break;
2842                 }
2843             }
2844           check_value_useless (v);
2845         }
2846     }
2847 }
2848
2849 /* The memory at address MEM_BASE is being changed.
2850    Return whether this change will invalidate VAL.  */
2851 static int
2852 cselib_mem_conflict_p (mem_base, val)
2853      rtx mem_base;
2854      rtx val;
2855 {
2856   enum rtx_code code;
2857   const char *fmt;
2858   int i;
2859
2860   code = GET_CODE (val);
2861   switch (code)
2862     {
2863       /* Get rid of a few simple cases quickly. */
2864     case REG:
2865     case PC:
2866     case CC0:
2867     case SCRATCH:
2868     case CONST:
2869     case CONST_INT:
2870     case CONST_DOUBLE:
2871     case SYMBOL_REF:
2872     case LABEL_REF:
2873       return 0;
2874
2875     case MEM:
2876       if (GET_MODE (mem_base) == BLKmode
2877           || GET_MODE (val) == BLKmode)
2878         return 1;
2879       if (anti_dependence (val, mem_base))
2880         return 1;
2881       /* The address may contain nested MEMs.  */
2882       break;
2883
2884     default:
2885       break;
2886     }
2887
2888   fmt = GET_RTX_FORMAT (code);
2889
2890   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2891     {
2892       if (fmt[i] == 'e')
2893         {
2894           if (cselib_mem_conflict_p (mem_base, XEXP (val, i)))
2895             return 1;
2896         }
2897       else if (fmt[i] == 'E')
2898         {
2899           int j;
2900
2901           for (j = 0; j < XVECLEN (val, i); j++)
2902             if (cselib_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
2903               return 1;
2904         }
2905     }
2906
2907   return 0;
2908 }
2909
2910 /* For the value found in SLOT, walk its locations to determine if any overlap
2911    INFO (which is a MEM rtx).  */
2912 static int
2913 cselib_invalidate_mem_1 (slot, info)
2914      void **slot;
2915      void *info;
2916 {
2917   cselib_val *v = (cselib_val *) *slot;
2918   rtx mem_rtx = (rtx) info;
2919   struct elt_loc_list **p = &v->locs;
2920
2921   while (*p)
2922     {
2923       cselib_val *addr;
2924       struct elt_list **mem_chain;
2925       rtx x = (*p)->loc;
2926
2927       /* MEMs may occur in locations only at the top level; below
2928          that every MEM or REG is substituted by its VALUE.  */
2929       if (GET_CODE (x) != MEM
2930           || ! cselib_mem_conflict_p (mem_rtx, x))
2931         {
2932           p = &(*p)->next;
2933           continue;
2934         }
2935
2936       /* This one overlaps.  */
2937       /* We must have a mapping from this MEM's address to the
2938          value (E).  Remove that, too.  */
2939       addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0);
2940       mem_chain = &addr->addr_list;
2941       for (;;)
2942         {
2943           if ((*mem_chain)->elt == v)
2944             {
2945               unchain_one_elt_list (mem_chain);
2946               break;
2947             }
2948           mem_chain = &(*mem_chain)->next;
2949         }
2950       unchain_one_elt_loc_list (p);
2951     }
2952   check_value_useless (v);
2953   return 1;
2954 }
2955
2956 /* Invalidate any locations in the table which are changed because of a
2957    store to MEM_RTX.  If this is called because of a non-const call
2958    instruction, MEM_RTX is (mem:BLK const0_rtx).  */
2959 static void
2960 cselib_invalidate_mem (mem_rtx)
2961      rtx mem_rtx;
2962 {
2963   htab_traverse (hash_table, cselib_invalidate_mem_1, mem_rtx);
2964 }
2965
2966 /* Invalidate DEST, which is being assigned to or clobbered.  The second and
2967    the third parameter exist so that this function can be passed to
2968    note_stores; they are ignored.  */
2969 static void
2970 cselib_invalidate_rtx (dest, ignore, data)
2971      rtx dest;
2972      rtx ignore ATTRIBUTE_UNUSED;
2973      void *data ATTRIBUTE_UNUSED;
2974 {
2975   while (GET_CODE (dest) == STRICT_LOW_PART
2976          || GET_CODE (dest) == SIGN_EXTRACT
2977          || GET_CODE (dest) == ZERO_EXTRACT
2978          || GET_CODE (dest) == SUBREG)
2979     dest = XEXP (dest, 0);
2980
2981   if (GET_CODE (dest) == REG)
2982     cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2983   else if (GET_CODE (dest) == MEM)
2984     cselib_invalidate_mem (dest);
2985
2986   /* Some machines don't define AUTO_INC_DEC, but they still use push
2987      instructions.  We need to catch that case here in order to
2988      invalidate the stack pointer correctly.  Note that invalidating
2989      the stack pointer is different from invalidating DEST.  */
2990   if (push_operand (dest, GET_MODE (dest)))
2991     cselib_invalidate_rtx (stack_pointer_rtx, NULL_RTX, NULL);
2992 }
2993
2994 /* Record the result of a SET instruction.  DEST is being set; the source
2995    contains the value described by SRC_ELT.  If DEST is a MEM, DEST_ADDR_ELT
2996    describes its address.  */
2997
2998 static void
2999 cselib_record_set (dest, src_elt, dest_addr_elt)
3000      rtx dest;
3001      cselib_val *src_elt, *dest_addr_elt;
3002 {
3003   int dreg = GET_CODE (dest) == REG ? (int) REGNO (dest) : -1;
3004
3005   if (src_elt == 0 || side_effects_p (dest))
3006     return;
3007
3008   if (dreg >= 0)
3009     {
3010       REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
3011       src_elt->locs = new_elt_loc_list (src_elt->locs, dest);
3012     }
3013   else if (GET_CODE (dest) == MEM && dest_addr_elt != 0)
3014     add_mem_for_addr (dest_addr_elt, src_elt, dest);
3015 }
3016
3017 /* Describe a single set that is part of an insn.  */
3018 struct set
3019 {
3020   rtx src;
3021   rtx dest;
3022   cselib_val *src_elt;
3023   cselib_val *dest_addr_elt;
3024 };
3025
3026 /* There is no good way to determine how many elements there can be
3027    in a PARALLEL.  Since it's fairly cheap, use a really large number.  */
3028 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
3029
3030 /* Record the effects of any sets in INSN.  */
3031 static void
3032 cselib_record_sets (insn)
3033      rtx insn;
3034 {
3035   int n_sets = 0;
3036   int i;
3037   struct set sets[MAX_SETS];
3038   rtx body = PATTERN (insn);
3039
3040   body = PATTERN (insn);
3041   /* Find all sets.  */
3042   if (GET_CODE (body) == SET)
3043     {
3044       sets[0].src = SET_SRC (body);
3045       sets[0].dest = SET_DEST (body);
3046       n_sets = 1;
3047     }
3048   else if (GET_CODE (body) == PARALLEL)
3049     {
3050       /* Look through the PARALLEL and record the values being
3051          set, if possible.  Also handle any CLOBBERs.  */
3052       for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
3053         {
3054           rtx x = XVECEXP (body, 0, i);
3055
3056           if (GET_CODE (x) == SET)
3057             {
3058               sets[n_sets].src = SET_SRC (x);
3059               sets[n_sets].dest = SET_DEST (x);
3060               n_sets++;
3061             }
3062         }
3063     }
3064
3065   /* Look up the values that are read.  Do this before invalidating the
3066      locations that are written.  */
3067   for (i = 0; i < n_sets; i++)
3068     {
3069       sets[i].src_elt = cselib_lookup (sets[i].src, GET_MODE (sets[i].dest),
3070                                        1);
3071       if (GET_CODE (sets[i].dest) == MEM)
3072         sets[i].dest_addr_elt = cselib_lookup (XEXP (sets[i].dest, 0), Pmode,
3073                                                1);
3074       else
3075         sets[i].dest_addr_elt = 0;
3076     }
3077
3078   /* Invalidate all locations written by this insn.  Note that the elts we
3079      looked up in the previous loop aren't affected, just some of their
3080      locations may go away.  */
3081   note_stores (body, cselib_invalidate_rtx, NULL);
3082
3083   /* Now enter the equivalences in our tables.  */
3084   for (i = 0; i < n_sets; i++)
3085     cselib_record_set (sets[i].dest, sets[i].src_elt, sets[i].dest_addr_elt);
3086 }
3087
3088 /* Record the effects of INSN.  */
3089 void
3090 cselib_process_insn (insn)
3091      rtx insn;
3092 {
3093   int i;
3094
3095   cselib_current_insn = insn;
3096
3097   /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp.  */
3098   if (GET_CODE (insn) == CODE_LABEL
3099       || (GET_CODE (insn) == NOTE
3100           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
3101       || (GET_CODE (insn) == INSN
3102           && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
3103           && MEM_VOLATILE_P (PATTERN (insn))))
3104     {
3105       clear_table ();
3106       return;
3107     }
3108
3109   if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
3110     {
3111       cselib_current_insn = 0;
3112       return;
3113     }
3114   /* If this is a call instruction, forget anything stored in a
3115      call clobbered register, or, if this is not a const call, in
3116      memory.  */
3117   if (GET_CODE (insn) == CALL_INSN)
3118     {
3119       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3120         if (call_used_regs[i])
3121           cselib_invalidate_regno (i, VOIDmode);
3122
3123       if (! CONST_CALL_P (insn))
3124         cselib_invalidate_mem (callmem);
3125     }
3126
3127   cselib_record_sets (insn);
3128
3129 #ifdef AUTO_INC_DEC
3130   /* Clobber any registers which appear in REG_INC notes.  We
3131      could keep track of the changes to their values, but it is
3132      unlikely to help.  */
3133   {
3134     rtx x;
3135
3136     for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
3137       if (REG_NOTE_KIND (x) == REG_INC)
3138         cselib_invalidate_rtx (XEXP (x, 0), NULL_RTX, NULL);
3139   }
3140 #endif
3141
3142   /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
3143      after we have processed the insn.  */
3144   if (GET_CODE (insn) == CALL_INSN)
3145     {
3146       rtx x;
3147
3148       for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
3149         if (GET_CODE (XEXP (x, 0)) == CLOBBER)
3150           cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX,
3151                                      NULL);
3152     }
3153
3154   cselib_current_insn = 0;
3155
3156   if (n_useless_values > MAX_USELESS_VALUES)
3157     remove_useless_values ();
3158 }
3159
3160 /* Make sure our varrays are big enough.  Not called from any cselib routines;
3161    it must be called by the user if it allocated new registers.  */
3162 void
3163 cselib_update_varray_sizes ()
3164 {
3165   int nregs = max_reg_num ();
3166   if (nregs == cselib_nregs)
3167     return;
3168   cselib_nregs = nregs;
3169   VARRAY_GROW (reg_values, nregs);
3170 }
3171
3172 /* Initialize cselib for one pass.  The caller must also call
3173    init_alias_analysis.  */
3174 void
3175 cselib_init ()
3176 {
3177   /* These are only created once.  */
3178   if (! callmem)
3179     {
3180       extern struct obstack permanent_obstack;
3181       gcc_obstack_init (&cselib_obstack);
3182       cselib_startobj = obstack_alloc (&cselib_obstack, 0);
3183
3184       push_obstacks (&permanent_obstack, &permanent_obstack);
3185       callmem = gen_rtx_MEM (BLKmode, const0_rtx);
3186       pop_obstacks ();
3187       ggc_add_rtx_root (&callmem, 1);
3188     }
3189
3190   cselib_nregs = max_reg_num ();
3191   VARRAY_ELT_LIST_INIT (reg_values, cselib_nregs, "reg_values");
3192   hash_table = htab_create (31, get_value_hash, entry_and_rtx_equal_p, NULL);
3193   clear_table ();
3194 }
3195
3196 /* Called when the current user is done with cselib.  */
3197 void
3198 cselib_finish ()
3199 {
3200   clear_table ();
3201   htab_delete (hash_table);
3202 }