OSDN Git Service

* rtlanal.c (find_reg_equal_equiv_note): New function.
[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_fast_math)
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_fast_math)
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_fast_math)
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 -ffast-math.  */
1043           if (rtx_equal_p (op0, op1)
1044               && ! side_effects_p (op0)
1045               && (! FLOAT_MODE_P (mode) || flag_fast_math))
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_fast_math)
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_fast_math)
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 -ffast-math until an expert says it is safe in
1271              general.  */
1272           else if (GET_CODE (op1) == CONST_DOUBLE
1273                    && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT
1274                    && op1 != CONST0_RTX (mode)
1275                    && flag_fast_math)
1276             {
1277               REAL_VALUE_TYPE d;
1278               REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
1279
1280               if (! REAL_VALUES_EQUAL (d, dconst0))
1281                 {
1282 #if defined (REAL_ARITHMETIC)
1283                   REAL_ARITHMETIC (d, rtx_to_tree_code (DIV), dconst1, d);
1284                   return gen_rtx_MULT (mode, op0, 
1285                                        CONST_DOUBLE_FROM_REAL_VALUE (d, mode));
1286 #else
1287                   return
1288                     gen_rtx_MULT (mode, op0, 
1289                                   CONST_DOUBLE_FROM_REAL_VALUE (1./d, mode));
1290 #endif
1291                 }
1292             }
1293 #endif
1294           break;
1295
1296         case UMOD:
1297           /* Handle modulus by power of two (mod with 1 handled below).  */
1298           if (GET_CODE (op1) == CONST_INT
1299               && exact_log2 (INTVAL (op1)) > 0)
1300             return gen_rtx_AND (mode, op0, GEN_INT (INTVAL (op1) - 1));
1301
1302           /* ... fall through ...  */
1303
1304         case MOD:
1305           if ((op0 == const0_rtx || op1 == const1_rtx)
1306               && ! side_effects_p (op0) && ! side_effects_p (op1))
1307             return const0_rtx;
1308           break;
1309
1310         case ROTATERT:
1311         case ROTATE:
1312           /* Rotating ~0 always results in ~0.  */
1313           if (GET_CODE (op0) == CONST_INT && width <= HOST_BITS_PER_WIDE_INT
1314               && (unsigned HOST_WIDE_INT) INTVAL (op0) == GET_MODE_MASK (mode)
1315               && ! side_effects_p (op1))
1316             return op0;
1317
1318           /* ... fall through ...  */
1319
1320         case ASHIFT:
1321         case ASHIFTRT:
1322         case LSHIFTRT:
1323           if (op1 == const0_rtx)
1324             return op0;
1325           if (op0 == const0_rtx && ! side_effects_p (op1))
1326             return op0;
1327           break;
1328
1329         case SMIN:
1330           if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT 
1331               && INTVAL (op1) == (HOST_WIDE_INT) 1 << (width -1)
1332               && ! side_effects_p (op0))
1333             return op1;
1334           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1335             return op0;
1336           break;
1337            
1338         case SMAX:
1339           if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT
1340               && ((unsigned HOST_WIDE_INT) INTVAL (op1)
1341                   == (unsigned HOST_WIDE_INT) GET_MODE_MASK (mode) >> 1)
1342               && ! side_effects_p (op0))
1343             return op1;
1344           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1345             return op0;
1346           break;
1347
1348         case UMIN:
1349           if (op1 == const0_rtx && ! side_effects_p (op0))
1350             return op1;
1351           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1352             return op0;
1353           break;
1354             
1355         case UMAX:
1356           if (op1 == constm1_rtx && ! side_effects_p (op0))
1357             return op1;
1358           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1359             return op0;
1360           break;
1361
1362         default:
1363           abort ();
1364         }
1365       
1366       return 0;
1367     }
1368
1369   /* Get the integer argument values in two forms:
1370      zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S.  */
1371
1372   arg0 = INTVAL (op0);
1373   arg1 = INTVAL (op1);
1374
1375   if (width < HOST_BITS_PER_WIDE_INT)
1376     {
1377       arg0 &= ((HOST_WIDE_INT) 1 << width) - 1;
1378       arg1 &= ((HOST_WIDE_INT) 1 << width) - 1;
1379
1380       arg0s = arg0;
1381       if (arg0s & ((HOST_WIDE_INT) 1 << (width - 1)))
1382         arg0s |= ((HOST_WIDE_INT) (-1) << width);
1383
1384       arg1s = arg1;
1385       if (arg1s & ((HOST_WIDE_INT) 1 << (width - 1)))
1386         arg1s |= ((HOST_WIDE_INT) (-1) << width);
1387     }
1388   else
1389     {
1390       arg0s = arg0;
1391       arg1s = arg1;
1392     }
1393
1394   /* Compute the value of the arithmetic.  */
1395
1396   switch (code)
1397     {
1398     case PLUS:
1399       val = arg0s + arg1s;
1400       break;
1401
1402     case MINUS:
1403       val = arg0s - arg1s;
1404       break;
1405
1406     case MULT:
1407       val = arg0s * arg1s;
1408       break;
1409
1410     case DIV:
1411       if (arg1s == 0)
1412         return 0;
1413       val = arg0s / arg1s;
1414       break;
1415
1416     case MOD:
1417       if (arg1s == 0)
1418         return 0;
1419       val = arg0s % arg1s;
1420       break;
1421
1422     case UDIV:
1423       if (arg1 == 0)
1424         return 0;
1425       val = (unsigned HOST_WIDE_INT) arg0 / arg1;
1426       break;
1427
1428     case UMOD:
1429       if (arg1 == 0)
1430         return 0;
1431       val = (unsigned HOST_WIDE_INT) arg0 % arg1;
1432       break;
1433
1434     case AND:
1435       val = arg0 & arg1;
1436       break;
1437
1438     case IOR:
1439       val = arg0 | arg1;
1440       break;
1441
1442     case XOR:
1443       val = arg0 ^ arg1;
1444       break;
1445
1446     case LSHIFTRT:
1447       /* If shift count is undefined, don't fold it; let the machine do
1448          what it wants.  But truncate it if the machine will do that.  */
1449       if (arg1 < 0)
1450         return 0;
1451
1452 #ifdef SHIFT_COUNT_TRUNCATED
1453       if (SHIFT_COUNT_TRUNCATED)
1454         arg1 %= width;
1455 #endif
1456
1457       val = ((unsigned HOST_WIDE_INT) arg0) >> arg1;
1458       break;
1459
1460     case ASHIFT:
1461       if (arg1 < 0)
1462         return 0;
1463
1464 #ifdef SHIFT_COUNT_TRUNCATED
1465       if (SHIFT_COUNT_TRUNCATED)
1466         arg1 %= width;
1467 #endif
1468
1469       val = ((unsigned HOST_WIDE_INT) arg0) << arg1;
1470       break;
1471
1472     case ASHIFTRT:
1473       if (arg1 < 0)
1474         return 0;
1475
1476 #ifdef SHIFT_COUNT_TRUNCATED
1477       if (SHIFT_COUNT_TRUNCATED)
1478         arg1 %= width;
1479 #endif
1480
1481       val = arg0s >> arg1;
1482
1483       /* Bootstrap compiler may not have sign extended the right shift.
1484          Manually extend the sign to insure bootstrap cc matches gcc.  */
1485       if (arg0s < 0 && arg1 > 0)
1486         val |= ((HOST_WIDE_INT) -1) << (HOST_BITS_PER_WIDE_INT - arg1);
1487
1488       break;
1489
1490     case ROTATERT:
1491       if (arg1 < 0)
1492         return 0;
1493
1494       arg1 %= width;
1495       val = ((((unsigned HOST_WIDE_INT) arg0) << (width - arg1))
1496              | (((unsigned HOST_WIDE_INT) arg0) >> arg1));
1497       break;
1498
1499     case ROTATE:
1500       if (arg1 < 0)
1501         return 0;
1502
1503       arg1 %= width;
1504       val = ((((unsigned HOST_WIDE_INT) arg0) << arg1)
1505              | (((unsigned HOST_WIDE_INT) arg0) >> (width - arg1)));
1506       break;
1507
1508     case COMPARE:
1509       /* Do nothing here.  */
1510       return 0;
1511
1512     case SMIN:
1513       val = arg0s <= arg1s ? arg0s : arg1s;
1514       break;
1515
1516     case UMIN:
1517       val = ((unsigned HOST_WIDE_INT) arg0
1518              <= (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1519       break;
1520
1521     case SMAX:
1522       val = arg0s > arg1s ? arg0s : arg1s;
1523       break;
1524
1525     case UMAX:
1526       val = ((unsigned HOST_WIDE_INT) arg0
1527              > (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1528       break;
1529
1530     default:
1531       abort ();
1532     }
1533
1534   val = trunc_int_for_mode (val, mode);
1535
1536   return GEN_INT (val);
1537 }
1538 \f
1539 /* Simplify a PLUS or MINUS, at least one of whose operands may be another
1540    PLUS or MINUS.
1541
1542    Rather than test for specific case, we do this by a brute-force method
1543    and do all possible simplifications until no more changes occur.  Then
1544    we rebuild the operation.  */
1545
1546 static rtx
1547 simplify_plus_minus (code, mode, op0, op1)
1548      enum rtx_code code;
1549      enum machine_mode mode;
1550      rtx op0, op1;
1551 {
1552   rtx ops[8];
1553   int negs[8];
1554   rtx result, tem;
1555   int n_ops = 2, input_ops = 2, input_consts = 0, n_consts = 0;
1556   int first = 1, negate = 0, changed;
1557   int i, j;
1558
1559   memset ((char *) ops, 0, sizeof ops);
1560   
1561   /* Set up the two operands and then expand them until nothing has been
1562      changed.  If we run out of room in our array, give up; this should
1563      almost never happen.  */
1564
1565   ops[0] = op0, ops[1] = op1, negs[0] = 0, negs[1] = (code == MINUS);
1566
1567   changed = 1;
1568   while (changed)
1569     {
1570       changed = 0;
1571
1572       for (i = 0; i < n_ops; i++)
1573         switch (GET_CODE (ops[i]))
1574           {
1575           case PLUS:
1576           case MINUS:
1577             if (n_ops == 7)
1578               return 0;
1579
1580             ops[n_ops] = XEXP (ops[i], 1);
1581             negs[n_ops++] = GET_CODE (ops[i]) == MINUS ? !negs[i] : negs[i];
1582             ops[i] = XEXP (ops[i], 0);
1583             input_ops++;
1584             changed = 1;
1585             break;
1586
1587           case NEG:
1588             ops[i] = XEXP (ops[i], 0);
1589             negs[i] = ! negs[i];
1590             changed = 1;
1591             break;
1592
1593           case CONST:
1594             ops[i] = XEXP (ops[i], 0);
1595             input_consts++;
1596             changed = 1;
1597             break;
1598
1599           case NOT:
1600             /* ~a -> (-a - 1) */
1601             if (n_ops != 7)
1602               {
1603                 ops[n_ops] = constm1_rtx;
1604                 negs[n_ops++] = negs[i];
1605                 ops[i] = XEXP (ops[i], 0);
1606                 negs[i] = ! negs[i];
1607                 changed = 1;
1608               }
1609             break;
1610
1611           case CONST_INT:
1612             if (negs[i])
1613               ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0, changed = 1;
1614             break;
1615
1616           default:
1617             break;
1618           }
1619     }
1620
1621   /* If we only have two operands, we can't do anything.  */
1622   if (n_ops <= 2)
1623     return 0;
1624
1625   /* Now simplify each pair of operands until nothing changes.  The first
1626      time through just simplify constants against each other.  */
1627
1628   changed = 1;
1629   while (changed)
1630     {
1631       changed = first;
1632
1633       for (i = 0; i < n_ops - 1; i++)
1634         for (j = i + 1; j < n_ops; j++)
1635           if (ops[i] != 0 && ops[j] != 0
1636               && (! first || (CONSTANT_P (ops[i]) && CONSTANT_P (ops[j]))))
1637             {
1638               rtx lhs = ops[i], rhs = ops[j];
1639               enum rtx_code ncode = PLUS;
1640
1641               if (negs[i] && ! negs[j])
1642                 lhs = ops[j], rhs = ops[i], ncode = MINUS;
1643               else if (! negs[i] && negs[j])
1644                 ncode = MINUS;
1645
1646               tem = simplify_binary_operation (ncode, mode, lhs, rhs);
1647               if (tem)
1648                 {
1649                   ops[i] = tem, ops[j] = 0;
1650                   negs[i] = negs[i] && negs[j];
1651                   if (GET_CODE (tem) == NEG)
1652                     ops[i] = XEXP (tem, 0), negs[i] = ! negs[i];
1653
1654                   if (GET_CODE (ops[i]) == CONST_INT && negs[i])
1655                     ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0;
1656                   changed = 1;
1657                 }
1658             }
1659
1660       first = 0;
1661     }
1662
1663   /* Pack all the operands to the lower-numbered entries and give up if
1664      we didn't reduce the number of operands we had.  Make sure we
1665      count a CONST as two operands.  If we have the same number of
1666      operands, but have made more CONSTs than we had, this is also
1667      an improvement, so accept it.  */
1668
1669   for (i = 0, j = 0; j < n_ops; j++)
1670     if (ops[j] != 0)
1671       {
1672         ops[i] = ops[j], negs[i++] = negs[j];
1673         if (GET_CODE (ops[j]) == CONST)
1674           n_consts++;
1675       }
1676
1677   if (i + n_consts > input_ops
1678       || (i + n_consts == input_ops && n_consts <= input_consts))
1679     return 0;
1680
1681   n_ops = i;
1682
1683   /* If we have a CONST_INT, put it last.  */
1684   for (i = 0; i < n_ops - 1; i++)
1685     if (GET_CODE (ops[i]) == CONST_INT)
1686       {
1687         tem = ops[n_ops - 1], ops[n_ops - 1] = ops[i] , ops[i] = tem;
1688         j = negs[n_ops - 1], negs[n_ops - 1] = negs[i], negs[i] = j;
1689       }
1690
1691   /* Put a non-negated operand first.  If there aren't any, make all
1692      operands positive and negate the whole thing later.  */
1693   for (i = 0; i < n_ops && negs[i]; i++)
1694     ;
1695
1696   if (i == n_ops)
1697     {
1698       for (i = 0; i < n_ops; i++)
1699         negs[i] = 0;
1700       negate = 1;
1701     }
1702   else if (i != 0)
1703     {
1704       tem = ops[0], ops[0] = ops[i], ops[i] = tem;
1705       j = negs[0], negs[0] = negs[i], negs[i] = j;
1706     }
1707
1708   /* Now make the result by performing the requested operations.  */
1709   result = ops[0];
1710   for (i = 1; i < n_ops; i++)
1711     result = simplify_gen_binary (negs[i] ? MINUS : PLUS, mode, result, ops[i]);
1712
1713   return negate ? gen_rtx_NEG (mode, result) : result;
1714 }
1715
1716 struct cfc_args
1717 {
1718   rtx op0, op1;                 /* Input */
1719   int equal, op0lt, op1lt;      /* Output */
1720   int unordered;
1721 };
1722
1723 static void
1724 check_fold_consts (data)
1725   PTR data;
1726 {
1727   struct cfc_args *args = (struct cfc_args *) data;
1728   REAL_VALUE_TYPE d0, d1;
1729
1730   /* We may possibly raise an exception while reading the value.  */
1731   args->unordered = 1;
1732   REAL_VALUE_FROM_CONST_DOUBLE (d0, args->op0);
1733   REAL_VALUE_FROM_CONST_DOUBLE (d1, args->op1);
1734
1735   /* Comparisons of Inf versus Inf are ordered.  */
1736   if (REAL_VALUE_ISNAN (d0)
1737       || REAL_VALUE_ISNAN (d1))
1738     return;
1739   args->equal = REAL_VALUES_EQUAL (d0, d1);
1740   args->op0lt = REAL_VALUES_LESS (d0, d1);
1741   args->op1lt = REAL_VALUES_LESS (d1, d0);
1742   args->unordered = 0;
1743 }
1744
1745 /* Like simplify_binary_operation except used for relational operators.
1746    MODE is the mode of the operands, not that of the result.  If MODE
1747    is VOIDmode, both operands must also be VOIDmode and we compare the
1748    operands in "infinite precision".
1749
1750    If no simplification is possible, this function returns zero.  Otherwise,
1751    it returns either const_true_rtx or const0_rtx.  */
1752
1753 rtx
1754 simplify_relational_operation (code, mode, op0, op1)
1755      enum rtx_code code;
1756      enum machine_mode mode;
1757      rtx op0, op1;
1758 {
1759   int equal, op0lt, op0ltu, op1lt, op1ltu;
1760   rtx tem;
1761
1762   if (mode == VOIDmode
1763       && (GET_MODE (op0) != VOIDmode
1764           || GET_MODE (op1) != VOIDmode))
1765     abort ();
1766
1767   /* If op0 is a compare, extract the comparison arguments from it.  */
1768   if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
1769     op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
1770
1771   /* We can't simplify MODE_CC values since we don't know what the
1772      actual comparison is.  */
1773   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC
1774 #ifdef HAVE_cc0
1775       || op0 == cc0_rtx
1776 #endif
1777       )
1778     return 0;
1779
1780   /* Make sure the constant is second.  */
1781   if ((CONSTANT_P (op0) && ! CONSTANT_P (op1))
1782       || (GET_CODE (op0) == CONST_INT && GET_CODE (op1) != CONST_INT))
1783     {
1784       tem = op0, op0 = op1, op1 = tem;
1785       code = swap_condition (code);
1786     }
1787
1788   /* For integer comparisons of A and B maybe we can simplify A - B and can
1789      then simplify a comparison of that with zero.  If A and B are both either
1790      a register or a CONST_INT, this can't help; testing for these cases will
1791      prevent infinite recursion here and speed things up.
1792
1793      If CODE is an unsigned comparison, then we can never do this optimization,
1794      because it gives an incorrect result if the subtraction wraps around zero.
1795      ANSI C defines unsigned operations such that they never overflow, and
1796      thus such cases can not be ignored.  */
1797
1798   if (INTEGRAL_MODE_P (mode) && op1 != const0_rtx
1799       && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == CONST_INT)
1800             && (GET_CODE (op1) == REG || GET_CODE (op1) == CONST_INT))
1801       && 0 != (tem = simplify_binary_operation (MINUS, mode, op0, op1))
1802       && code != GTU && code != GEU && code != LTU && code != LEU)
1803     return simplify_relational_operation (signed_condition (code),
1804                                           mode, tem, const0_rtx);
1805
1806   if (flag_fast_math && code == ORDERED)
1807     return const_true_rtx;
1808
1809   if (flag_fast_math && code == UNORDERED)
1810     return const0_rtx;
1811
1812   /* For non-IEEE floating-point, if the two operands are equal, we know the
1813      result.  */
1814   if (rtx_equal_p (op0, op1)
1815       && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1816           || ! FLOAT_MODE_P (GET_MODE (op0)) || flag_fast_math))
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_fast_math)
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_fast_math)
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 }