OSDN Git Service

Brad's -ffast-math breakup.
[pf3gnuchains/gcc-fork.git] / gcc / simplify-rtx.c
1 /* RTL simplification functions for GNU compiler.
2    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001 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 #include "system.h"
25 #include <setjmp.h>
26
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "flags.h"
32 #include "real.h"
33 #include "insn-config.h"
34 #include "recog.h"
35 #include "function.h"
36 #include "expr.h"
37 #include "toplev.h"
38 #include "output.h"
39 #include "ggc.h"
40
41 /* Simplification and canonicalization of RTL.  */
42
43 /* Nonzero if X has the form (PLUS frame-pointer integer).  We check for
44    virtual regs here because the simplify_*_operation routines are called
45    by integrate.c, which is called before virtual register instantiation.
46
47    ?!? FIXED_BASE_PLUS_P and NONZERO_BASE_PLUS_P need to move into 
48    a header file so that their definitions can be shared with the
49    simplification routines in simplify-rtx.c.  Until then, do not
50    change these macros without also changing the copy in simplify-rtx.c.  */
51
52 #define FIXED_BASE_PLUS_P(X)                                    \
53   ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx    \
54    || ((X) == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])\
55    || (X) == virtual_stack_vars_rtx                             \
56    || (X) == virtual_incoming_args_rtx                          \
57    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
58        && (XEXP (X, 0) == frame_pointer_rtx                     \
59            || XEXP (X, 0) == hard_frame_pointer_rtx             \
60            || ((X) == arg_pointer_rtx                           \
61                && fixed_regs[ARG_POINTER_REGNUM])               \
62            || XEXP (X, 0) == virtual_stack_vars_rtx             \
63            || XEXP (X, 0) == virtual_incoming_args_rtx))        \
64    || GET_CODE (X) == ADDRESSOF)
65
66 /* Similar, but also allows reference to the stack pointer.
67
68    This used to include FIXED_BASE_PLUS_P, however, we can't assume that
69    arg_pointer_rtx by itself is nonzero, because on at least one machine,
70    the i960, the arg pointer is zero when it is unused.  */
71
72 #define NONZERO_BASE_PLUS_P(X)                                  \
73   ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx    \
74    || (X) == virtual_stack_vars_rtx                             \
75    || (X) == virtual_incoming_args_rtx                          \
76    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
77        && (XEXP (X, 0) == frame_pointer_rtx                     \
78            || XEXP (X, 0) == hard_frame_pointer_rtx             \
79            || ((X) == arg_pointer_rtx                           \
80                && fixed_regs[ARG_POINTER_REGNUM])               \
81            || XEXP (X, 0) == virtual_stack_vars_rtx             \
82            || XEXP (X, 0) == virtual_incoming_args_rtx))        \
83    || (X) == stack_pointer_rtx                                  \
84    || (X) == virtual_stack_dynamic_rtx                          \
85    || (X) == virtual_outgoing_args_rtx                          \
86    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
87        && (XEXP (X, 0) == stack_pointer_rtx                     \
88            || XEXP (X, 0) == virtual_stack_dynamic_rtx          \
89            || XEXP (X, 0) == virtual_outgoing_args_rtx))        \
90    || GET_CODE (X) == ADDRESSOF)
91
92 /* Much code operates on (low, high) pairs; the low value is an
93    unsigned wide int, the high value a signed wide int.  We
94    occasionally need to sign extend from low to high as if low were a
95    signed wide int.  */
96 #define HWI_SIGN_EXTEND(low) \
97  ((((HOST_WIDE_INT) low) < 0) ? ((HOST_WIDE_INT) -1) : ((HOST_WIDE_INT) 0))
98
99 static rtx simplify_plus_minus          PARAMS ((enum rtx_code,
100                                                  enum machine_mode, rtx, rtx));
101 static void check_fold_consts           PARAMS ((PTR));
102 \f
103 /* Make a binary operation by properly ordering the operands and 
104    seeing if the expression folds.  */
105
106 rtx
107 simplify_gen_binary (code, mode, op0, op1)
108      enum rtx_code code;
109      enum machine_mode mode;
110      rtx op0, op1;
111 {
112   rtx tem;
113
114   /* Put complex operands first and constants second if commutative.  */
115   if (GET_RTX_CLASS (code) == 'c'
116       && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
117           || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
118               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
119           || (GET_CODE (op0) == SUBREG
120               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
121               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
122     tem = op0, op0 = op1, op1 = tem;
123
124   /* If this simplifies, do it.  */
125   tem = simplify_binary_operation (code, mode, op0, op1);
126
127   if (tem)
128     return tem;
129
130   /* Handle addition and subtraction of CONST_INT specially.  Otherwise,
131      just form the operation.  */
132
133   if (code == PLUS && GET_CODE (op1) == CONST_INT
134       && GET_MODE (op0) != VOIDmode)
135     return plus_constant (op0, INTVAL (op1));
136   else if (code == MINUS && GET_CODE (op1) == CONST_INT
137            && GET_MODE (op0) != VOIDmode)
138     return plus_constant (op0, - INTVAL (op1));
139   else
140     return gen_rtx_fmt_ee (code, mode, op0, op1);
141 }
142 \f
143 /* Make a unary operation by first seeing if it folds and otherwise making
144    the specified operation.  */
145
146 rtx
147 simplify_gen_unary (code, mode, op, op_mode)
148      enum rtx_code code;
149      enum machine_mode mode;
150      rtx op;
151      enum machine_mode op_mode;
152 {
153   rtx tem;
154
155   /* If this simplifies, use it.  */
156   if ((tem = simplify_unary_operation (code, mode, op, op_mode)) != 0)
157     return tem;
158
159   return gen_rtx_fmt_e (code, mode, op);
160 }
161
162 /* Likewise for ternary operations.  */
163
164 rtx
165 simplify_gen_ternary (code, mode, op0_mode, op0, op1, op2)
166      enum rtx_code code;
167      enum machine_mode mode, op0_mode;
168      rtx op0, op1, op2;
169 {
170   rtx tem;
171
172   /* If this simplifies, use it.  */
173   if (0 != (tem = simplify_ternary_operation (code, mode, op0_mode,
174                                               op0, op1, op2)))
175     return tem;
176
177   return gen_rtx_fmt_eee (code, mode, op0, op1, op2);
178 }
179 \f
180 /* Likewise, for relational operations.  */
181
182 rtx
183 simplify_gen_relational (code, mode, op0, op1)
184      enum rtx_code code;
185      enum machine_mode mode;
186      rtx op0, op1;
187 {
188   rtx tem;
189
190   if ((tem = simplify_relational_operation (code, mode, op0, op1)) != 0)
191     return tem;
192
193   /* Put complex operands first and constants second.  */
194   if ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
195       || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
196           && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
197       || (GET_CODE (op0) == SUBREG
198           && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
199           && GET_RTX_CLASS (GET_CODE (op1)) != 'o'))
200     tem = op0, op0 = op1, op1 = tem, code = swap_condition (code);
201
202   return gen_rtx_fmt_ee (code, mode, op0, op1);
203 }
204 \f
205 /* Replace all occurrences of OLD in X with NEW and try to simplify the
206    resulting RTX.  Return a new RTX which is as simplified as possible.  */
207
208 rtx
209 simplify_replace_rtx (x, old, new)
210      rtx x;
211      rtx old;
212      rtx new;
213 {
214   enum rtx_code code = GET_CODE (x);
215   enum machine_mode mode = GET_MODE (x);
216
217   /* If X is OLD, return NEW.  Otherwise, if this is an expression, try
218      to build a new expression substituting recursively.  If we can't do
219      anything, return our input.  */
220
221   if (x == old)
222     return new;
223
224   switch (GET_RTX_CLASS (code))
225     {
226     case '1':
227       {
228         enum machine_mode op_mode = GET_MODE (XEXP (x, 0));
229         rtx op = (XEXP (x, 0) == old
230                   ? new : simplify_replace_rtx (XEXP (x, 0), old, new));
231
232         return simplify_gen_unary (code, mode, op, op_mode);
233       }
234
235     case '2':
236     case 'c':
237       return
238         simplify_gen_binary (code, mode,
239                              simplify_replace_rtx (XEXP (x, 0), old, new),
240                              simplify_replace_rtx (XEXP (x, 1), old, new));
241
242     case '3':
243     case 'b':
244       return
245         simplify_gen_ternary (code, mode, GET_MODE (XEXP (x, 0)),
246                               simplify_replace_rtx (XEXP (x, 0), old, new),
247                               simplify_replace_rtx (XEXP (x, 1), old, new),
248                               simplify_replace_rtx (XEXP (x, 2), old, new));
249
250     case 'x':
251       /* The only case we try to handle is a lowpart SUBREG of a single-word
252          CONST_INT.  */
253       if (code == SUBREG && subreg_lowpart_p (x) && old == SUBREG_REG (x)
254           && GET_CODE (new) == CONST_INT
255           && GET_MODE_SIZE (GET_MODE (old)) <= UNITS_PER_WORD)
256         return GEN_INT (INTVAL (new) & GET_MODE_MASK (mode));
257
258       return x;
259
260     default:
261       return x;
262     }
263 }
264 \f
265 /* Try to simplify a unary operation CODE whose output mode is to be
266    MODE with input operand OP whose mode was originally OP_MODE.
267    Return zero if no simplification can be made.  */
268
269 rtx
270 simplify_unary_operation (code, mode, op, op_mode)
271      enum rtx_code code;
272      enum machine_mode mode;
273      rtx op;
274      enum machine_mode op_mode;
275 {
276   unsigned int width = GET_MODE_BITSIZE (mode);
277
278   /* The order of these tests is critical so that, for example, we don't
279      check the wrong mode (input vs. output) for a conversion operation,
280      such as FIX.  At some point, this should be simplified.  */
281
282 #if !defined(REAL_IS_NOT_DOUBLE) || defined(REAL_ARITHMETIC)
283
284   if (code == FLOAT && GET_MODE (op) == VOIDmode
285       && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
286     {
287       HOST_WIDE_INT hv, lv;
288       REAL_VALUE_TYPE d;
289
290       if (GET_CODE (op) == CONST_INT)
291         lv = INTVAL (op), hv = HWI_SIGN_EXTEND (lv);
292       else
293         lv = CONST_DOUBLE_LOW (op),  hv = CONST_DOUBLE_HIGH (op);
294
295 #ifdef REAL_ARITHMETIC
296       REAL_VALUE_FROM_INT (d, lv, hv, mode);
297 #else
298       if (hv < 0)
299         {
300           d = (double) (~ hv);
301           d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
302                 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
303           d += (double) (unsigned HOST_WIDE_INT) (~ lv);
304           d = (- d - 1.0);
305         }
306       else
307         {
308           d = (double) hv;
309           d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
310                 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
311           d += (double) (unsigned HOST_WIDE_INT) lv;
312         }
313 #endif  /* REAL_ARITHMETIC */
314       d = real_value_truncate (mode, d);
315       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
316     }
317   else if (code == UNSIGNED_FLOAT && GET_MODE (op) == VOIDmode
318            && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
319     {
320       HOST_WIDE_INT hv, lv;
321       REAL_VALUE_TYPE d;
322
323       if (GET_CODE (op) == CONST_INT)
324         lv = INTVAL (op), hv = HWI_SIGN_EXTEND (lv);
325       else
326         lv = CONST_DOUBLE_LOW (op),  hv = CONST_DOUBLE_HIGH (op);
327
328       if (op_mode == VOIDmode)
329         {
330           /* We don't know how to interpret negative-looking numbers in
331              this case, so don't try to fold those.  */
332           if (hv < 0)
333             return 0;
334         }
335       else if (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT * 2)
336         ;
337       else
338         hv = 0, lv &= GET_MODE_MASK (op_mode);
339
340 #ifdef REAL_ARITHMETIC
341       REAL_VALUE_FROM_UNSIGNED_INT (d, lv, hv, mode);
342 #else
343
344       d = (double) (unsigned HOST_WIDE_INT) hv;
345       d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
346             * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
347       d += (double) (unsigned HOST_WIDE_INT) lv;
348 #endif  /* REAL_ARITHMETIC */
349       d = real_value_truncate (mode, d);
350       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
351     }
352 #endif
353
354   if (GET_CODE (op) == CONST_INT
355       && width <= HOST_BITS_PER_WIDE_INT && width > 0)
356     {
357       register HOST_WIDE_INT arg0 = INTVAL (op);
358       register HOST_WIDE_INT val;
359
360       switch (code)
361         {
362         case NOT:
363           val = ~ arg0;
364           break;
365
366         case NEG:
367           val = - arg0;
368           break;
369
370         case ABS:
371           val = (arg0 >= 0 ? arg0 : - arg0);
372           break;
373
374         case FFS:
375           /* Don't use ffs here.  Instead, get low order bit and then its
376              number.  If arg0 is zero, this will return 0, as desired.  */
377           arg0 &= GET_MODE_MASK (mode);
378           val = exact_log2 (arg0 & (- arg0)) + 1;
379           break;
380
381         case TRUNCATE:
382           val = arg0;
383           break;
384
385         case ZERO_EXTEND:
386           if (op_mode == VOIDmode)
387             op_mode = mode;
388           if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
389             {
390               /* If we were really extending the mode,
391                  we would have to distinguish between zero-extension
392                  and sign-extension.  */
393               if (width != GET_MODE_BITSIZE (op_mode))
394                 abort ();
395               val = arg0;
396             }
397           else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
398             val = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
399           else
400             return 0;
401           break;
402
403         case SIGN_EXTEND:
404           if (op_mode == VOIDmode)
405             op_mode = mode;
406           if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
407             {
408               /* If we were really extending the mode,
409                  we would have to distinguish between zero-extension
410                  and sign-extension.  */
411               if (width != GET_MODE_BITSIZE (op_mode))
412                 abort ();
413               val = arg0;
414             }
415           else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
416             {
417               val
418                 = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
419               if (val
420                   & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (op_mode) - 1)))
421                 val -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
422             }
423           else
424             return 0;
425           break;
426
427         case SQRT:
428         case FLOAT_EXTEND:
429         case FLOAT_TRUNCATE:
430           return 0;
431
432         default:
433           abort ();
434         }
435
436       val = trunc_int_for_mode (val, mode);
437
438       return GEN_INT (val);
439     }
440
441   /* We can do some operations on integer CONST_DOUBLEs.  Also allow
442      for a DImode operation on a CONST_INT.  */
443   else if (GET_MODE (op) == VOIDmode && width <= HOST_BITS_PER_INT * 2
444            && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
445     {
446       unsigned HOST_WIDE_INT l1, lv;
447       HOST_WIDE_INT h1, hv;
448
449       if (GET_CODE (op) == CONST_DOUBLE)
450         l1 = CONST_DOUBLE_LOW (op), h1 = CONST_DOUBLE_HIGH (op);
451       else
452         l1 = INTVAL (op), h1 = HWI_SIGN_EXTEND (l1);
453
454       switch (code)
455         {
456         case NOT:
457           lv = ~ l1;
458           hv = ~ h1;
459           break;
460
461         case NEG:
462           neg_double (l1, h1, &lv, &hv);
463           break;
464
465         case ABS:
466           if (h1 < 0)
467             neg_double (l1, h1, &lv, &hv);
468           else
469             lv = l1, hv = h1;
470           break;
471
472         case FFS:
473           hv = 0;
474           if (l1 == 0)
475             lv = HOST_BITS_PER_WIDE_INT + exact_log2 (h1 & (-h1)) + 1;
476           else
477             lv = exact_log2 (l1 & (-l1)) + 1;
478           break;
479
480         case TRUNCATE:
481           /* This is just a change-of-mode, so do nothing.  */
482           lv = l1, hv = h1;
483           break;
484
485         case ZERO_EXTEND:
486           if (op_mode == VOIDmode
487               || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
488             return 0;
489
490           hv = 0;
491           lv = l1 & GET_MODE_MASK (op_mode);
492           break;
493
494         case SIGN_EXTEND:
495           if (op_mode == VOIDmode
496               || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
497             return 0;
498           else
499             {
500               lv = l1 & GET_MODE_MASK (op_mode);
501               if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
502                   && (lv & ((HOST_WIDE_INT) 1
503                             << (GET_MODE_BITSIZE (op_mode) - 1))) != 0)
504                 lv -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
505
506               hv = HWI_SIGN_EXTEND (lv);
507             }
508           break;
509
510         case SQRT:
511           return 0;
512
513         default:
514           return 0;
515         }
516
517       return immed_double_const (lv, hv, mode);
518     }
519
520 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
521   else if (GET_CODE (op) == CONST_DOUBLE
522            && GET_MODE_CLASS (mode) == MODE_FLOAT)
523     {
524       REAL_VALUE_TYPE d;
525       jmp_buf handler;
526       rtx x;
527
528       if (setjmp (handler))
529         /* There used to be a warning here, but that is inadvisable.
530            People may want to cause traps, and the natural way
531            to do it should not get a warning.  */
532         return 0;
533
534       set_float_handler (handler);
535
536       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
537
538       switch (code)
539         {
540         case NEG:
541           d = REAL_VALUE_NEGATE (d);
542           break;
543
544         case ABS:
545           if (REAL_VALUE_NEGATIVE (d))
546             d = REAL_VALUE_NEGATE (d);
547           break;
548
549         case FLOAT_TRUNCATE:
550           d = real_value_truncate (mode, d);
551           break;
552
553         case FLOAT_EXTEND:
554           /* All this does is change the mode.  */
555           break;
556
557         case FIX:
558           d = REAL_VALUE_RNDZINT (d);
559           break;
560
561         case UNSIGNED_FIX:
562           d = REAL_VALUE_UNSIGNED_RNDZINT (d);
563           break;
564
565         case SQRT:
566           return 0;
567
568         default:
569           abort ();
570         }
571
572       x = CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
573       set_float_handler (NULL_PTR);
574       return x;
575     }
576
577   else if (GET_CODE (op) == CONST_DOUBLE
578            && GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT
579            && GET_MODE_CLASS (mode) == MODE_INT
580            && width <= HOST_BITS_PER_WIDE_INT && width > 0)
581     {
582       REAL_VALUE_TYPE d;
583       jmp_buf handler;
584       HOST_WIDE_INT val;
585
586       if (setjmp (handler))
587         return 0;
588
589       set_float_handler (handler);
590
591       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
592
593       switch (code)
594         {
595         case FIX:
596           val = REAL_VALUE_FIX (d);
597           break;
598
599         case UNSIGNED_FIX:
600           val = REAL_VALUE_UNSIGNED_FIX (d);
601           break;
602
603         default:
604           abort ();
605         }
606
607       set_float_handler (NULL_PTR);
608
609       val = trunc_int_for_mode (val, mode);
610
611       return GEN_INT (val);
612     }
613 #endif
614   /* This was formerly used only for non-IEEE float.
615      eggert@twinsun.com says it is safe for IEEE also.  */
616   else
617     {
618       enum rtx_code reversed;
619       /* There are some simplifications we can do even if the operands
620          aren't constant.  */
621       switch (code)
622         {
623         case NOT:
624           /* (not (not X)) == X.  */
625           if (GET_CODE (op) == NOT)
626             return XEXP (op, 0);
627
628           /* (not (eq X Y)) == (ne X Y), etc.  */
629           if (mode == BImode && GET_RTX_CLASS (GET_CODE (op)) == '<'
630               && ((reversed = reversed_comparison_code (op, NULL_RTX))
631                   != UNKNOWN))
632             return gen_rtx_fmt_ee (reversed,
633                                    op_mode, XEXP (op, 0), XEXP (op, 1));
634           break;
635
636         case NEG:
637           /* (neg (neg X)) == X.  */
638           if (GET_CODE (op) == NEG)
639             return XEXP (op, 0);
640           break;
641
642         case SIGN_EXTEND:
643           /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
644              becomes just the MINUS if its mode is MODE.  This allows
645              folding switch statements on machines using casesi (such as
646              the Vax).  */
647           if (GET_CODE (op) == TRUNCATE
648               && GET_MODE (XEXP (op, 0)) == mode
649               && GET_CODE (XEXP (op, 0)) == MINUS
650               && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
651               && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
652             return XEXP (op, 0);
653
654 #ifdef POINTERS_EXTEND_UNSIGNED
655           if (! POINTERS_EXTEND_UNSIGNED
656               && mode == Pmode && GET_MODE (op) == ptr_mode
657               && (CONSTANT_P (op)
658                   || (GET_CODE (op) == SUBREG
659                       && GET_CODE (SUBREG_REG (op)) == REG
660                       && REG_POINTER (SUBREG_REG (op))
661                       && GET_MODE (SUBREG_REG (op)) == Pmode)))
662             return convert_memory_address (Pmode, op);
663 #endif
664           break;
665
666 #ifdef POINTERS_EXTEND_UNSIGNED
667         case ZERO_EXTEND:
668           if (POINTERS_EXTEND_UNSIGNED
669               && mode == Pmode && GET_MODE (op) == ptr_mode
670               && (CONSTANT_P (op)
671                   || (GET_CODE (op) == SUBREG
672                       && GET_CODE (SUBREG_REG (op)) == REG
673                       && REG_POINTER (SUBREG_REG (op))
674                       && GET_MODE (SUBREG_REG (op)) == Pmode)))
675             return convert_memory_address (Pmode, op);
676           break;
677 #endif
678           
679         default:
680           break;
681         }
682
683       return 0;
684     }
685 }
686 \f
687 /* Simplify a binary operation CODE with result mode MODE, operating on OP0
688    and OP1.  Return 0 if no simplification is possible.
689
690    Don't use this for relational operations such as EQ or LT.
691    Use simplify_relational_operation instead.  */
692
693 rtx
694 simplify_binary_operation (code, mode, op0, op1)
695      enum rtx_code code;
696      enum machine_mode mode;
697      rtx op0, op1;
698 {
699   register HOST_WIDE_INT arg0, arg1, arg0s, arg1s;
700   HOST_WIDE_INT val;
701   unsigned int width = GET_MODE_BITSIZE (mode);
702   rtx tem;
703
704   /* Relational operations don't work here.  We must know the mode
705      of the operands in order to do the comparison correctly.
706      Assuming a full word can give incorrect results.
707      Consider comparing 128 with -128 in QImode.  */
708
709   if (GET_RTX_CLASS (code) == '<')
710     abort ();
711
712 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
713   if (GET_MODE_CLASS (mode) == MODE_FLOAT
714       && GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE
715       && mode == GET_MODE (op0) && mode == GET_MODE (op1))
716     {
717       REAL_VALUE_TYPE f0, f1, value;
718       jmp_buf handler;
719
720       if (setjmp (handler))
721         return 0;
722
723       set_float_handler (handler);
724
725       REAL_VALUE_FROM_CONST_DOUBLE (f0, op0);
726       REAL_VALUE_FROM_CONST_DOUBLE (f1, op1);
727       f0 = real_value_truncate (mode, f0);
728       f1 = real_value_truncate (mode, f1);
729
730 #ifdef REAL_ARITHMETIC
731 #ifndef REAL_INFINITY
732       if (code == DIV && REAL_VALUES_EQUAL (f1, dconst0))
733         return 0;
734 #endif
735       REAL_ARITHMETIC (value, rtx_to_tree_code (code), f0, f1);
736 #else
737       switch (code)
738         {
739         case PLUS:
740           value = f0 + f1;
741           break;
742         case MINUS:
743           value = f0 - f1;
744           break;
745         case MULT:
746           value = f0 * f1;
747           break;
748         case DIV:
749 #ifndef REAL_INFINITY
750           if (f1 == 0)
751             return 0;
752 #endif
753           value = f0 / f1;
754           break;
755         case SMIN:
756           value = MIN (f0, f1);
757           break;
758         case SMAX:
759           value = MAX (f0, f1);
760           break;
761         default:
762           abort ();
763         }
764 #endif
765
766       value = real_value_truncate (mode, value);
767       set_float_handler (NULL_PTR);
768       return CONST_DOUBLE_FROM_REAL_VALUE (value, mode);
769     }
770 #endif  /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
771
772   /* We can fold some multi-word operations.  */
773   if (GET_MODE_CLASS (mode) == MODE_INT
774       && width == HOST_BITS_PER_WIDE_INT * 2
775       && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT)
776       && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT))
777     {
778       unsigned HOST_WIDE_INT l1, l2, lv;
779       HOST_WIDE_INT h1, h2, hv;
780
781       if (GET_CODE (op0) == CONST_DOUBLE)
782         l1 = CONST_DOUBLE_LOW (op0), h1 = CONST_DOUBLE_HIGH (op0);
783       else
784         l1 = INTVAL (op0), h1 = HWI_SIGN_EXTEND (l1);
785
786       if (GET_CODE (op1) == CONST_DOUBLE)
787         l2 = CONST_DOUBLE_LOW (op1), h2 = CONST_DOUBLE_HIGH (op1);
788       else
789         l2 = INTVAL (op1), h2 = HWI_SIGN_EXTEND (l2);
790
791       switch (code)
792         {
793         case MINUS:
794           /* A - B == A + (-B).  */
795           neg_double (l2, h2, &lv, &hv);
796           l2 = lv, h2 = hv;
797
798           /* .. fall through ...  */
799
800         case PLUS:
801           add_double (l1, h1, l2, h2, &lv, &hv);
802           break;
803
804         case MULT:
805           mul_double (l1, h1, l2, h2, &lv, &hv);
806           break;
807
808         case DIV:  case MOD:   case UDIV:  case UMOD:
809           /* We'd need to include tree.h to do this and it doesn't seem worth
810              it.  */
811           return 0;
812
813         case AND:
814           lv = l1 & l2, hv = h1 & h2;
815           break;
816
817         case IOR:
818           lv = l1 | l2, hv = h1 | h2;
819           break;
820
821         case XOR:
822           lv = l1 ^ l2, hv = h1 ^ h2;
823           break;
824
825         case SMIN:
826           if (h1 < h2
827               || (h1 == h2
828                   && ((unsigned HOST_WIDE_INT) l1
829                       < (unsigned HOST_WIDE_INT) l2)))
830             lv = l1, hv = h1;
831           else
832             lv = l2, hv = h2;
833           break;
834
835         case SMAX:
836           if (h1 > h2
837               || (h1 == h2
838                   && ((unsigned HOST_WIDE_INT) l1
839                       > (unsigned HOST_WIDE_INT) l2)))
840             lv = l1, hv = h1;
841           else
842             lv = l2, hv = h2;
843           break;
844
845         case UMIN:
846           if ((unsigned HOST_WIDE_INT) h1 < (unsigned HOST_WIDE_INT) h2
847               || (h1 == h2
848                   && ((unsigned HOST_WIDE_INT) l1
849                       < (unsigned HOST_WIDE_INT) l2)))
850             lv = l1, hv = h1;
851           else
852             lv = l2, hv = h2;
853           break;
854
855         case UMAX:
856           if ((unsigned HOST_WIDE_INT) h1 > (unsigned HOST_WIDE_INT) h2
857               || (h1 == h2
858                   && ((unsigned HOST_WIDE_INT) l1
859                       > (unsigned HOST_WIDE_INT) l2)))
860             lv = l1, hv = h1;
861           else
862             lv = l2, hv = h2;
863           break;
864
865         case LSHIFTRT:   case ASHIFTRT:
866         case ASHIFT:
867         case ROTATE:     case ROTATERT:
868 #ifdef SHIFT_COUNT_TRUNCATED
869           if (SHIFT_COUNT_TRUNCATED)
870             l2 &= (GET_MODE_BITSIZE (mode) - 1), h2 = 0;
871 #endif
872
873           if (h2 != 0 || l2 >= GET_MODE_BITSIZE (mode))
874             return 0;
875
876           if (code == LSHIFTRT || code == ASHIFTRT)
877             rshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv,
878                            code == ASHIFTRT);
879           else if (code == ASHIFT)
880             lshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv, 1);
881           else if (code == ROTATE)
882             lrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
883           else /* code == ROTATERT */
884             rrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
885           break;
886
887         default:
888           return 0;
889         }
890
891       return immed_double_const (lv, hv, mode);
892     }
893
894   if (GET_CODE (op0) != CONST_INT || GET_CODE (op1) != CONST_INT
895       || width > HOST_BITS_PER_WIDE_INT || width == 0)
896     {
897       /* Even if we can't compute a constant result,
898          there are some cases worth simplifying.  */
899
900       switch (code)
901         {
902         case PLUS:
903           /* In IEEE floating point, x+0 is not the same as x.  Similarly
904              for the other optimizations below.  */
905           if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
906               && FLOAT_MODE_P (mode) && ! flag_unsafe_math_optimizations)
907             break;
908
909           if (op1 == CONST0_RTX (mode))
910             return op0;
911
912           /* ((-a) + b) -> (b - a) and similarly for (a + (-b)) */
913           if (GET_CODE (op0) == NEG)
914             return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
915           else if (GET_CODE (op1) == NEG)
916             return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
917
918           /* Handle both-operands-constant cases.  We can only add
919              CONST_INTs to constants since the sum of relocatable symbols
920              can't be handled by most assemblers.  Don't add CONST_INT
921              to CONST_INT since overflow won't be computed properly if wider
922              than HOST_BITS_PER_WIDE_INT.  */
923
924           if (CONSTANT_P (op0) && GET_MODE (op0) != VOIDmode
925               && GET_CODE (op1) == CONST_INT)
926             return plus_constant (op0, INTVAL (op1));
927           else if (CONSTANT_P (op1) && GET_MODE (op1) != VOIDmode
928                    && GET_CODE (op0) == CONST_INT)
929             return plus_constant (op1, INTVAL (op0));
930
931           /* See if this is something like X * C - X or vice versa or
932              if the multiplication is written as a shift.  If so, we can
933              distribute and make a new multiply, shift, or maybe just
934              have X (if C is 2 in the example above).  But don't make
935              real multiply if we didn't have one before.  */
936
937           if (! FLOAT_MODE_P (mode))
938             {
939               HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
940               rtx lhs = op0, rhs = op1;
941               int had_mult = 0;
942
943               if (GET_CODE (lhs) == NEG)
944                 coeff0 = -1, lhs = XEXP (lhs, 0);
945               else if (GET_CODE (lhs) == MULT
946                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
947                 {
948                   coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
949                   had_mult = 1;
950                 }
951               else if (GET_CODE (lhs) == ASHIFT
952                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT
953                        && INTVAL (XEXP (lhs, 1)) >= 0
954                        && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
955                 {
956                   coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
957                   lhs = XEXP (lhs, 0);
958                 }
959
960               if (GET_CODE (rhs) == NEG)
961                 coeff1 = -1, rhs = XEXP (rhs, 0);
962               else if (GET_CODE (rhs) == MULT
963                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
964                 {
965                   coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
966                   had_mult = 1;
967                 }
968               else if (GET_CODE (rhs) == ASHIFT
969                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT
970                        && INTVAL (XEXP (rhs, 1)) >= 0
971                        && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
972                 {
973                   coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
974                   rhs = XEXP (rhs, 0);
975                 }
976
977               if (rtx_equal_p (lhs, rhs))
978                 {
979                   tem = simplify_gen_binary (MULT, mode, lhs,
980                                         GEN_INT (coeff0 + coeff1));
981                   return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
982                 }
983             }
984
985           /* If one of the operands is a PLUS or a MINUS, see if we can
986              simplify this by the associative law. 
987              Don't use the associative law for floating point.
988              The inaccuracy makes it nonassociative,
989              and subtle programs can break if operations are associated.  */
990
991           if (INTEGRAL_MODE_P (mode)
992               && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
993                   || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS)
994               && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
995             return tem;
996           break;
997
998         case COMPARE:
999 #ifdef HAVE_cc0
1000           /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
1001              using cc0, in which case we want to leave it as a COMPARE
1002              so we can distinguish it from a register-register-copy.
1003
1004              In IEEE floating point, x-0 is not the same as x.  */
1005
1006           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1007                || ! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
1008               && op1 == CONST0_RTX (mode))
1009             return op0;
1010 #endif
1011
1012           /* Convert (compare (gt (flags) 0) (lt (flags) 0)) to (flags).  */
1013           if (((GET_CODE (op0) == GT && GET_CODE (op1) == LT)
1014                || (GET_CODE (op0) == GTU && GET_CODE (op1) == LTU))
1015               && XEXP (op0, 1) == const0_rtx && XEXP (op1, 1) == const0_rtx)
1016             {
1017               rtx xop00 = XEXP (op0, 0);
1018               rtx xop10 = XEXP (op1, 0);
1019
1020 #ifdef HAVE_cc0
1021               if (GET_CODE (xop00) == CC0 && GET_CODE (xop10) == CC0)
1022 #else
1023               if (GET_CODE (xop00) == REG && GET_CODE (xop10) == REG
1024                   && GET_MODE (xop00) == GET_MODE (xop10)
1025                   && REGNO (xop00) == REGNO (xop10)
1026                   && GET_MODE_CLASS (GET_MODE (xop00)) == MODE_CC
1027                   && GET_MODE_CLASS (GET_MODE (xop10)) == MODE_CC)
1028 #endif
1029                 return xop00;
1030             }
1031
1032           break;              
1033         case MINUS:
1034           /* None of these optimizations can be done for IEEE
1035              floating point.  */
1036           if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1037               && FLOAT_MODE_P (mode) && ! flag_unsafe_math_optimizations)
1038             break;
1039
1040           /* We can't assume x-x is 0 even with non-IEEE floating point,
1041              but since it is zero except in very strange circumstances, we
1042              will treat it as zero with -funsafe-math-optimizations.  */
1043           if (rtx_equal_p (op0, op1)
1044               && ! side_effects_p (op0)
1045               && (! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations))
1046             return CONST0_RTX (mode);
1047
1048           /* Change subtraction from zero into negation.  */
1049           if (op0 == CONST0_RTX (mode))
1050             return gen_rtx_NEG (mode, op1);
1051
1052           /* (-1 - a) is ~a.  */
1053           if (op0 == constm1_rtx)
1054             return gen_rtx_NOT (mode, op1);
1055
1056           /* Subtracting 0 has no effect.  */
1057           if (op1 == CONST0_RTX (mode))
1058             return op0;
1059
1060           /* See if this is something like X * C - X or vice versa or
1061              if the multiplication is written as a shift.  If so, we can
1062              distribute and make a new multiply, shift, or maybe just
1063              have X (if C is 2 in the example above).  But don't make
1064              real multiply if we didn't have one before.  */
1065
1066           if (! FLOAT_MODE_P (mode))
1067             {
1068               HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
1069               rtx lhs = op0, rhs = op1;
1070               int had_mult = 0;
1071
1072               if (GET_CODE (lhs) == NEG)
1073                 coeff0 = -1, lhs = XEXP (lhs, 0);
1074               else if (GET_CODE (lhs) == MULT
1075                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
1076                 {
1077                   coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
1078                   had_mult = 1;
1079                 }
1080               else if (GET_CODE (lhs) == ASHIFT
1081                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT
1082                        && INTVAL (XEXP (lhs, 1)) >= 0
1083                        && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
1084                 {
1085                   coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
1086                   lhs = XEXP (lhs, 0);
1087                 }
1088
1089               if (GET_CODE (rhs) == NEG)
1090                 coeff1 = - 1, rhs = XEXP (rhs, 0);
1091               else if (GET_CODE (rhs) == MULT
1092                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
1093                 {
1094                   coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
1095                   had_mult = 1;
1096                 }
1097               else if (GET_CODE (rhs) == ASHIFT
1098                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT
1099                        && INTVAL (XEXP (rhs, 1)) >= 0
1100                        && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
1101                 {
1102                   coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
1103                   rhs = XEXP (rhs, 0);
1104                 }
1105
1106               if (rtx_equal_p (lhs, rhs))
1107                 {
1108                   tem = simplify_gen_binary (MULT, mode, lhs,
1109                                              GEN_INT (coeff0 - coeff1));
1110                   return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
1111                 }
1112             }
1113
1114           /* (a - (-b)) -> (a + b).  */
1115           if (GET_CODE (op1) == NEG)
1116             return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
1117
1118           /* If one of the operands is a PLUS or a MINUS, see if we can
1119              simplify this by the associative law. 
1120              Don't use the associative law for floating point.
1121              The inaccuracy makes it nonassociative,
1122              and subtle programs can break if operations are associated.  */
1123
1124           if (INTEGRAL_MODE_P (mode)
1125               && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
1126                   || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS)
1127               && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
1128             return tem;
1129
1130           /* Don't let a relocatable value get a negative coeff.  */
1131           if (GET_CODE (op1) == CONST_INT && GET_MODE (op0) != VOIDmode)
1132             return plus_constant (op0, - INTVAL (op1));
1133
1134           /* (x - (x & y)) -> (x & ~y) */
1135           if (GET_CODE (op1) == AND)
1136             {
1137              if (rtx_equal_p (op0, XEXP (op1, 0)))
1138                return simplify_gen_binary (AND, mode, op0,
1139                                            gen_rtx_NOT (mode, XEXP (op1, 1)));
1140              if (rtx_equal_p (op0, XEXP (op1, 1)))
1141                return simplify_gen_binary (AND, mode, op0,
1142                                            gen_rtx_NOT (mode, XEXP (op1, 0)));
1143            }
1144           break;
1145
1146         case MULT:
1147           if (op1 == constm1_rtx)
1148             {
1149               tem = simplify_unary_operation (NEG, mode, op0, mode);
1150
1151               return tem ? tem : gen_rtx_NEG (mode, op0);
1152             }
1153
1154           /* In IEEE floating point, x*0 is not always 0.  */
1155           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1156                || ! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
1157               && op1 == CONST0_RTX (mode)
1158               && ! side_effects_p (op0))
1159             return op1;
1160
1161           /* In IEEE floating point, x*1 is not equivalent to x for nans.
1162              However, ANSI says we can drop signals,
1163              so we can do this anyway.  */
1164           if (op1 == CONST1_RTX (mode))
1165             return op0;
1166
1167           /* Convert multiply by constant power of two into shift unless
1168              we are still generating RTL.  This test is a kludge.  */
1169           if (GET_CODE (op1) == CONST_INT
1170               && (val = exact_log2 (INTVAL (op1))) >= 0
1171               /* If the mode is larger than the host word size, and the
1172                  uppermost bit is set, then this isn't a power of two due
1173                  to implicit sign extension.  */
1174               && (width <= HOST_BITS_PER_WIDE_INT
1175                   || val != HOST_BITS_PER_WIDE_INT - 1)
1176               && ! rtx_equal_function_value_matters)
1177             return gen_rtx_ASHIFT (mode, op0, GEN_INT (val));
1178
1179           if (GET_CODE (op1) == CONST_DOUBLE
1180               && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT)
1181             {
1182               REAL_VALUE_TYPE d;
1183               jmp_buf handler;
1184               int op1is2, op1ism1;
1185
1186               if (setjmp (handler))
1187                 return 0;
1188
1189               set_float_handler (handler);
1190               REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
1191               op1is2 = REAL_VALUES_EQUAL (d, dconst2);
1192               op1ism1 = REAL_VALUES_EQUAL (d, dconstm1);
1193               set_float_handler (NULL_PTR);
1194
1195               /* x*2 is x+x and x*(-1) is -x */
1196               if (op1is2 && GET_MODE (op0) == mode)
1197                 return gen_rtx_PLUS (mode, op0, copy_rtx (op0));
1198
1199               else if (op1ism1 && GET_MODE (op0) == mode)
1200                 return gen_rtx_NEG (mode, op0);
1201             }
1202           break;
1203
1204         case IOR:
1205           if (op1 == const0_rtx)
1206             return op0;
1207           if (GET_CODE (op1) == CONST_INT
1208               && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1209             return op1;
1210           if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1211             return op0;
1212           /* A | (~A) -> -1 */
1213           if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
1214                || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
1215               && ! side_effects_p (op0)
1216               && GET_MODE_CLASS (mode) != MODE_CC)
1217             return constm1_rtx;
1218           break;
1219
1220         case XOR:
1221           if (op1 == const0_rtx)
1222             return op0;
1223           if (GET_CODE (op1) == CONST_INT
1224               && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1225             return gen_rtx_NOT (mode, op0);
1226           if (op0 == op1 && ! side_effects_p (op0)
1227               && GET_MODE_CLASS (mode) != MODE_CC)
1228             return const0_rtx;
1229           break;
1230
1231         case AND:
1232           if (op1 == const0_rtx && ! side_effects_p (op0))
1233             return const0_rtx;
1234           if (GET_CODE (op1) == CONST_INT
1235               && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1236             return op0;
1237           if (op0 == op1 && ! side_effects_p (op0)
1238               && GET_MODE_CLASS (mode) != MODE_CC)
1239             return op0;
1240           /* A & (~A) -> 0 */
1241           if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
1242                || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
1243               && ! side_effects_p (op0)
1244               && GET_MODE_CLASS (mode) != MODE_CC)
1245             return const0_rtx;
1246           break;
1247
1248         case UDIV:
1249           /* Convert divide by power of two into shift (divide by 1 handled
1250              below).  */
1251           if (GET_CODE (op1) == CONST_INT
1252               && (arg1 = exact_log2 (INTVAL (op1))) > 0)
1253             return gen_rtx_LSHIFTRT (mode, op0, GEN_INT (arg1));
1254
1255           /* ... fall through ...  */
1256
1257         case DIV:
1258           if (op1 == CONST1_RTX (mode))
1259             return op0;
1260
1261           /* In IEEE floating point, 0/x is not always 0.  */
1262           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1263                || ! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
1264               && op0 == CONST0_RTX (mode)
1265               && ! side_effects_p (op1))
1266             return op0;
1267
1268 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1269           /* Change division by a constant into multiplication.  Only do
1270              this with -funsafe-math-optimizations.  */
1271           else if (GET_CODE (op1) == CONST_DOUBLE
1272                    && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT
1273                    && op1 != CONST0_RTX (mode)
1274                    && flag_unsafe_math_optimizations)
1275             {
1276               REAL_VALUE_TYPE d;
1277               REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
1278
1279               if (! REAL_VALUES_EQUAL (d, dconst0))
1280                 {
1281 #if defined (REAL_ARITHMETIC)
1282                   REAL_ARITHMETIC (d, rtx_to_tree_code (DIV), dconst1, d);
1283                   return gen_rtx_MULT (mode, op0, 
1284                                        CONST_DOUBLE_FROM_REAL_VALUE (d, mode));
1285 #else
1286                   return
1287                     gen_rtx_MULT (mode, op0, 
1288                                   CONST_DOUBLE_FROM_REAL_VALUE (1./d, mode));
1289 #endif
1290                 }
1291             }
1292 #endif
1293           break;
1294
1295         case UMOD:
1296           /* Handle modulus by power of two (mod with 1 handled below).  */
1297           if (GET_CODE (op1) == CONST_INT
1298               && exact_log2 (INTVAL (op1)) > 0)
1299             return gen_rtx_AND (mode, op0, GEN_INT (INTVAL (op1) - 1));
1300
1301           /* ... fall through ...  */
1302
1303         case MOD:
1304           if ((op0 == const0_rtx || op1 == const1_rtx)
1305               && ! side_effects_p (op0) && ! side_effects_p (op1))
1306             return const0_rtx;
1307           break;
1308
1309         case ROTATERT:
1310         case ROTATE:
1311           /* Rotating ~0 always results in ~0.  */
1312           if (GET_CODE (op0) == CONST_INT && width <= HOST_BITS_PER_WIDE_INT
1313               && (unsigned HOST_WIDE_INT) INTVAL (op0) == GET_MODE_MASK (mode)
1314               && ! side_effects_p (op1))
1315             return op0;
1316
1317           /* ... fall through ...  */
1318
1319         case ASHIFT:
1320         case ASHIFTRT:
1321         case LSHIFTRT:
1322           if (op1 == const0_rtx)
1323             return op0;
1324           if (op0 == const0_rtx && ! side_effects_p (op1))
1325             return op0;
1326           break;
1327
1328         case SMIN:
1329           if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT 
1330               && INTVAL (op1) == (HOST_WIDE_INT) 1 << (width -1)
1331               && ! side_effects_p (op0))
1332             return op1;
1333           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1334             return op0;
1335           break;
1336            
1337         case SMAX:
1338           if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT
1339               && ((unsigned HOST_WIDE_INT) INTVAL (op1)
1340                   == (unsigned HOST_WIDE_INT) GET_MODE_MASK (mode) >> 1)
1341               && ! side_effects_p (op0))
1342             return op1;
1343           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1344             return op0;
1345           break;
1346
1347         case UMIN:
1348           if (op1 == const0_rtx && ! side_effects_p (op0))
1349             return op1;
1350           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1351             return op0;
1352           break;
1353             
1354         case UMAX:
1355           if (op1 == constm1_rtx && ! side_effects_p (op0))
1356             return op1;
1357           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1358             return op0;
1359           break;
1360
1361         default:
1362           abort ();
1363         }
1364       
1365       return 0;
1366     }
1367
1368   /* Get the integer argument values in two forms:
1369      zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S.  */
1370
1371   arg0 = INTVAL (op0);
1372   arg1 = INTVAL (op1);
1373
1374   if (width < HOST_BITS_PER_WIDE_INT)
1375     {
1376       arg0 &= ((HOST_WIDE_INT) 1 << width) - 1;
1377       arg1 &= ((HOST_WIDE_INT) 1 << width) - 1;
1378
1379       arg0s = arg0;
1380       if (arg0s & ((HOST_WIDE_INT) 1 << (width - 1)))
1381         arg0s |= ((HOST_WIDE_INT) (-1) << width);
1382
1383       arg1s = arg1;
1384       if (arg1s & ((HOST_WIDE_INT) 1 << (width - 1)))
1385         arg1s |= ((HOST_WIDE_INT) (-1) << width);
1386     }
1387   else
1388     {
1389       arg0s = arg0;
1390       arg1s = arg1;
1391     }
1392
1393   /* Compute the value of the arithmetic.  */
1394
1395   switch (code)
1396     {
1397     case PLUS:
1398       val = arg0s + arg1s;
1399       break;
1400
1401     case MINUS:
1402       val = arg0s - arg1s;
1403       break;
1404
1405     case MULT:
1406       val = arg0s * arg1s;
1407       break;
1408
1409     case DIV:
1410       if (arg1s == 0)
1411         return 0;
1412       val = arg0s / arg1s;
1413       break;
1414
1415     case MOD:
1416       if (arg1s == 0)
1417         return 0;
1418       val = arg0s % arg1s;
1419       break;
1420
1421     case UDIV:
1422       if (arg1 == 0)
1423         return 0;
1424       val = (unsigned HOST_WIDE_INT) arg0 / arg1;
1425       break;
1426
1427     case UMOD:
1428       if (arg1 == 0)
1429         return 0;
1430       val = (unsigned HOST_WIDE_INT) arg0 % arg1;
1431       break;
1432
1433     case AND:
1434       val = arg0 & arg1;
1435       break;
1436
1437     case IOR:
1438       val = arg0 | arg1;
1439       break;
1440
1441     case XOR:
1442       val = arg0 ^ arg1;
1443       break;
1444
1445     case LSHIFTRT:
1446       /* If shift count is undefined, don't fold it; let the machine do
1447          what it wants.  But truncate it if the machine will do that.  */
1448       if (arg1 < 0)
1449         return 0;
1450
1451 #ifdef SHIFT_COUNT_TRUNCATED
1452       if (SHIFT_COUNT_TRUNCATED)
1453         arg1 %= width;
1454 #endif
1455
1456       val = ((unsigned HOST_WIDE_INT) arg0) >> arg1;
1457       break;
1458
1459     case ASHIFT:
1460       if (arg1 < 0)
1461         return 0;
1462
1463 #ifdef SHIFT_COUNT_TRUNCATED
1464       if (SHIFT_COUNT_TRUNCATED)
1465         arg1 %= width;
1466 #endif
1467
1468       val = ((unsigned HOST_WIDE_INT) arg0) << arg1;
1469       break;
1470
1471     case ASHIFTRT:
1472       if (arg1 < 0)
1473         return 0;
1474
1475 #ifdef SHIFT_COUNT_TRUNCATED
1476       if (SHIFT_COUNT_TRUNCATED)
1477         arg1 %= width;
1478 #endif
1479
1480       val = arg0s >> arg1;
1481
1482       /* Bootstrap compiler may not have sign extended the right shift.
1483          Manually extend the sign to insure bootstrap cc matches gcc.  */
1484       if (arg0s < 0 && arg1 > 0)
1485         val |= ((HOST_WIDE_INT) -1) << (HOST_BITS_PER_WIDE_INT - arg1);
1486
1487       break;
1488
1489     case ROTATERT:
1490       if (arg1 < 0)
1491         return 0;
1492
1493       arg1 %= width;
1494       val = ((((unsigned HOST_WIDE_INT) arg0) << (width - arg1))
1495              | (((unsigned HOST_WIDE_INT) arg0) >> arg1));
1496       break;
1497
1498     case ROTATE:
1499       if (arg1 < 0)
1500         return 0;
1501
1502       arg1 %= width;
1503       val = ((((unsigned HOST_WIDE_INT) arg0) << arg1)
1504              | (((unsigned HOST_WIDE_INT) arg0) >> (width - arg1)));
1505       break;
1506
1507     case COMPARE:
1508       /* Do nothing here.  */
1509       return 0;
1510
1511     case SMIN:
1512       val = arg0s <= arg1s ? arg0s : arg1s;
1513       break;
1514
1515     case UMIN:
1516       val = ((unsigned HOST_WIDE_INT) arg0
1517              <= (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1518       break;
1519
1520     case SMAX:
1521       val = arg0s > arg1s ? arg0s : arg1s;
1522       break;
1523
1524     case UMAX:
1525       val = ((unsigned HOST_WIDE_INT) arg0
1526              > (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1527       break;
1528
1529     default:
1530       abort ();
1531     }
1532
1533   val = trunc_int_for_mode (val, mode);
1534
1535   return GEN_INT (val);
1536 }
1537 \f
1538 /* Simplify a PLUS or MINUS, at least one of whose operands may be another
1539    PLUS or MINUS.
1540
1541    Rather than test for specific case, we do this by a brute-force method
1542    and do all possible simplifications until no more changes occur.  Then
1543    we rebuild the operation.  */
1544
1545 static rtx
1546 simplify_plus_minus (code, mode, op0, op1)
1547      enum rtx_code code;
1548      enum machine_mode mode;
1549      rtx op0, op1;
1550 {
1551   rtx ops[8];
1552   int negs[8];
1553   rtx result, tem;
1554   int n_ops = 2, input_ops = 2, input_consts = 0, n_consts = 0;
1555   int first = 1, negate = 0, changed;
1556   int i, j;
1557
1558   memset ((char *) ops, 0, sizeof ops);
1559   
1560   /* Set up the two operands and then expand them until nothing has been
1561      changed.  If we run out of room in our array, give up; this should
1562      almost never happen.  */
1563
1564   ops[0] = op0, ops[1] = op1, negs[0] = 0, negs[1] = (code == MINUS);
1565
1566   changed = 1;
1567   while (changed)
1568     {
1569       changed = 0;
1570
1571       for (i = 0; i < n_ops; i++)
1572         switch (GET_CODE (ops[i]))
1573           {
1574           case PLUS:
1575           case MINUS:
1576             if (n_ops == 7)
1577               return 0;
1578
1579             ops[n_ops] = XEXP (ops[i], 1);
1580             negs[n_ops++] = GET_CODE (ops[i]) == MINUS ? !negs[i] : negs[i];
1581             ops[i] = XEXP (ops[i], 0);
1582             input_ops++;
1583             changed = 1;
1584             break;
1585
1586           case NEG:
1587             ops[i] = XEXP (ops[i], 0);
1588             negs[i] = ! negs[i];
1589             changed = 1;
1590             break;
1591
1592           case CONST:
1593             ops[i] = XEXP (ops[i], 0);
1594             input_consts++;
1595             changed = 1;
1596             break;
1597
1598           case NOT:
1599             /* ~a -> (-a - 1) */
1600             if (n_ops != 7)
1601               {
1602                 ops[n_ops] = constm1_rtx;
1603                 negs[n_ops++] = negs[i];
1604                 ops[i] = XEXP (ops[i], 0);
1605                 negs[i] = ! negs[i];
1606                 changed = 1;
1607               }
1608             break;
1609
1610           case CONST_INT:
1611             if (negs[i])
1612               ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0, changed = 1;
1613             break;
1614
1615           default:
1616             break;
1617           }
1618     }
1619
1620   /* If we only have two operands, we can't do anything.  */
1621   if (n_ops <= 2)
1622     return 0;
1623
1624   /* Now simplify each pair of operands until nothing changes.  The first
1625      time through just simplify constants against each other.  */
1626
1627   changed = 1;
1628   while (changed)
1629     {
1630       changed = first;
1631
1632       for (i = 0; i < n_ops - 1; i++)
1633         for (j = i + 1; j < n_ops; j++)
1634           if (ops[i] != 0 && ops[j] != 0
1635               && (! first || (CONSTANT_P (ops[i]) && CONSTANT_P (ops[j]))))
1636             {
1637               rtx lhs = ops[i], rhs = ops[j];
1638               enum rtx_code ncode = PLUS;
1639
1640               if (negs[i] && ! negs[j])
1641                 lhs = ops[j], rhs = ops[i], ncode = MINUS;
1642               else if (! negs[i] && negs[j])
1643                 ncode = MINUS;
1644
1645               tem = simplify_binary_operation (ncode, mode, lhs, rhs);
1646               if (tem)
1647                 {
1648                   ops[i] = tem, ops[j] = 0;
1649                   negs[i] = negs[i] && negs[j];
1650                   if (GET_CODE (tem) == NEG)
1651                     ops[i] = XEXP (tem, 0), negs[i] = ! negs[i];
1652
1653                   if (GET_CODE (ops[i]) == CONST_INT && negs[i])
1654                     ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0;
1655                   changed = 1;
1656                 }
1657             }
1658
1659       first = 0;
1660     }
1661
1662   /* Pack all the operands to the lower-numbered entries and give up if
1663      we didn't reduce the number of operands we had.  Make sure we
1664      count a CONST as two operands.  If we have the same number of
1665      operands, but have made more CONSTs than we had, this is also
1666      an improvement, so accept it.  */
1667
1668   for (i = 0, j = 0; j < n_ops; j++)
1669     if (ops[j] != 0)
1670       {
1671         ops[i] = ops[j], negs[i++] = negs[j];
1672         if (GET_CODE (ops[j]) == CONST)
1673           n_consts++;
1674       }
1675
1676   if (i + n_consts > input_ops
1677       || (i + n_consts == input_ops && n_consts <= input_consts))
1678     return 0;
1679
1680   n_ops = i;
1681
1682   /* If we have a CONST_INT, put it last.  */
1683   for (i = 0; i < n_ops - 1; i++)
1684     if (GET_CODE (ops[i]) == CONST_INT)
1685       {
1686         tem = ops[n_ops - 1], ops[n_ops - 1] = ops[i] , ops[i] = tem;
1687         j = negs[n_ops - 1], negs[n_ops - 1] = negs[i], negs[i] = j;
1688       }
1689
1690   /* Put a non-negated operand first.  If there aren't any, make all
1691      operands positive and negate the whole thing later.  */
1692   for (i = 0; i < n_ops && negs[i]; i++)
1693     ;
1694
1695   if (i == n_ops)
1696     {
1697       for (i = 0; i < n_ops; i++)
1698         negs[i] = 0;
1699       negate = 1;
1700     }
1701   else if (i != 0)
1702     {
1703       tem = ops[0], ops[0] = ops[i], ops[i] = tem;
1704       j = negs[0], negs[0] = negs[i], negs[i] = j;
1705     }
1706
1707   /* Now make the result by performing the requested operations.  */
1708   result = ops[0];
1709   for (i = 1; i < n_ops; i++)
1710     result = simplify_gen_binary (negs[i] ? MINUS : PLUS, mode, result, ops[i]);
1711
1712   return negate ? gen_rtx_NEG (mode, result) : result;
1713 }
1714
1715 struct cfc_args
1716 {
1717   rtx op0, op1;                 /* Input */
1718   int equal, op0lt, op1lt;      /* Output */
1719   int unordered;
1720 };
1721
1722 static void
1723 check_fold_consts (data)
1724   PTR data;
1725 {
1726   struct cfc_args *args = (struct cfc_args *) data;
1727   REAL_VALUE_TYPE d0, d1;
1728
1729   /* We may possibly raise an exception while reading the value.  */
1730   args->unordered = 1;
1731   REAL_VALUE_FROM_CONST_DOUBLE (d0, args->op0);
1732   REAL_VALUE_FROM_CONST_DOUBLE (d1, args->op1);
1733
1734   /* Comparisons of Inf versus Inf are ordered.  */
1735   if (REAL_VALUE_ISNAN (d0)
1736       || REAL_VALUE_ISNAN (d1))
1737     return;
1738   args->equal = REAL_VALUES_EQUAL (d0, d1);
1739   args->op0lt = REAL_VALUES_LESS (d0, d1);
1740   args->op1lt = REAL_VALUES_LESS (d1, d0);
1741   args->unordered = 0;
1742 }
1743
1744 /* Like simplify_binary_operation except used for relational operators.
1745    MODE is the mode of the operands, not that of the result.  If MODE
1746    is VOIDmode, both operands must also be VOIDmode and we compare the
1747    operands in "infinite precision".
1748
1749    If no simplification is possible, this function returns zero.  Otherwise,
1750    it returns either const_true_rtx or const0_rtx.  */
1751
1752 rtx
1753 simplify_relational_operation (code, mode, op0, op1)
1754      enum rtx_code code;
1755      enum machine_mode mode;
1756      rtx op0, op1;
1757 {
1758   int equal, op0lt, op0ltu, op1lt, op1ltu;
1759   rtx tem;
1760
1761   if (mode == VOIDmode
1762       && (GET_MODE (op0) != VOIDmode
1763           || GET_MODE (op1) != VOIDmode))
1764     abort ();
1765
1766   /* If op0 is a compare, extract the comparison arguments from it.  */
1767   if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
1768     op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
1769
1770   /* We can't simplify MODE_CC values since we don't know what the
1771      actual comparison is.  */
1772   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC
1773 #ifdef HAVE_cc0
1774       || op0 == cc0_rtx
1775 #endif
1776       )
1777     return 0;
1778
1779   /* Make sure the constant is second.  */
1780   if ((CONSTANT_P (op0) && ! CONSTANT_P (op1))
1781       || (GET_CODE (op0) == CONST_INT && GET_CODE (op1) != CONST_INT))
1782     {
1783       tem = op0, op0 = op1, op1 = tem;
1784       code = swap_condition (code);
1785     }
1786
1787   /* For integer comparisons of A and B maybe we can simplify A - B and can
1788      then simplify a comparison of that with zero.  If A and B are both either
1789      a register or a CONST_INT, this can't help; testing for these cases will
1790      prevent infinite recursion here and speed things up.
1791
1792      If CODE is an unsigned comparison, then we can never do this optimization,
1793      because it gives an incorrect result if the subtraction wraps around zero.
1794      ANSI C defines unsigned operations such that they never overflow, and
1795      thus such cases can not be ignored.  */
1796
1797   if (INTEGRAL_MODE_P (mode) && op1 != const0_rtx
1798       && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == CONST_INT)
1799             && (GET_CODE (op1) == REG || GET_CODE (op1) == CONST_INT))
1800       && 0 != (tem = simplify_binary_operation (MINUS, mode, op0, op1))
1801       && code != GTU && code != GEU && code != LTU && code != LEU)
1802     return simplify_relational_operation (signed_condition (code),
1803                                           mode, tem, const0_rtx);
1804
1805   if (flag_unsafe_math_optimizations && code == ORDERED)
1806     return const_true_rtx;
1807
1808   if (flag_unsafe_math_optimizations && code == UNORDERED)
1809     return const0_rtx;
1810
1811   /* For non-IEEE floating-point, if the two operands are equal, we know the
1812      result.  */
1813   if (rtx_equal_p (op0, op1)
1814       && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1815           || ! FLOAT_MODE_P (GET_MODE (op0)) 
1816           || flag_unsafe_math_optimizations))
1817     equal = 1, op0lt = 0, op0ltu = 0, op1lt = 0, op1ltu = 0;
1818
1819   /* If the operands are floating-point constants, see if we can fold
1820      the result.  */
1821 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1822   else if (GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE
1823            && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
1824     {
1825       struct cfc_args args;
1826
1827       /* Setup input for check_fold_consts() */
1828       args.op0 = op0;
1829       args.op1 = op1;
1830       
1831       
1832       if (!do_float_handler (check_fold_consts, (PTR) &args))
1833         args.unordered = 1;
1834
1835       if (args.unordered)
1836         switch (code)
1837           {
1838           case UNEQ:
1839           case UNLT:
1840           case UNGT:
1841           case UNLE:
1842           case UNGE:
1843           case NE:
1844           case UNORDERED:
1845             return const_true_rtx;
1846           case EQ:
1847           case LT:
1848           case GT:
1849           case LE:
1850           case GE:
1851           case LTGT:
1852           case ORDERED:
1853             return const0_rtx;
1854           default:
1855             return 0;
1856           }
1857
1858       /* Receive output from check_fold_consts() */
1859       equal = args.equal;
1860       op0lt = op0ltu = args.op0lt;
1861       op1lt = op1ltu = args.op1lt;
1862     }
1863 #endif  /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1864
1865   /* Otherwise, see if the operands are both integers.  */
1866   else if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
1867            && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT)
1868            && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT))
1869     {
1870       int width = GET_MODE_BITSIZE (mode);
1871       HOST_WIDE_INT l0s, h0s, l1s, h1s;
1872       unsigned HOST_WIDE_INT l0u, h0u, l1u, h1u;
1873
1874       /* Get the two words comprising each integer constant.  */
1875       if (GET_CODE (op0) == CONST_DOUBLE)
1876         {
1877           l0u = l0s = CONST_DOUBLE_LOW (op0);
1878           h0u = h0s = CONST_DOUBLE_HIGH (op0);
1879         }
1880       else
1881         {
1882           l0u = l0s = INTVAL (op0);
1883           h0u = h0s = HWI_SIGN_EXTEND (l0s);
1884         }
1885           
1886       if (GET_CODE (op1) == CONST_DOUBLE)
1887         {
1888           l1u = l1s = CONST_DOUBLE_LOW (op1);
1889           h1u = h1s = CONST_DOUBLE_HIGH (op1);
1890         }
1891       else
1892         {
1893           l1u = l1s = INTVAL (op1);
1894           h1u = h1s = HWI_SIGN_EXTEND (l1s);
1895         }
1896
1897       /* If WIDTH is nonzero and smaller than HOST_BITS_PER_WIDE_INT,
1898          we have to sign or zero-extend the values.  */
1899       if (width != 0 && width < HOST_BITS_PER_WIDE_INT)
1900         {
1901           l0u &= ((HOST_WIDE_INT) 1 << width) - 1;
1902           l1u &= ((HOST_WIDE_INT) 1 << width) - 1;
1903
1904           if (l0s & ((HOST_WIDE_INT) 1 << (width - 1)))
1905             l0s |= ((HOST_WIDE_INT) (-1) << width);
1906
1907           if (l1s & ((HOST_WIDE_INT) 1 << (width - 1)))
1908             l1s |= ((HOST_WIDE_INT) (-1) << width);
1909         }
1910       if (width != 0 && width <= HOST_BITS_PER_WIDE_INT)
1911         h0u = h1u = 0, h0s = HWI_SIGN_EXTEND (l0s), h1s = HWI_SIGN_EXTEND (l1s);
1912
1913       equal = (h0u == h1u && l0u == l1u);
1914       op0lt = (h0s < h1s || (h0s == h1s && l0u < l1u));
1915       op1lt = (h1s < h0s || (h1s == h0s && l1u < l0u));
1916       op0ltu = (h0u < h1u || (h0u == h1u && l0u < l1u));
1917       op1ltu = (h1u < h0u || (h1u == h0u && l1u < l0u));
1918     }
1919
1920   /* Otherwise, there are some code-specific tests we can make.  */
1921   else
1922     {
1923       switch (code)
1924         {
1925         case EQ:
1926           /* References to the frame plus a constant or labels cannot
1927              be zero, but a SYMBOL_REF can due to #pragma weak.  */
1928           if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx)
1929                || GET_CODE (op0) == LABEL_REF)
1930 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1931               /* On some machines, the ap reg can be 0 sometimes.  */
1932               && op0 != arg_pointer_rtx
1933 #endif
1934                 )
1935             return const0_rtx;
1936           break;
1937
1938         case NE:
1939           if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx)
1940                || GET_CODE (op0) == LABEL_REF)
1941 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1942               && op0 != arg_pointer_rtx
1943 #endif
1944               )
1945             return const_true_rtx;
1946           break;
1947
1948         case GEU:
1949           /* Unsigned values are never negative.  */
1950           if (op1 == const0_rtx)
1951             return const_true_rtx;
1952           break;
1953
1954         case LTU:
1955           if (op1 == const0_rtx)
1956             return const0_rtx;
1957           break;
1958
1959         case LEU:
1960           /* Unsigned values are never greater than the largest
1961              unsigned value.  */
1962           if (GET_CODE (op1) == CONST_INT
1963               && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
1964             && INTEGRAL_MODE_P (mode))
1965           return const_true_rtx;
1966           break;
1967
1968         case GTU:
1969           if (GET_CODE (op1) == CONST_INT
1970               && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
1971               && INTEGRAL_MODE_P (mode))
1972             return const0_rtx;
1973           break;
1974           
1975         default:
1976           break;
1977         }
1978
1979       return 0;
1980     }
1981
1982   /* If we reach here, EQUAL, OP0LT, OP0LTU, OP1LT, and OP1LTU are set
1983      as appropriate.  */
1984   switch (code)
1985     {
1986     case EQ:
1987     case UNEQ:
1988       return equal ? const_true_rtx : const0_rtx;
1989     case NE:
1990     case LTGT:
1991       return ! equal ? const_true_rtx : const0_rtx;
1992     case LT:
1993     case UNLT:
1994       return op0lt ? const_true_rtx : const0_rtx;
1995     case GT:
1996     case UNGT:
1997       return op1lt ? const_true_rtx : const0_rtx;
1998     case LTU:
1999       return op0ltu ? const_true_rtx : const0_rtx;
2000     case GTU:
2001       return op1ltu ? const_true_rtx : const0_rtx;
2002     case LE:
2003     case UNLE:
2004       return equal || op0lt ? const_true_rtx : const0_rtx;
2005     case GE:
2006     case UNGE:
2007       return equal || op1lt ? const_true_rtx : const0_rtx;
2008     case LEU:
2009       return equal || op0ltu ? const_true_rtx : const0_rtx;
2010     case GEU:
2011       return equal || op1ltu ? const_true_rtx : const0_rtx;
2012     case ORDERED:
2013       return const_true_rtx;
2014     case UNORDERED:
2015       return const0_rtx;
2016     default:
2017       abort ();
2018     }
2019 }
2020 \f
2021 /* Simplify CODE, an operation with result mode MODE and three operands,
2022    OP0, OP1, and OP2.  OP0_MODE was the mode of OP0 before it became
2023    a constant.  Return 0 if no simplifications is possible.  */
2024
2025 rtx
2026 simplify_ternary_operation (code, mode, op0_mode, op0, op1, op2)
2027      enum rtx_code code;
2028      enum machine_mode mode, op0_mode;
2029      rtx op0, op1, op2;
2030 {
2031   unsigned int width = GET_MODE_BITSIZE (mode);
2032
2033   /* VOIDmode means "infinite" precision.  */
2034   if (width == 0)
2035     width = HOST_BITS_PER_WIDE_INT;
2036
2037   switch (code)
2038     {
2039     case SIGN_EXTRACT:
2040     case ZERO_EXTRACT:
2041       if (GET_CODE (op0) == CONST_INT
2042           && GET_CODE (op1) == CONST_INT
2043           && GET_CODE (op2) == CONST_INT
2044           && ((unsigned) INTVAL (op1) + (unsigned) INTVAL (op2) <= width)
2045           && width <= (unsigned) HOST_BITS_PER_WIDE_INT)
2046         {
2047           /* Extracting a bit-field from a constant */
2048           HOST_WIDE_INT val = INTVAL (op0);
2049
2050           if (BITS_BIG_ENDIAN)
2051             val >>= (GET_MODE_BITSIZE (op0_mode)
2052                      - INTVAL (op2) - INTVAL (op1));
2053           else
2054             val >>= INTVAL (op2);
2055
2056           if (HOST_BITS_PER_WIDE_INT != INTVAL (op1))
2057             {
2058               /* First zero-extend.  */
2059               val &= ((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1;
2060               /* If desired, propagate sign bit.  */
2061               if (code == SIGN_EXTRACT
2062                   && (val & ((HOST_WIDE_INT) 1 << (INTVAL (op1) - 1))))
2063                 val |= ~ (((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1);
2064             }
2065
2066           /* Clear the bits that don't belong in our mode,
2067              unless they and our sign bit are all one.
2068              So we get either a reasonable negative value or a reasonable
2069              unsigned value for this mode.  */
2070           if (width < HOST_BITS_PER_WIDE_INT
2071               && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
2072                   != ((HOST_WIDE_INT) (-1) << (width - 1))))
2073             val &= ((HOST_WIDE_INT) 1 << width) - 1;
2074
2075           return GEN_INT (val);
2076         }
2077       break;
2078
2079     case IF_THEN_ELSE:
2080       if (GET_CODE (op0) == CONST_INT)
2081         return op0 != const0_rtx ? op1 : op2;
2082
2083       /* Convert a == b ? b : a to "a".  */
2084       if (GET_CODE (op0) == NE && ! side_effects_p (op0)
2085           && (! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
2086           && rtx_equal_p (XEXP (op0, 0), op1)
2087           && rtx_equal_p (XEXP (op0, 1), op2))
2088         return op1;
2089       else if (GET_CODE (op0) == EQ && ! side_effects_p (op0)
2090           && (! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
2091           && rtx_equal_p (XEXP (op0, 1), op1)
2092           && rtx_equal_p (XEXP (op0, 0), op2))
2093         return op2;
2094       else if (GET_RTX_CLASS (GET_CODE (op0)) == '<' && ! side_effects_p (op0))
2095         {
2096           enum machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
2097                                         ? GET_MODE (XEXP (op0, 1))
2098                                         : GET_MODE (XEXP (op0, 0)));
2099           rtx temp;
2100           if (cmp_mode == VOIDmode)
2101             cmp_mode = op0_mode;
2102           temp = simplify_relational_operation (GET_CODE (op0), cmp_mode,
2103                                                 XEXP (op0, 0), XEXP (op0, 1));
2104
2105           /* See if any simplifications were possible.  */
2106           if (temp == const0_rtx)
2107             return op2;
2108           else if (temp == const1_rtx)
2109             return op1;
2110           else if (temp)
2111             op0 = temp;
2112
2113           /* Look for happy constants in op1 and op2.  */
2114           if (GET_CODE (op1) == CONST_INT && GET_CODE (op2) == CONST_INT)
2115             {
2116               HOST_WIDE_INT t = INTVAL (op1);
2117               HOST_WIDE_INT f = INTVAL (op2);
2118               
2119               if (t == STORE_FLAG_VALUE && f == 0)
2120                 code = GET_CODE (op0);
2121               else if (t == 0 && f == STORE_FLAG_VALUE)
2122                 {
2123                   enum rtx_code tmp;
2124                   tmp = reversed_comparison_code (op0, NULL_RTX);
2125                   if (tmp == UNKNOWN)
2126                     break;
2127                   code = tmp;
2128                 }
2129               else
2130                 break;
2131
2132               return gen_rtx_fmt_ee (code, mode, XEXP (op0, 0), XEXP (op0, 1));
2133             }
2134         }
2135       break;
2136
2137     default:
2138       abort ();
2139     }
2140
2141   return 0;
2142 }
2143
2144 /* Simplify X, an rtx expression.
2145
2146    Return the simplified expression or NULL if no simplifications
2147    were possible.
2148
2149    This is the preferred entry point into the simplification routines;
2150    however, we still allow passes to call the more specific routines.
2151
2152    Right now GCC has three (yes, three) major bodies of RTL simplficiation
2153    code that need to be unified.
2154
2155         1. fold_rtx in cse.c.  This code uses various CSE specific
2156            information to aid in RTL simplification.
2157
2158         2. simplify_rtx in combine.c.  Similar to fold_rtx, except that
2159            it uses combine specific information to aid in RTL
2160            simplification.
2161
2162         3. The routines in this file.
2163
2164
2165    Long term we want to only have one body of simplification code; to
2166    get to that state I recommend the following steps:
2167
2168         1. Pour over fold_rtx & simplify_rtx and move any simplifications
2169            which are not pass dependent state into these routines.
2170
2171         2. As code is moved by #1, change fold_rtx & simplify_rtx to
2172            use this routine whenever possible.
2173
2174         3. Allow for pass dependent state to be provided to these
2175            routines and add simplifications based on the pass dependent
2176            state.  Remove code from cse.c & combine.c that becomes
2177            redundant/dead.
2178
2179     It will take time, but ultimately the compiler will be easier to
2180     maintain and improve.  It's totally silly that when we add a
2181     simplification that it needs to be added to 4 places (3 for RTL
2182     simplification and 1 for tree simplification.  */
2183            
2184 rtx
2185 simplify_rtx (x)
2186      rtx x;
2187 {
2188   enum rtx_code code = GET_CODE (x);
2189   enum machine_mode mode = GET_MODE (x);
2190
2191   switch (GET_RTX_CLASS (code))
2192     {
2193     case '1':
2194       return simplify_unary_operation (code, mode,
2195                                        XEXP (x, 0), GET_MODE (XEXP (x, 0)));
2196     case '2':
2197     case 'c':
2198       return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
2199
2200     case '3':
2201     case 'b':
2202       return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
2203                                          XEXP (x, 0), XEXP (x, 1),
2204                                          XEXP (x, 2));
2205
2206     case '<':
2207       return simplify_relational_operation (code,
2208                                             ((GET_MODE (XEXP (x, 0))
2209                                               != VOIDmode)
2210                                              ? GET_MODE (XEXP (x, 0))
2211                                              : GET_MODE (XEXP (x, 1))),
2212                                             XEXP (x, 0), XEXP (x, 1));
2213     default:
2214       return NULL;
2215     }
2216 }