OSDN Git Service

2001-04-04 Diego Novillo <dnovillo@redhat.com>
[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           || (arg0s == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)
1412               && arg1s == -1))
1413         return 0;
1414       val = arg0s / arg1s;
1415       break;
1416
1417     case MOD:
1418       if (arg1s == 0
1419           || (arg0s == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)
1420               && arg1s == -1))
1421         return 0;
1422       val = arg0s % arg1s;
1423       break;
1424
1425     case UDIV:
1426       if (arg1 == 0
1427           || (arg0s == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)
1428               && arg1s == -1))
1429         return 0;
1430       val = (unsigned HOST_WIDE_INT) arg0 / arg1;
1431       break;
1432
1433     case UMOD:
1434       if (arg1 == 0
1435           || (arg0s == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)
1436               && arg1s == -1))
1437         return 0;
1438       val = (unsigned HOST_WIDE_INT) arg0 % arg1;
1439       break;
1440
1441     case AND:
1442       val = arg0 & arg1;
1443       break;
1444
1445     case IOR:
1446       val = arg0 | arg1;
1447       break;
1448
1449     case XOR:
1450       val = arg0 ^ arg1;
1451       break;
1452
1453     case LSHIFTRT:
1454       /* If shift count is undefined, don't fold it; let the machine do
1455          what it wants.  But truncate it if the machine will do that.  */
1456       if (arg1 < 0)
1457         return 0;
1458
1459 #ifdef SHIFT_COUNT_TRUNCATED
1460       if (SHIFT_COUNT_TRUNCATED)
1461         arg1 %= width;
1462 #endif
1463
1464       val = ((unsigned HOST_WIDE_INT) arg0) >> arg1;
1465       break;
1466
1467     case ASHIFT:
1468       if (arg1 < 0)
1469         return 0;
1470
1471 #ifdef SHIFT_COUNT_TRUNCATED
1472       if (SHIFT_COUNT_TRUNCATED)
1473         arg1 %= width;
1474 #endif
1475
1476       val = ((unsigned HOST_WIDE_INT) arg0) << arg1;
1477       break;
1478
1479     case ASHIFTRT:
1480       if (arg1 < 0)
1481         return 0;
1482
1483 #ifdef SHIFT_COUNT_TRUNCATED
1484       if (SHIFT_COUNT_TRUNCATED)
1485         arg1 %= width;
1486 #endif
1487
1488       val = arg0s >> arg1;
1489
1490       /* Bootstrap compiler may not have sign extended the right shift.
1491          Manually extend the sign to insure bootstrap cc matches gcc.  */
1492       if (arg0s < 0 && arg1 > 0)
1493         val |= ((HOST_WIDE_INT) -1) << (HOST_BITS_PER_WIDE_INT - arg1);
1494
1495       break;
1496
1497     case ROTATERT:
1498       if (arg1 < 0)
1499         return 0;
1500
1501       arg1 %= width;
1502       val = ((((unsigned HOST_WIDE_INT) arg0) << (width - arg1))
1503              | (((unsigned HOST_WIDE_INT) arg0) >> arg1));
1504       break;
1505
1506     case ROTATE:
1507       if (arg1 < 0)
1508         return 0;
1509
1510       arg1 %= width;
1511       val = ((((unsigned HOST_WIDE_INT) arg0) << arg1)
1512              | (((unsigned HOST_WIDE_INT) arg0) >> (width - arg1)));
1513       break;
1514
1515     case COMPARE:
1516       /* Do nothing here.  */
1517       return 0;
1518
1519     case SMIN:
1520       val = arg0s <= arg1s ? arg0s : arg1s;
1521       break;
1522
1523     case UMIN:
1524       val = ((unsigned HOST_WIDE_INT) arg0
1525              <= (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1526       break;
1527
1528     case SMAX:
1529       val = arg0s > arg1s ? arg0s : arg1s;
1530       break;
1531
1532     case UMAX:
1533       val = ((unsigned HOST_WIDE_INT) arg0
1534              > (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1535       break;
1536
1537     default:
1538       abort ();
1539     }
1540
1541   val = trunc_int_for_mode (val, mode);
1542
1543   return GEN_INT (val);
1544 }
1545 \f
1546 /* Simplify a PLUS or MINUS, at least one of whose operands may be another
1547    PLUS or MINUS.
1548
1549    Rather than test for specific case, we do this by a brute-force method
1550    and do all possible simplifications until no more changes occur.  Then
1551    we rebuild the operation.  */
1552
1553 static rtx
1554 simplify_plus_minus (code, mode, op0, op1)
1555      enum rtx_code code;
1556      enum machine_mode mode;
1557      rtx op0, op1;
1558 {
1559   rtx ops[8];
1560   int negs[8];
1561   rtx result, tem;
1562   int n_ops = 2, input_ops = 2, input_consts = 0, n_consts = 0;
1563   int first = 1, negate = 0, changed;
1564   int i, j;
1565
1566   memset ((char *) ops, 0, sizeof ops);
1567   
1568   /* Set up the two operands and then expand them until nothing has been
1569      changed.  If we run out of room in our array, give up; this should
1570      almost never happen.  */
1571
1572   ops[0] = op0, ops[1] = op1, negs[0] = 0, negs[1] = (code == MINUS);
1573
1574   changed = 1;
1575   while (changed)
1576     {
1577       changed = 0;
1578
1579       for (i = 0; i < n_ops; i++)
1580         switch (GET_CODE (ops[i]))
1581           {
1582           case PLUS:
1583           case MINUS:
1584             if (n_ops == 7)
1585               return 0;
1586
1587             ops[n_ops] = XEXP (ops[i], 1);
1588             negs[n_ops++] = GET_CODE (ops[i]) == MINUS ? !negs[i] : negs[i];
1589             ops[i] = XEXP (ops[i], 0);
1590             input_ops++;
1591             changed = 1;
1592             break;
1593
1594           case NEG:
1595             ops[i] = XEXP (ops[i], 0);
1596             negs[i] = ! negs[i];
1597             changed = 1;
1598             break;
1599
1600           case CONST:
1601             ops[i] = XEXP (ops[i], 0);
1602             input_consts++;
1603             changed = 1;
1604             break;
1605
1606           case NOT:
1607             /* ~a -> (-a - 1) */
1608             if (n_ops != 7)
1609               {
1610                 ops[n_ops] = constm1_rtx;
1611                 negs[n_ops++] = negs[i];
1612                 ops[i] = XEXP (ops[i], 0);
1613                 negs[i] = ! negs[i];
1614                 changed = 1;
1615               }
1616             break;
1617
1618           case CONST_INT:
1619             if (negs[i])
1620               ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0, changed = 1;
1621             break;
1622
1623           default:
1624             break;
1625           }
1626     }
1627
1628   /* If we only have two operands, we can't do anything.  */
1629   if (n_ops <= 2)
1630     return 0;
1631
1632   /* Now simplify each pair of operands until nothing changes.  The first
1633      time through just simplify constants against each other.  */
1634
1635   changed = 1;
1636   while (changed)
1637     {
1638       changed = first;
1639
1640       for (i = 0; i < n_ops - 1; i++)
1641         for (j = i + 1; j < n_ops; j++)
1642           if (ops[i] != 0 && ops[j] != 0
1643               && (! first || (CONSTANT_P (ops[i]) && CONSTANT_P (ops[j]))))
1644             {
1645               rtx lhs = ops[i], rhs = ops[j];
1646               enum rtx_code ncode = PLUS;
1647
1648               if (negs[i] && ! negs[j])
1649                 lhs = ops[j], rhs = ops[i], ncode = MINUS;
1650               else if (! negs[i] && negs[j])
1651                 ncode = MINUS;
1652
1653               tem = simplify_binary_operation (ncode, mode, lhs, rhs);
1654               if (tem)
1655                 {
1656                   ops[i] = tem, ops[j] = 0;
1657                   negs[i] = negs[i] && negs[j];
1658                   if (GET_CODE (tem) == NEG)
1659                     ops[i] = XEXP (tem, 0), negs[i] = ! negs[i];
1660
1661                   if (GET_CODE (ops[i]) == CONST_INT && negs[i])
1662                     ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0;
1663                   changed = 1;
1664                 }
1665             }
1666
1667       first = 0;
1668     }
1669
1670   /* Pack all the operands to the lower-numbered entries and give up if
1671      we didn't reduce the number of operands we had.  Make sure we
1672      count a CONST as two operands.  If we have the same number of
1673      operands, but have made more CONSTs than we had, this is also
1674      an improvement, so accept it.  */
1675
1676   for (i = 0, j = 0; j < n_ops; j++)
1677     if (ops[j] != 0)
1678       {
1679         ops[i] = ops[j], negs[i++] = negs[j];
1680         if (GET_CODE (ops[j]) == CONST)
1681           n_consts++;
1682       }
1683
1684   if (i + n_consts > input_ops
1685       || (i + n_consts == input_ops && n_consts <= input_consts))
1686     return 0;
1687
1688   n_ops = i;
1689
1690   /* If we have a CONST_INT, put it last.  */
1691   for (i = 0; i < n_ops - 1; i++)
1692     if (GET_CODE (ops[i]) == CONST_INT)
1693       {
1694         tem = ops[n_ops - 1], ops[n_ops - 1] = ops[i] , ops[i] = tem;
1695         j = negs[n_ops - 1], negs[n_ops - 1] = negs[i], negs[i] = j;
1696       }
1697
1698   /* Put a non-negated operand first.  If there aren't any, make all
1699      operands positive and negate the whole thing later.  */
1700   for (i = 0; i < n_ops && negs[i]; i++)
1701     ;
1702
1703   if (i == n_ops)
1704     {
1705       for (i = 0; i < n_ops; i++)
1706         negs[i] = 0;
1707       negate = 1;
1708     }
1709   else if (i != 0)
1710     {
1711       tem = ops[0], ops[0] = ops[i], ops[i] = tem;
1712       j = negs[0], negs[0] = negs[i], negs[i] = j;
1713     }
1714
1715   /* Now make the result by performing the requested operations.  */
1716   result = ops[0];
1717   for (i = 1; i < n_ops; i++)
1718     result = simplify_gen_binary (negs[i] ? MINUS : PLUS, mode, result, ops[i]);
1719
1720   return negate ? gen_rtx_NEG (mode, result) : result;
1721 }
1722
1723 struct cfc_args
1724 {
1725   rtx op0, op1;                 /* Input */
1726   int equal, op0lt, op1lt;      /* Output */
1727   int unordered;
1728 };
1729
1730 static void
1731 check_fold_consts (data)
1732   PTR data;
1733 {
1734   struct cfc_args *args = (struct cfc_args *) data;
1735   REAL_VALUE_TYPE d0, d1;
1736
1737   /* We may possibly raise an exception while reading the value.  */
1738   args->unordered = 1;
1739   REAL_VALUE_FROM_CONST_DOUBLE (d0, args->op0);
1740   REAL_VALUE_FROM_CONST_DOUBLE (d1, args->op1);
1741
1742   /* Comparisons of Inf versus Inf are ordered.  */
1743   if (REAL_VALUE_ISNAN (d0)
1744       || REAL_VALUE_ISNAN (d1))
1745     return;
1746   args->equal = REAL_VALUES_EQUAL (d0, d1);
1747   args->op0lt = REAL_VALUES_LESS (d0, d1);
1748   args->op1lt = REAL_VALUES_LESS (d1, d0);
1749   args->unordered = 0;
1750 }
1751
1752 /* Like simplify_binary_operation except used for relational operators.
1753    MODE is the mode of the operands, not that of the result.  If MODE
1754    is VOIDmode, both operands must also be VOIDmode and we compare the
1755    operands in "infinite precision".
1756
1757    If no simplification is possible, this function returns zero.  Otherwise,
1758    it returns either const_true_rtx or const0_rtx.  */
1759
1760 rtx
1761 simplify_relational_operation (code, mode, op0, op1)
1762      enum rtx_code code;
1763      enum machine_mode mode;
1764      rtx op0, op1;
1765 {
1766   int equal, op0lt, op0ltu, op1lt, op1ltu;
1767   rtx tem;
1768
1769   if (mode == VOIDmode
1770       && (GET_MODE (op0) != VOIDmode
1771           || GET_MODE (op1) != VOIDmode))
1772     abort ();
1773
1774   /* If op0 is a compare, extract the comparison arguments from it.  */
1775   if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
1776     op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
1777
1778   /* We can't simplify MODE_CC values since we don't know what the
1779      actual comparison is.  */
1780   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC
1781 #ifdef HAVE_cc0
1782       || op0 == cc0_rtx
1783 #endif
1784       )
1785     return 0;
1786
1787   /* Make sure the constant is second.  */
1788   if ((CONSTANT_P (op0) && ! CONSTANT_P (op1))
1789       || (GET_CODE (op0) == CONST_INT && GET_CODE (op1) != CONST_INT))
1790     {
1791       tem = op0, op0 = op1, op1 = tem;
1792       code = swap_condition (code);
1793     }
1794
1795   /* For integer comparisons of A and B maybe we can simplify A - B and can
1796      then simplify a comparison of that with zero.  If A and B are both either
1797      a register or a CONST_INT, this can't help; testing for these cases will
1798      prevent infinite recursion here and speed things up.
1799
1800      If CODE is an unsigned comparison, then we can never do this optimization,
1801      because it gives an incorrect result if the subtraction wraps around zero.
1802      ANSI C defines unsigned operations such that they never overflow, and
1803      thus such cases can not be ignored.  */
1804
1805   if (INTEGRAL_MODE_P (mode) && op1 != const0_rtx
1806       && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == CONST_INT)
1807             && (GET_CODE (op1) == REG || GET_CODE (op1) == CONST_INT))
1808       && 0 != (tem = simplify_binary_operation (MINUS, mode, op0, op1))
1809       && code != GTU && code != GEU && code != LTU && code != LEU)
1810     return simplify_relational_operation (signed_condition (code),
1811                                           mode, tem, const0_rtx);
1812
1813   if (flag_unsafe_math_optimizations && code == ORDERED)
1814     return const_true_rtx;
1815
1816   if (flag_unsafe_math_optimizations && code == UNORDERED)
1817     return const0_rtx;
1818
1819   /* For non-IEEE floating-point, if the two operands are equal, we know the
1820      result.  */
1821   if (rtx_equal_p (op0, op1)
1822       && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1823           || ! FLOAT_MODE_P (GET_MODE (op0)) 
1824           || flag_unsafe_math_optimizations))
1825     equal = 1, op0lt = 0, op0ltu = 0, op1lt = 0, op1ltu = 0;
1826
1827   /* If the operands are floating-point constants, see if we can fold
1828      the result.  */
1829 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1830   else if (GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE
1831            && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
1832     {
1833       struct cfc_args args;
1834
1835       /* Setup input for check_fold_consts() */
1836       args.op0 = op0;
1837       args.op1 = op1;
1838       
1839       
1840       if (!do_float_handler (check_fold_consts, (PTR) &args))
1841         args.unordered = 1;
1842
1843       if (args.unordered)
1844         switch (code)
1845           {
1846           case UNEQ:
1847           case UNLT:
1848           case UNGT:
1849           case UNLE:
1850           case UNGE:
1851           case NE:
1852           case UNORDERED:
1853             return const_true_rtx;
1854           case EQ:
1855           case LT:
1856           case GT:
1857           case LE:
1858           case GE:
1859           case LTGT:
1860           case ORDERED:
1861             return const0_rtx;
1862           default:
1863             return 0;
1864           }
1865
1866       /* Receive output from check_fold_consts() */
1867       equal = args.equal;
1868       op0lt = op0ltu = args.op0lt;
1869       op1lt = op1ltu = args.op1lt;
1870     }
1871 #endif  /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1872
1873   /* Otherwise, see if the operands are both integers.  */
1874   else if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
1875            && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT)
1876            && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT))
1877     {
1878       int width = GET_MODE_BITSIZE (mode);
1879       HOST_WIDE_INT l0s, h0s, l1s, h1s;
1880       unsigned HOST_WIDE_INT l0u, h0u, l1u, h1u;
1881
1882       /* Get the two words comprising each integer constant.  */
1883       if (GET_CODE (op0) == CONST_DOUBLE)
1884         {
1885           l0u = l0s = CONST_DOUBLE_LOW (op0);
1886           h0u = h0s = CONST_DOUBLE_HIGH (op0);
1887         }
1888       else
1889         {
1890           l0u = l0s = INTVAL (op0);
1891           h0u = h0s = HWI_SIGN_EXTEND (l0s);
1892         }
1893           
1894       if (GET_CODE (op1) == CONST_DOUBLE)
1895         {
1896           l1u = l1s = CONST_DOUBLE_LOW (op1);
1897           h1u = h1s = CONST_DOUBLE_HIGH (op1);
1898         }
1899       else
1900         {
1901           l1u = l1s = INTVAL (op1);
1902           h1u = h1s = HWI_SIGN_EXTEND (l1s);
1903         }
1904
1905       /* If WIDTH is nonzero and smaller than HOST_BITS_PER_WIDE_INT,
1906          we have to sign or zero-extend the values.  */
1907       if (width != 0 && width < HOST_BITS_PER_WIDE_INT)
1908         {
1909           l0u &= ((HOST_WIDE_INT) 1 << width) - 1;
1910           l1u &= ((HOST_WIDE_INT) 1 << width) - 1;
1911
1912           if (l0s & ((HOST_WIDE_INT) 1 << (width - 1)))
1913             l0s |= ((HOST_WIDE_INT) (-1) << width);
1914
1915           if (l1s & ((HOST_WIDE_INT) 1 << (width - 1)))
1916             l1s |= ((HOST_WIDE_INT) (-1) << width);
1917         }
1918       if (width != 0 && width <= HOST_BITS_PER_WIDE_INT)
1919         h0u = h1u = 0, h0s = HWI_SIGN_EXTEND (l0s), h1s = HWI_SIGN_EXTEND (l1s);
1920
1921       equal = (h0u == h1u && l0u == l1u);
1922       op0lt = (h0s < h1s || (h0s == h1s && l0u < l1u));
1923       op1lt = (h1s < h0s || (h1s == h0s && l1u < l0u));
1924       op0ltu = (h0u < h1u || (h0u == h1u && l0u < l1u));
1925       op1ltu = (h1u < h0u || (h1u == h0u && l1u < l0u));
1926     }
1927
1928   /* Otherwise, there are some code-specific tests we can make.  */
1929   else
1930     {
1931       switch (code)
1932         {
1933         case EQ:
1934           /* References to the frame plus a constant or labels cannot
1935              be zero, but a SYMBOL_REF can due to #pragma weak.  */
1936           if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx)
1937                || GET_CODE (op0) == LABEL_REF)
1938 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1939               /* On some machines, the ap reg can be 0 sometimes.  */
1940               && op0 != arg_pointer_rtx
1941 #endif
1942                 )
1943             return const0_rtx;
1944           break;
1945
1946         case NE:
1947           if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx)
1948                || GET_CODE (op0) == LABEL_REF)
1949 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1950               && op0 != arg_pointer_rtx
1951 #endif
1952               )
1953             return const_true_rtx;
1954           break;
1955
1956         case GEU:
1957           /* Unsigned values are never negative.  */
1958           if (op1 == const0_rtx)
1959             return const_true_rtx;
1960           break;
1961
1962         case LTU:
1963           if (op1 == const0_rtx)
1964             return const0_rtx;
1965           break;
1966
1967         case LEU:
1968           /* Unsigned values are never greater than the largest
1969              unsigned value.  */
1970           if (GET_CODE (op1) == CONST_INT
1971               && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
1972             && INTEGRAL_MODE_P (mode))
1973           return const_true_rtx;
1974           break;
1975
1976         case GTU:
1977           if (GET_CODE (op1) == CONST_INT
1978               && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
1979               && INTEGRAL_MODE_P (mode))
1980             return const0_rtx;
1981           break;
1982           
1983         default:
1984           break;
1985         }
1986
1987       return 0;
1988     }
1989
1990   /* If we reach here, EQUAL, OP0LT, OP0LTU, OP1LT, and OP1LTU are set
1991      as appropriate.  */
1992   switch (code)
1993     {
1994     case EQ:
1995     case UNEQ:
1996       return equal ? const_true_rtx : const0_rtx;
1997     case NE:
1998     case LTGT:
1999       return ! equal ? const_true_rtx : const0_rtx;
2000     case LT:
2001     case UNLT:
2002       return op0lt ? const_true_rtx : const0_rtx;
2003     case GT:
2004     case UNGT:
2005       return op1lt ? const_true_rtx : const0_rtx;
2006     case LTU:
2007       return op0ltu ? const_true_rtx : const0_rtx;
2008     case GTU:
2009       return op1ltu ? const_true_rtx : const0_rtx;
2010     case LE:
2011     case UNLE:
2012       return equal || op0lt ? const_true_rtx : const0_rtx;
2013     case GE:
2014     case UNGE:
2015       return equal || op1lt ? const_true_rtx : const0_rtx;
2016     case LEU:
2017       return equal || op0ltu ? const_true_rtx : const0_rtx;
2018     case GEU:
2019       return equal || op1ltu ? const_true_rtx : const0_rtx;
2020     case ORDERED:
2021       return const_true_rtx;
2022     case UNORDERED:
2023       return const0_rtx;
2024     default:
2025       abort ();
2026     }
2027 }
2028 \f
2029 /* Simplify CODE, an operation with result mode MODE and three operands,
2030    OP0, OP1, and OP2.  OP0_MODE was the mode of OP0 before it became
2031    a constant.  Return 0 if no simplifications is possible.  */
2032
2033 rtx
2034 simplify_ternary_operation (code, mode, op0_mode, op0, op1, op2)
2035      enum rtx_code code;
2036      enum machine_mode mode, op0_mode;
2037      rtx op0, op1, op2;
2038 {
2039   unsigned int width = GET_MODE_BITSIZE (mode);
2040
2041   /* VOIDmode means "infinite" precision.  */
2042   if (width == 0)
2043     width = HOST_BITS_PER_WIDE_INT;
2044
2045   switch (code)
2046     {
2047     case SIGN_EXTRACT:
2048     case ZERO_EXTRACT:
2049       if (GET_CODE (op0) == CONST_INT
2050           && GET_CODE (op1) == CONST_INT
2051           && GET_CODE (op2) == CONST_INT
2052           && ((unsigned) INTVAL (op1) + (unsigned) INTVAL (op2) <= width)
2053           && width <= (unsigned) HOST_BITS_PER_WIDE_INT)
2054         {
2055           /* Extracting a bit-field from a constant */
2056           HOST_WIDE_INT val = INTVAL (op0);
2057
2058           if (BITS_BIG_ENDIAN)
2059             val >>= (GET_MODE_BITSIZE (op0_mode)
2060                      - INTVAL (op2) - INTVAL (op1));
2061           else
2062             val >>= INTVAL (op2);
2063
2064           if (HOST_BITS_PER_WIDE_INT != INTVAL (op1))
2065             {
2066               /* First zero-extend.  */
2067               val &= ((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1;
2068               /* If desired, propagate sign bit.  */
2069               if (code == SIGN_EXTRACT
2070                   && (val & ((HOST_WIDE_INT) 1 << (INTVAL (op1) - 1))))
2071                 val |= ~ (((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1);
2072             }
2073
2074           /* Clear the bits that don't belong in our mode,
2075              unless they and our sign bit are all one.
2076              So we get either a reasonable negative value or a reasonable
2077              unsigned value for this mode.  */
2078           if (width < HOST_BITS_PER_WIDE_INT
2079               && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
2080                   != ((HOST_WIDE_INT) (-1) << (width - 1))))
2081             val &= ((HOST_WIDE_INT) 1 << width) - 1;
2082
2083           return GEN_INT (val);
2084         }
2085       break;
2086
2087     case IF_THEN_ELSE:
2088       if (GET_CODE (op0) == CONST_INT)
2089         return op0 != const0_rtx ? op1 : op2;
2090
2091       /* Convert a == b ? b : a to "a".  */
2092       if (GET_CODE (op0) == NE && ! side_effects_p (op0)
2093           && (! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
2094           && rtx_equal_p (XEXP (op0, 0), op1)
2095           && rtx_equal_p (XEXP (op0, 1), op2))
2096         return op1;
2097       else if (GET_CODE (op0) == EQ && ! side_effects_p (op0)
2098           && (! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
2099           && rtx_equal_p (XEXP (op0, 1), op1)
2100           && rtx_equal_p (XEXP (op0, 0), op2))
2101         return op2;
2102       else if (GET_RTX_CLASS (GET_CODE (op0)) == '<' && ! side_effects_p (op0))
2103         {
2104           enum machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
2105                                         ? GET_MODE (XEXP (op0, 1))
2106                                         : GET_MODE (XEXP (op0, 0)));
2107           rtx temp;
2108           if (cmp_mode == VOIDmode)
2109             cmp_mode = op0_mode;
2110           temp = simplify_relational_operation (GET_CODE (op0), cmp_mode,
2111                                                 XEXP (op0, 0), XEXP (op0, 1));
2112
2113           /* See if any simplifications were possible.  */
2114           if (temp == const0_rtx)
2115             return op2;
2116           else if (temp == const1_rtx)
2117             return op1;
2118           else if (temp)
2119             op0 = temp;
2120
2121           /* Look for happy constants in op1 and op2.  */
2122           if (GET_CODE (op1) == CONST_INT && GET_CODE (op2) == CONST_INT)
2123             {
2124               HOST_WIDE_INT t = INTVAL (op1);
2125               HOST_WIDE_INT f = INTVAL (op2);
2126               
2127               if (t == STORE_FLAG_VALUE && f == 0)
2128                 code = GET_CODE (op0);
2129               else if (t == 0 && f == STORE_FLAG_VALUE)
2130                 {
2131                   enum rtx_code tmp;
2132                   tmp = reversed_comparison_code (op0, NULL_RTX);
2133                   if (tmp == UNKNOWN)
2134                     break;
2135                   code = tmp;
2136                 }
2137               else
2138                 break;
2139
2140               return gen_rtx_fmt_ee (code, mode, XEXP (op0, 0), XEXP (op0, 1));
2141             }
2142         }
2143       break;
2144
2145     default:
2146       abort ();
2147     }
2148
2149   return 0;
2150 }
2151
2152 /* Simplify X, an rtx expression.
2153
2154    Return the simplified expression or NULL if no simplifications
2155    were possible.
2156
2157    This is the preferred entry point into the simplification routines;
2158    however, we still allow passes to call the more specific routines.
2159
2160    Right now GCC has three (yes, three) major bodies of RTL simplficiation
2161    code that need to be unified.
2162
2163         1. fold_rtx in cse.c.  This code uses various CSE specific
2164            information to aid in RTL simplification.
2165
2166         2. simplify_rtx in combine.c.  Similar to fold_rtx, except that
2167            it uses combine specific information to aid in RTL
2168            simplification.
2169
2170         3. The routines in this file.
2171
2172
2173    Long term we want to only have one body of simplification code; to
2174    get to that state I recommend the following steps:
2175
2176         1. Pour over fold_rtx & simplify_rtx and move any simplifications
2177            which are not pass dependent state into these routines.
2178
2179         2. As code is moved by #1, change fold_rtx & simplify_rtx to
2180            use this routine whenever possible.
2181
2182         3. Allow for pass dependent state to be provided to these
2183            routines and add simplifications based on the pass dependent
2184            state.  Remove code from cse.c & combine.c that becomes
2185            redundant/dead.
2186
2187     It will take time, but ultimately the compiler will be easier to
2188     maintain and improve.  It's totally silly that when we add a
2189     simplification that it needs to be added to 4 places (3 for RTL
2190     simplification and 1 for tree simplification.  */
2191            
2192 rtx
2193 simplify_rtx (x)
2194      rtx x;
2195 {
2196   enum rtx_code code = GET_CODE (x);
2197   enum machine_mode mode = GET_MODE (x);
2198
2199   switch (GET_RTX_CLASS (code))
2200     {
2201     case '1':
2202       return simplify_unary_operation (code, mode,
2203                                        XEXP (x, 0), GET_MODE (XEXP (x, 0)));
2204     case '2':
2205     case 'c':
2206       return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
2207
2208     case '3':
2209     case 'b':
2210       return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
2211                                          XEXP (x, 0), XEXP (x, 1),
2212                                          XEXP (x, 2));
2213
2214     case '<':
2215       return simplify_relational_operation (code,
2216                                             ((GET_MODE (XEXP (x, 0))
2217                                               != VOIDmode)
2218                                              ? GET_MODE (XEXP (x, 0))
2219                                              : GET_MODE (XEXP (x, 1))),
2220                                             XEXP (x, 0), XEXP (x, 1));
2221     default:
2222       return NULL;
2223     }
2224 }