OSDN Git Service

no xfail
[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 GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "flags.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "ggc.h"
39
40 /* Simplification and canonicalization of RTL.  */
41
42 /* Nonzero if X has the form (PLUS frame-pointer integer).  We check for
43    virtual regs here because the simplify_*_operation routines are called
44    by integrate.c, which is called before virtual register instantiation.
45
46    ?!? FIXED_BASE_PLUS_P and NONZERO_BASE_PLUS_P need to move into 
47    a header file so that their definitions can be shared with the
48    simplification routines in simplify-rtx.c.  Until then, do not
49    change these macros without also changing the copy in simplify-rtx.c.  */
50
51 #define FIXED_BASE_PLUS_P(X)                                    \
52   ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx    \
53    || ((X) == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])\
54    || (X) == virtual_stack_vars_rtx                             \
55    || (X) == virtual_incoming_args_rtx                          \
56    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
57        && (XEXP (X, 0) == frame_pointer_rtx                     \
58            || XEXP (X, 0) == hard_frame_pointer_rtx             \
59            || ((X) == arg_pointer_rtx                           \
60                && fixed_regs[ARG_POINTER_REGNUM])               \
61            || XEXP (X, 0) == virtual_stack_vars_rtx             \
62            || XEXP (X, 0) == virtual_incoming_args_rtx))        \
63    || GET_CODE (X) == ADDRESSOF)
64
65 /* Similar, but also allows reference to the stack pointer.
66
67    This used to include FIXED_BASE_PLUS_P, however, we can't assume that
68    arg_pointer_rtx by itself is nonzero, because on at least one machine,
69    the i960, the arg pointer is zero when it is unused.  */
70
71 #define NONZERO_BASE_PLUS_P(X)                                  \
72   ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx    \
73    || (X) == virtual_stack_vars_rtx                             \
74    || (X) == virtual_incoming_args_rtx                          \
75    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
76        && (XEXP (X, 0) == frame_pointer_rtx                     \
77            || XEXP (X, 0) == hard_frame_pointer_rtx             \
78            || ((X) == arg_pointer_rtx                           \
79                && fixed_regs[ARG_POINTER_REGNUM])               \
80            || XEXP (X, 0) == virtual_stack_vars_rtx             \
81            || XEXP (X, 0) == virtual_incoming_args_rtx))        \
82    || (X) == stack_pointer_rtx                                  \
83    || (X) == virtual_stack_dynamic_rtx                          \
84    || (X) == virtual_outgoing_args_rtx                          \
85    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
86        && (XEXP (X, 0) == stack_pointer_rtx                     \
87            || XEXP (X, 0) == virtual_stack_dynamic_rtx          \
88            || XEXP (X, 0) == virtual_outgoing_args_rtx))        \
89    || GET_CODE (X) == ADDRESSOF)
90
91 /* Much code operates on (low, high) pairs; the low value is an
92    unsigned wide int, the high value a signed wide int.  We
93    occasionally need to sign extend from low to high as if low were a
94    signed wide int.  */
95 #define HWI_SIGN_EXTEND(low) \
96  ((((HOST_WIDE_INT) low) < 0) ? ((HOST_WIDE_INT) -1) : ((HOST_WIDE_INT) 0))
97
98 static rtx neg_const_int PARAMS ((enum machine_mode, rtx));
99 static int simplify_plus_minus_op_data_cmp PARAMS ((const void *,
100                                                     const void *));
101 static rtx simplify_plus_minus          PARAMS ((enum rtx_code,
102                                                  enum machine_mode, rtx, rtx));
103 static void check_fold_consts           PARAMS ((PTR));
104 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
105 static void simplify_unary_real         PARAMS ((PTR));
106 static void simplify_binary_real        PARAMS ((PTR));
107 #endif
108 static void simplify_binary_is2orm1     PARAMS ((PTR));
109
110 \f
111 /* Negate a CONST_INT rtx, truncating (because a conversion from a
112    maximally negative number can overflow).  */
113 static rtx
114 neg_const_int (mode, i)
115      enum machine_mode mode;
116      rtx i;
117 {
118   return GEN_INT (trunc_int_for_mode (- INTVAL (i), mode));
119 }
120
121 \f
122 /* Make a binary operation by properly ordering the operands and 
123    seeing if the expression folds.  */
124
125 rtx
126 simplify_gen_binary (code, mode, op0, op1)
127      enum rtx_code code;
128      enum machine_mode mode;
129      rtx op0, op1;
130 {
131   rtx tem;
132
133   /* Put complex operands first and constants second if commutative.  */
134   if (GET_RTX_CLASS (code) == 'c'
135       && swap_commutative_operands_p (op0, op1))
136     tem = op0, op0 = op1, op1 = tem;
137
138   /* If this simplifies, do it.  */
139   tem = simplify_binary_operation (code, mode, op0, op1);
140
141   if (tem)
142     return tem;
143
144   /* Handle addition and subtraction of CONST_INT specially.  Otherwise,
145      just form the operation.  */
146
147   if (GET_CODE (op1) == CONST_INT
148       && GET_MODE (op0) != VOIDmode
149       && (code == PLUS || code == MINUS))
150     {
151       if (code == MINUS)
152         op1 = neg_const_int (mode, op1);
153       return plus_constant (op0, INTVAL (op1));
154     }
155   else
156     return gen_rtx_fmt_ee (code, mode, op0, op1);
157 }
158 \f
159 /* If X is a MEM referencing the constant pool, return the real value.
160    Otherwise return X.  */
161 rtx
162 avoid_constant_pool_reference (x)
163      rtx x;
164 {
165   rtx c, addr;
166   enum machine_mode cmode;
167
168   if (GET_CODE (x) != MEM)
169     return x;
170   addr = XEXP (x, 0);
171
172   if (GET_CODE (addr) != SYMBOL_REF
173       || ! CONSTANT_POOL_ADDRESS_P (addr))
174     return x;
175
176   c = get_pool_constant (addr);
177   cmode = get_pool_mode (addr);
178
179   /* If we're accessing the constant in a different mode than it was
180      originally stored, attempt to fix that up via subreg simplifications.
181      If that fails we have no choice but to return the original memory.  */
182   if (cmode != GET_MODE (x))
183     {
184       c = simplify_subreg (GET_MODE (x), c, cmode, 0);
185       return c ? c : x;
186     }
187
188   return c;
189 }
190 \f
191 /* Make a unary operation by first seeing if it folds and otherwise making
192    the specified operation.  */
193
194 rtx
195 simplify_gen_unary (code, mode, op, op_mode)
196      enum rtx_code code;
197      enum machine_mode mode;
198      rtx op;
199      enum machine_mode op_mode;
200 {
201   rtx tem;
202
203   /* If this simplifies, use it.  */
204   if ((tem = simplify_unary_operation (code, mode, op, op_mode)) != 0)
205     return tem;
206
207   return gen_rtx_fmt_e (code, mode, op);
208 }
209
210 /* Likewise for ternary operations.  */
211
212 rtx
213 simplify_gen_ternary (code, mode, op0_mode, op0, op1, op2)
214      enum rtx_code code;
215      enum machine_mode mode, op0_mode;
216      rtx op0, op1, op2;
217 {
218   rtx tem;
219
220   /* If this simplifies, use it.  */
221   if (0 != (tem = simplify_ternary_operation (code, mode, op0_mode,
222                                               op0, op1, op2)))
223     return tem;
224
225   return gen_rtx_fmt_eee (code, mode, op0, op1, op2);
226 }
227 \f
228 /* Likewise, for relational operations.
229    CMP_MODE specifies mode comparison is done in.
230   */
231
232 rtx
233 simplify_gen_relational (code, mode, cmp_mode, op0, op1)
234      enum rtx_code code;
235      enum machine_mode mode;
236      enum machine_mode cmp_mode;
237      rtx op0, op1;
238 {
239   rtx tem;
240
241   if ((tem = simplify_relational_operation (code, cmp_mode, op0, op1)) != 0)
242     return tem;
243
244   /* Put complex operands first and constants second.  */
245   if (swap_commutative_operands_p (op0, op1))
246     tem = op0, op0 = op1, op1 = tem, code = swap_condition (code);
247
248   return gen_rtx_fmt_ee (code, mode, op0, op1);
249 }
250 \f
251 /* Replace all occurrences of OLD in X with NEW and try to simplify the
252    resulting RTX.  Return a new RTX which is as simplified as possible.  */
253
254 rtx
255 simplify_replace_rtx (x, old, new)
256      rtx x;
257      rtx old;
258      rtx new;
259 {
260   enum rtx_code code = GET_CODE (x);
261   enum machine_mode mode = GET_MODE (x);
262
263   /* If X is OLD, return NEW.  Otherwise, if this is an expression, try
264      to build a new expression substituting recursively.  If we can't do
265      anything, return our input.  */
266
267   if (x == old)
268     return new;
269
270   switch (GET_RTX_CLASS (code))
271     {
272     case '1':
273       {
274         enum machine_mode op_mode = GET_MODE (XEXP (x, 0));
275         rtx op = (XEXP (x, 0) == old
276                   ? new : simplify_replace_rtx (XEXP (x, 0), old, new));
277
278         return simplify_gen_unary (code, mode, op, op_mode);
279       }
280
281     case '2':
282     case 'c':
283       return
284         simplify_gen_binary (code, mode,
285                              simplify_replace_rtx (XEXP (x, 0), old, new),
286                              simplify_replace_rtx (XEXP (x, 1), old, new));
287     case '<':
288       {
289         enum machine_mode op_mode = (GET_MODE (XEXP (x, 0)) != VOIDmode
290                                      ? GET_MODE (XEXP (x, 0))
291                                      : GET_MODE (XEXP (x, 1)));
292         rtx op0 = simplify_replace_rtx (XEXP (x, 0), old, new);
293         rtx op1 = simplify_replace_rtx (XEXP (x, 1), old, new);
294
295         return
296           simplify_gen_relational (code, mode,
297                                    (op_mode != VOIDmode
298                                     ? op_mode
299                                     : GET_MODE (op0) != VOIDmode
300                                     ? GET_MODE (op0)
301                                     : GET_MODE (op1)),
302                                    op0, op1);
303       }
304
305     case '3':
306     case 'b':
307       {
308         enum machine_mode op_mode = GET_MODE (XEXP (x, 0));
309         rtx op0 = simplify_replace_rtx (XEXP (x, 0), old, new);
310
311         return
312           simplify_gen_ternary (code, mode, 
313                                 (op_mode != VOIDmode
314                                  ? op_mode
315                                  : GET_MODE (op0)),
316                                 op0,
317                                 simplify_replace_rtx (XEXP (x, 1), old, new),
318                                 simplify_replace_rtx (XEXP (x, 2), old, new));
319       }
320
321     case 'x':
322       /* The only case we try to handle is a SUBREG.  */
323       if (code == SUBREG)
324         {
325           rtx exp;
326           exp = simplify_gen_subreg (GET_MODE (x),
327                                      simplify_replace_rtx (SUBREG_REG (x),
328                                                            old, new),
329                                      GET_MODE (SUBREG_REG (x)),
330                                      SUBREG_BYTE (x));
331           if (exp)
332            x = exp;
333         }
334       return x;
335
336     default:
337       if (GET_CODE (x) == MEM)
338         return
339           replace_equiv_address_nv (x,
340                                     simplify_replace_rtx (XEXP (x, 0),
341                                                           old, new));
342
343       return x;
344     }
345   return x;
346 }
347 \f
348 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
349 /* Subroutine of simplify_unary_operation, called via do_float_handler.
350    Handles simplification of unary ops on floating point values.  */
351 struct simplify_unary_real_args
352 {
353   rtx operand;
354   rtx result;
355   enum machine_mode mode;
356   enum rtx_code code;
357   bool want_integer;
358 };
359 #define REAL_VALUE_ABS(d_) \
360    (REAL_VALUE_NEGATIVE (d_) ? REAL_VALUE_NEGATE (d_) : (d_))
361
362 static void
363 simplify_unary_real (p)
364      PTR p;
365 {
366   REAL_VALUE_TYPE d;
367
368   struct simplify_unary_real_args *args =
369     (struct simplify_unary_real_args *) p;
370
371   REAL_VALUE_FROM_CONST_DOUBLE (d, args->operand);
372
373   if (args->want_integer)
374     {
375       HOST_WIDE_INT i;
376
377       switch (args->code)
378         {
379         case FIX:               i = REAL_VALUE_FIX (d);           break;
380         case UNSIGNED_FIX:      i = REAL_VALUE_UNSIGNED_FIX (d);  break;
381         default:
382           abort ();
383         }
384       args->result = GEN_INT (trunc_int_for_mode (i, args->mode));
385     }
386   else
387     {
388       switch (args->code)
389         {
390         case SQRT:
391           /* We don't attempt to optimize this.  */
392           args->result = 0;
393           return;
394
395         case ABS:             d = REAL_VALUE_ABS (d);                   break;
396         case NEG:             d = REAL_VALUE_NEGATE (d);                break;
397         case FLOAT_TRUNCATE:  d = real_value_truncate (args->mode, d);  break;
398         case FLOAT_EXTEND:    /* All this does is change the mode.  */  break;
399         case FIX:             d = REAL_VALUE_RNDZINT (d);               break;
400         case UNSIGNED_FIX:    d = REAL_VALUE_UNSIGNED_RNDZINT (d);      break;
401         default:
402           abort ();
403         }
404       args->result = CONST_DOUBLE_FROM_REAL_VALUE (d, args->mode);
405     }
406 }
407 #endif
408
409 /* Try to simplify a unary operation CODE whose output mode is to be
410    MODE with input operand OP whose mode was originally OP_MODE.
411    Return zero if no simplification can be made.  */
412 rtx
413 simplify_unary_operation (code, mode, op, op_mode)
414      enum rtx_code code;
415      enum machine_mode mode;
416      rtx op;
417      enum machine_mode op_mode;
418 {
419   unsigned int width = GET_MODE_BITSIZE (mode);
420   rtx trueop = avoid_constant_pool_reference (op);
421
422   /* The order of these tests is critical so that, for example, we don't
423      check the wrong mode (input vs. output) for a conversion operation,
424      such as FIX.  At some point, this should be simplified.  */
425
426 #if !defined(REAL_IS_NOT_DOUBLE) || defined(REAL_ARITHMETIC)
427
428   if (code == FLOAT && GET_MODE (trueop) == VOIDmode
429       && (GET_CODE (trueop) == CONST_DOUBLE || GET_CODE (trueop) == CONST_INT))
430     {
431       HOST_WIDE_INT hv, lv;
432       REAL_VALUE_TYPE d;
433
434       if (GET_CODE (trueop) == CONST_INT)
435         lv = INTVAL (trueop), hv = HWI_SIGN_EXTEND (lv);
436       else
437         lv = CONST_DOUBLE_LOW (trueop),  hv = CONST_DOUBLE_HIGH (trueop);
438
439 #ifdef REAL_ARITHMETIC
440       REAL_VALUE_FROM_INT (d, lv, hv, mode);
441 #else
442       if (hv < 0)
443         {
444           d = (double) (~ hv);
445           d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
446                 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
447           d += (double) (unsigned HOST_WIDE_INT) (~ lv);
448           d = (- d - 1.0);
449         }
450       else
451         {
452           d = (double) hv;
453           d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
454                 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
455           d += (double) (unsigned HOST_WIDE_INT) lv;
456         }
457 #endif  /* REAL_ARITHMETIC */
458       d = real_value_truncate (mode, d);
459       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
460     }
461   else if (code == UNSIGNED_FLOAT && GET_MODE (trueop) == VOIDmode
462            && (GET_CODE (trueop) == CONST_DOUBLE
463                || GET_CODE (trueop) == CONST_INT))
464     {
465       HOST_WIDE_INT hv, lv;
466       REAL_VALUE_TYPE d;
467
468       if (GET_CODE (trueop) == CONST_INT)
469         lv = INTVAL (trueop), hv = HWI_SIGN_EXTEND (lv);
470       else
471         lv = CONST_DOUBLE_LOW (trueop),  hv = CONST_DOUBLE_HIGH (trueop);
472
473       if (op_mode == VOIDmode)
474         {
475           /* We don't know how to interpret negative-looking numbers in
476              this case, so don't try to fold those.  */
477           if (hv < 0)
478             return 0;
479         }
480       else if (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT * 2)
481         ;
482       else
483         hv = 0, lv &= GET_MODE_MASK (op_mode);
484
485 #ifdef REAL_ARITHMETIC
486       REAL_VALUE_FROM_UNSIGNED_INT (d, lv, hv, mode);
487 #else
488
489       d = (double) (unsigned HOST_WIDE_INT) hv;
490       d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
491             * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
492       d += (double) (unsigned HOST_WIDE_INT) lv;
493 #endif  /* REAL_ARITHMETIC */
494       d = real_value_truncate (mode, d);
495       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
496     }
497 #endif
498
499   if (GET_CODE (trueop) == CONST_INT
500       && width <= HOST_BITS_PER_WIDE_INT && width > 0)
501     {
502       HOST_WIDE_INT arg0 = INTVAL (trueop);
503       HOST_WIDE_INT val;
504
505       switch (code)
506         {
507         case NOT:
508           val = ~ arg0;
509           break;
510
511         case NEG:
512           val = - arg0;
513           break;
514
515         case ABS:
516           val = (arg0 >= 0 ? arg0 : - arg0);
517           break;
518
519         case FFS:
520           /* Don't use ffs here.  Instead, get low order bit and then its
521              number.  If arg0 is zero, this will return 0, as desired.  */
522           arg0 &= GET_MODE_MASK (mode);
523           val = exact_log2 (arg0 & (- arg0)) + 1;
524           break;
525
526         case TRUNCATE:
527           val = arg0;
528           break;
529
530         case ZERO_EXTEND:
531           if (op_mode == VOIDmode)
532             op_mode = mode;
533           if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
534             {
535               /* If we were really extending the mode,
536                  we would have to distinguish between zero-extension
537                  and sign-extension.  */
538               if (width != GET_MODE_BITSIZE (op_mode))
539                 abort ();
540               val = arg0;
541             }
542           else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
543             val = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
544           else
545             return 0;
546           break;
547
548         case SIGN_EXTEND:
549           if (op_mode == VOIDmode)
550             op_mode = mode;
551           if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
552             {
553               /* If we were really extending the mode,
554                  we would have to distinguish between zero-extension
555                  and sign-extension.  */
556               if (width != GET_MODE_BITSIZE (op_mode))
557                 abort ();
558               val = arg0;
559             }
560           else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
561             {
562               val
563                 = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
564               if (val
565                   & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (op_mode) - 1)))
566                 val -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
567             }
568           else
569             return 0;
570           break;
571
572         case SQRT:
573         case FLOAT_EXTEND:
574         case FLOAT_TRUNCATE:
575           return 0;
576
577         default:
578           abort ();
579         }
580
581       val = trunc_int_for_mode (val, mode);
582
583       return GEN_INT (val);
584     }
585
586   /* We can do some operations on integer CONST_DOUBLEs.  Also allow
587      for a DImode operation on a CONST_INT.  */
588   else if (GET_MODE (trueop) == VOIDmode && width <= HOST_BITS_PER_INT * 2
589            && (GET_CODE (trueop) == CONST_DOUBLE
590                || GET_CODE (trueop) == CONST_INT))
591     {
592       unsigned HOST_WIDE_INT l1, lv;
593       HOST_WIDE_INT h1, hv;
594
595       if (GET_CODE (trueop) == CONST_DOUBLE)
596         l1 = CONST_DOUBLE_LOW (trueop), h1 = CONST_DOUBLE_HIGH (trueop);
597       else
598         l1 = INTVAL (trueop), h1 = HWI_SIGN_EXTEND (l1);
599
600       switch (code)
601         {
602         case NOT:
603           lv = ~ l1;
604           hv = ~ h1;
605           break;
606
607         case NEG:
608           neg_double (l1, h1, &lv, &hv);
609           break;
610
611         case ABS:
612           if (h1 < 0)
613             neg_double (l1, h1, &lv, &hv);
614           else
615             lv = l1, hv = h1;
616           break;
617
618         case FFS:
619           hv = 0;
620           if (l1 == 0)
621             lv = HOST_BITS_PER_WIDE_INT + exact_log2 (h1 & (-h1)) + 1;
622           else
623             lv = exact_log2 (l1 & (-l1)) + 1;
624           break;
625
626         case TRUNCATE:
627           /* This is just a change-of-mode, so do nothing.  */
628           lv = l1, hv = h1;
629           break;
630
631         case ZERO_EXTEND:
632           if (op_mode == VOIDmode
633               || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
634             return 0;
635
636           hv = 0;
637           lv = l1 & GET_MODE_MASK (op_mode);
638           break;
639
640         case SIGN_EXTEND:
641           if (op_mode == VOIDmode
642               || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
643             return 0;
644           else
645             {
646               lv = l1 & GET_MODE_MASK (op_mode);
647               if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
648                   && (lv & ((HOST_WIDE_INT) 1
649                             << (GET_MODE_BITSIZE (op_mode) - 1))) != 0)
650                 lv -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
651
652               hv = HWI_SIGN_EXTEND (lv);
653             }
654           break;
655
656         case SQRT:
657           return 0;
658
659         default:
660           return 0;
661         }
662
663       return immed_double_const (lv, hv, mode);
664     }
665
666 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
667   else if (GET_CODE (trueop) == CONST_DOUBLE
668            && GET_MODE_CLASS (mode) == MODE_FLOAT)
669     {
670       struct simplify_unary_real_args args;
671       args.operand = trueop;
672       args.mode = mode;
673       args.code = code;
674       args.want_integer = false;
675
676       if (do_float_handler (simplify_unary_real, (PTR) &args))
677         return args.result;
678
679       return 0;
680     }
681
682   else if (GET_CODE (trueop) == CONST_DOUBLE
683            && GET_MODE_CLASS (GET_MODE (trueop)) == MODE_FLOAT
684            && GET_MODE_CLASS (mode) == MODE_INT
685            && width <= HOST_BITS_PER_WIDE_INT && width > 0)
686     {
687       struct simplify_unary_real_args args;
688       args.operand = trueop;
689       args.mode = mode;
690       args.code = code;
691       args.want_integer = true;
692
693       if (do_float_handler (simplify_unary_real, (PTR) &args))
694         return args.result;
695
696       return 0;
697     }
698 #endif
699   /* This was formerly used only for non-IEEE float.
700      eggert@twinsun.com says it is safe for IEEE also.  */
701   else
702     {
703       enum rtx_code reversed;
704       /* There are some simplifications we can do even if the operands
705          aren't constant.  */
706       switch (code)
707         {
708         case NOT:
709           /* (not (not X)) == X.  */
710           if (GET_CODE (op) == NOT)
711             return XEXP (op, 0);
712
713           /* (not (eq X Y)) == (ne X Y), etc.  */
714           if (mode == BImode && GET_RTX_CLASS (GET_CODE (op)) == '<'
715               && ((reversed = reversed_comparison_code (op, NULL_RTX))
716                   != UNKNOWN))
717             return gen_rtx_fmt_ee (reversed,
718                                    op_mode, XEXP (op, 0), XEXP (op, 1));
719           break;
720
721         case NEG:
722           /* (neg (neg X)) == X.  */
723           if (GET_CODE (op) == NEG)
724             return XEXP (op, 0);
725           break;
726
727         case SIGN_EXTEND:
728           /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
729              becomes just the MINUS if its mode is MODE.  This allows
730              folding switch statements on machines using casesi (such as
731              the VAX).  */
732           if (GET_CODE (op) == TRUNCATE
733               && GET_MODE (XEXP (op, 0)) == mode
734               && GET_CODE (XEXP (op, 0)) == MINUS
735               && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
736               && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
737             return XEXP (op, 0);
738
739 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
740           if (! POINTERS_EXTEND_UNSIGNED
741               && mode == Pmode && GET_MODE (op) == ptr_mode
742               && (CONSTANT_P (op)
743                   || (GET_CODE (op) == SUBREG
744                       && GET_CODE (SUBREG_REG (op)) == REG
745                       && REG_POINTER (SUBREG_REG (op))
746                       && GET_MODE (SUBREG_REG (op)) == Pmode)))
747             return convert_memory_address (Pmode, op);
748 #endif
749           break;
750
751 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
752         case ZERO_EXTEND:
753           if (POINTERS_EXTEND_UNSIGNED > 0
754               && mode == Pmode && GET_MODE (op) == ptr_mode
755               && (CONSTANT_P (op)
756                   || (GET_CODE (op) == SUBREG
757                       && GET_CODE (SUBREG_REG (op)) == REG
758                       && REG_POINTER (SUBREG_REG (op))
759                       && GET_MODE (SUBREG_REG (op)) == Pmode)))
760             return convert_memory_address (Pmode, op);
761           break;
762 #endif
763           
764         default:
765           break;
766         }
767
768       return 0;
769     }
770 }
771 \f
772 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
773 /* Subroutine of simplify_binary_operation, called via do_float_handler.
774    Handles simplification of binary ops on floating point values.  */
775 struct simplify_binary_real_args
776 {
777   rtx trueop0, trueop1;
778   rtx result;
779   enum rtx_code code;
780   enum machine_mode mode;
781 };
782
783 static void
784 simplify_binary_real (p)
785      PTR p;
786 {
787   REAL_VALUE_TYPE f0, f1, value;
788   struct simplify_binary_real_args *args =
789     (struct simplify_binary_real_args *) p;
790
791   REAL_VALUE_FROM_CONST_DOUBLE (f0, args->trueop0);
792   REAL_VALUE_FROM_CONST_DOUBLE (f1, args->trueop1);
793   f0 = real_value_truncate (args->mode, f0);
794   f1 = real_value_truncate (args->mode, f1);
795
796 #ifdef REAL_ARITHMETIC
797 #ifndef REAL_INFINITY
798   if (args->code == DIV && REAL_VALUES_EQUAL (f1, dconst0))
799     {
800       args->result = 0;
801       return;
802     }
803 #endif
804   REAL_ARITHMETIC (value, rtx_to_tree_code (args->code), f0, f1);
805 #else
806   switch (args->code)
807     {
808     case PLUS:
809       value = f0 + f1;
810       break;
811     case MINUS:
812       value = f0 - f1;
813       break;
814     case MULT:
815       value = f0 * f1;
816       break;
817     case DIV:
818 #ifndef REAL_INFINITY
819       if (f1 == 0)
820         return 0;
821 #endif
822       value = f0 / f1;
823       break;
824     case SMIN:
825       value = MIN (f0, f1);
826       break;
827     case SMAX:
828       value = MAX (f0, f1);
829       break;
830     default:
831       abort ();
832     }
833 #endif
834
835   value = real_value_truncate (args->mode, value);
836   args->result = CONST_DOUBLE_FROM_REAL_VALUE (value, args->mode);
837 }
838 #endif
839
840 /* Another subroutine called via do_float_handler.  This one tests
841    the floating point value given against 2. and -1.  */
842 struct simplify_binary_is2orm1_args
843 {
844   rtx value;
845   bool is_2;
846   bool is_m1;
847 };
848
849 static void
850 simplify_binary_is2orm1 (p)
851      PTR p;
852 {
853   REAL_VALUE_TYPE d;
854   struct simplify_binary_is2orm1_args *args =
855     (struct simplify_binary_is2orm1_args *) p;
856
857   REAL_VALUE_FROM_CONST_DOUBLE (d, args->value);
858   args->is_2 = REAL_VALUES_EQUAL (d, dconst2);
859   args->is_m1 = REAL_VALUES_EQUAL (d, dconstm1);
860 }
861
862 /* Simplify a binary operation CODE with result mode MODE, operating on OP0
863    and OP1.  Return 0 if no simplification is possible.
864
865    Don't use this for relational operations such as EQ or LT.
866    Use simplify_relational_operation instead.  */
867 rtx
868 simplify_binary_operation (code, mode, op0, op1)
869      enum rtx_code code;
870      enum machine_mode mode;
871      rtx op0, op1;
872 {
873   HOST_WIDE_INT arg0, arg1, arg0s, arg1s;
874   HOST_WIDE_INT val;
875   unsigned int width = GET_MODE_BITSIZE (mode);
876   rtx tem;
877   rtx trueop0 = avoid_constant_pool_reference (op0);
878   rtx trueop1 = avoid_constant_pool_reference (op1);
879
880   /* Relational operations don't work here.  We must know the mode
881      of the operands in order to do the comparison correctly.
882      Assuming a full word can give incorrect results.
883      Consider comparing 128 with -128 in QImode.  */
884
885   if (GET_RTX_CLASS (code) == '<')
886     abort ();
887
888   /* Make sure the constant is second.  */
889   if (GET_RTX_CLASS (code) == 'c'
890       && swap_commutative_operands_p (trueop0, trueop1))
891     {
892       tem = op0, op0 = op1, op1 = tem;
893       tem = trueop0, trueop0 = trueop1, trueop1 = tem;
894     }
895
896 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
897   if (GET_MODE_CLASS (mode) == MODE_FLOAT
898       && GET_CODE (trueop0) == CONST_DOUBLE
899       && GET_CODE (trueop1) == CONST_DOUBLE
900       && mode == GET_MODE (op0) && mode == GET_MODE (op1))
901     {
902       struct simplify_binary_real_args args;
903       args.trueop0 = trueop0;
904       args.trueop1 = trueop1;
905       args.mode = mode;
906       args.code = code;
907
908       if (do_float_handler (simplify_binary_real, (PTR) &args))
909         return args.result;
910       return 0;
911     }
912 #endif  /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
913
914   /* We can fold some multi-word operations.  */
915   if (GET_MODE_CLASS (mode) == MODE_INT
916       && width == HOST_BITS_PER_WIDE_INT * 2
917       && (GET_CODE (trueop0) == CONST_DOUBLE
918           || GET_CODE (trueop0) == CONST_INT)
919       && (GET_CODE (trueop1) == CONST_DOUBLE
920           || GET_CODE (trueop1) == CONST_INT))
921     {
922       unsigned HOST_WIDE_INT l1, l2, lv;
923       HOST_WIDE_INT h1, h2, hv;
924
925       if (GET_CODE (trueop0) == CONST_DOUBLE)
926         l1 = CONST_DOUBLE_LOW (trueop0), h1 = CONST_DOUBLE_HIGH (trueop0);
927       else
928         l1 = INTVAL (trueop0), h1 = HWI_SIGN_EXTEND (l1);
929
930       if (GET_CODE (trueop1) == CONST_DOUBLE)
931         l2 = CONST_DOUBLE_LOW (trueop1), h2 = CONST_DOUBLE_HIGH (trueop1);
932       else
933         l2 = INTVAL (trueop1), h2 = HWI_SIGN_EXTEND (l2);
934
935       switch (code)
936         {
937         case MINUS:
938           /* A - B == A + (-B).  */
939           neg_double (l2, h2, &lv, &hv);
940           l2 = lv, h2 = hv;
941
942           /* .. fall through ...  */
943
944         case PLUS:
945           add_double (l1, h1, l2, h2, &lv, &hv);
946           break;
947
948         case MULT:
949           mul_double (l1, h1, l2, h2, &lv, &hv);
950           break;
951
952         case DIV:  case MOD:   case UDIV:  case UMOD:
953           /* We'd need to include tree.h to do this and it doesn't seem worth
954              it.  */
955           return 0;
956
957         case AND:
958           lv = l1 & l2, hv = h1 & h2;
959           break;
960
961         case IOR:
962           lv = l1 | l2, hv = h1 | h2;
963           break;
964
965         case XOR:
966           lv = l1 ^ l2, hv = h1 ^ h2;
967           break;
968
969         case SMIN:
970           if (h1 < h2
971               || (h1 == h2
972                   && ((unsigned HOST_WIDE_INT) l1
973                       < (unsigned HOST_WIDE_INT) l2)))
974             lv = l1, hv = h1;
975           else
976             lv = l2, hv = h2;
977           break;
978
979         case SMAX:
980           if (h1 > h2
981               || (h1 == h2
982                   && ((unsigned HOST_WIDE_INT) l1
983                       > (unsigned HOST_WIDE_INT) l2)))
984             lv = l1, hv = h1;
985           else
986             lv = l2, hv = h2;
987           break;
988
989         case UMIN:
990           if ((unsigned HOST_WIDE_INT) h1 < (unsigned HOST_WIDE_INT) h2
991               || (h1 == h2
992                   && ((unsigned HOST_WIDE_INT) l1
993                       < (unsigned HOST_WIDE_INT) l2)))
994             lv = l1, hv = h1;
995           else
996             lv = l2, hv = h2;
997           break;
998
999         case UMAX:
1000           if ((unsigned HOST_WIDE_INT) h1 > (unsigned HOST_WIDE_INT) h2
1001               || (h1 == h2
1002                   && ((unsigned HOST_WIDE_INT) l1
1003                       > (unsigned HOST_WIDE_INT) l2)))
1004             lv = l1, hv = h1;
1005           else
1006             lv = l2, hv = h2;
1007           break;
1008
1009         case LSHIFTRT:   case ASHIFTRT:
1010         case ASHIFT:
1011         case ROTATE:     case ROTATERT:
1012 #ifdef SHIFT_COUNT_TRUNCATED
1013           if (SHIFT_COUNT_TRUNCATED)
1014             l2 &= (GET_MODE_BITSIZE (mode) - 1), h2 = 0;
1015 #endif
1016
1017           if (h2 != 0 || l2 >= GET_MODE_BITSIZE (mode))
1018             return 0;
1019
1020           if (code == LSHIFTRT || code == ASHIFTRT)
1021             rshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv,
1022                            code == ASHIFTRT);
1023           else if (code == ASHIFT)
1024             lshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv, 1);
1025           else if (code == ROTATE)
1026             lrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
1027           else /* code == ROTATERT */
1028             rrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
1029           break;
1030
1031         default:
1032           return 0;
1033         }
1034
1035       return immed_double_const (lv, hv, mode);
1036     }
1037
1038   if (GET_CODE (op0) != CONST_INT || GET_CODE (op1) != CONST_INT
1039       || width > HOST_BITS_PER_WIDE_INT || width == 0)
1040     {
1041       /* Even if we can't compute a constant result,
1042          there are some cases worth simplifying.  */
1043
1044       switch (code)
1045         {
1046         case PLUS:
1047           /* In IEEE floating point, x+0 is not the same as x.  Similarly
1048              for the other optimizations below.  */
1049           if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1050               && FLOAT_MODE_P (mode) && ! flag_unsafe_math_optimizations)
1051             break;
1052
1053           if (trueop1 == CONST0_RTX (mode))
1054             return op0;
1055
1056           /* ((-a) + b) -> (b - a) and similarly for (a + (-b)) */
1057           if (GET_CODE (op0) == NEG)
1058             return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
1059           else if (GET_CODE (op1) == NEG)
1060             return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
1061
1062           /* (~a) + 1 -> -a */
1063           if (INTEGRAL_MODE_P (mode)
1064               && GET_CODE (op0) == NOT
1065               && trueop1 == const1_rtx)
1066             return gen_rtx_NEG (mode, XEXP (op0, 0));
1067
1068           /* Handle both-operands-constant cases.  We can only add
1069              CONST_INTs to constants since the sum of relocatable symbols
1070              can't be handled by most assemblers.  Don't add CONST_INT
1071              to CONST_INT since overflow won't be computed properly if wider
1072              than HOST_BITS_PER_WIDE_INT.  */
1073
1074           if (CONSTANT_P (op0) && GET_MODE (op0) != VOIDmode
1075               && GET_CODE (op1) == CONST_INT)
1076             return plus_constant (op0, INTVAL (op1));
1077           else if (CONSTANT_P (op1) && GET_MODE (op1) != VOIDmode
1078                    && GET_CODE (op0) == CONST_INT)
1079             return plus_constant (op1, INTVAL (op0));
1080
1081           /* See if this is something like X * C - X or vice versa or
1082              if the multiplication is written as a shift.  If so, we can
1083              distribute and make a new multiply, shift, or maybe just
1084              have X (if C is 2 in the example above).  But don't make
1085              real multiply if we didn't have one before.  */
1086
1087           if (! FLOAT_MODE_P (mode))
1088             {
1089               HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
1090               rtx lhs = op0, rhs = op1;
1091               int had_mult = 0;
1092
1093               if (GET_CODE (lhs) == NEG)
1094                 coeff0 = -1, lhs = XEXP (lhs, 0);
1095               else if (GET_CODE (lhs) == MULT
1096                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
1097                 {
1098                   coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
1099                   had_mult = 1;
1100                 }
1101               else if (GET_CODE (lhs) == ASHIFT
1102                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT
1103                        && INTVAL (XEXP (lhs, 1)) >= 0
1104                        && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
1105                 {
1106                   coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
1107                   lhs = XEXP (lhs, 0);
1108                 }
1109
1110               if (GET_CODE (rhs) == NEG)
1111                 coeff1 = -1, rhs = XEXP (rhs, 0);
1112               else if (GET_CODE (rhs) == MULT
1113                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
1114                 {
1115                   coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
1116                   had_mult = 1;
1117                 }
1118               else if (GET_CODE (rhs) == ASHIFT
1119                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT
1120                        && INTVAL (XEXP (rhs, 1)) >= 0
1121                        && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
1122                 {
1123                   coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
1124                   rhs = XEXP (rhs, 0);
1125                 }
1126
1127               if (rtx_equal_p (lhs, rhs))
1128                 {
1129                   tem = simplify_gen_binary (MULT, mode, lhs,
1130                                         GEN_INT (coeff0 + coeff1));
1131                   return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
1132                 }
1133             }
1134
1135           /* If one of the operands is a PLUS or a MINUS, see if we can
1136              simplify this by the associative law. 
1137              Don't use the associative law for floating point.
1138              The inaccuracy makes it nonassociative,
1139              and subtle programs can break if operations are associated.  */
1140
1141           if (INTEGRAL_MODE_P (mode)
1142               && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
1143                   || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS
1144                   || (GET_CODE (op0) == CONST
1145                       && GET_CODE (XEXP (op0, 0)) == PLUS)
1146                   || (GET_CODE (op1) == CONST
1147                       && GET_CODE (XEXP (op1, 0)) == PLUS))
1148               && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
1149             return tem;
1150           break;
1151
1152         case COMPARE:
1153 #ifdef HAVE_cc0
1154           /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
1155              using cc0, in which case we want to leave it as a COMPARE
1156              so we can distinguish it from a register-register-copy.
1157
1158              In IEEE floating point, x-0 is not the same as x.  */
1159
1160           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1161                || ! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
1162               && trueop1 == CONST0_RTX (mode))
1163             return op0;
1164 #endif
1165
1166           /* Convert (compare (gt (flags) 0) (lt (flags) 0)) to (flags).  */
1167           if (((GET_CODE (op0) == GT && GET_CODE (op1) == LT)
1168                || (GET_CODE (op0) == GTU && GET_CODE (op1) == LTU))
1169               && XEXP (op0, 1) == const0_rtx && XEXP (op1, 1) == const0_rtx)
1170             {
1171               rtx xop00 = XEXP (op0, 0);
1172               rtx xop10 = XEXP (op1, 0);
1173
1174 #ifdef HAVE_cc0
1175               if (GET_CODE (xop00) == CC0 && GET_CODE (xop10) == CC0)
1176 #else
1177               if (GET_CODE (xop00) == REG && GET_CODE (xop10) == REG
1178                   && GET_MODE (xop00) == GET_MODE (xop10)
1179                   && REGNO (xop00) == REGNO (xop10)
1180                   && GET_MODE_CLASS (GET_MODE (xop00)) == MODE_CC
1181                   && GET_MODE_CLASS (GET_MODE (xop10)) == MODE_CC)
1182 #endif
1183                 return xop00;
1184             }
1185           break;              
1186
1187         case MINUS:
1188           /* None of these optimizations can be done for IEEE
1189              floating point.  */
1190           if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1191               && FLOAT_MODE_P (mode) && ! flag_unsafe_math_optimizations)
1192             break;
1193
1194           /* We can't assume x-x is 0 even with non-IEEE floating point,
1195              but since it is zero except in very strange circumstances, we
1196              will treat it as zero with -funsafe-math-optimizations.  */
1197           if (rtx_equal_p (trueop0, trueop1)
1198               && ! side_effects_p (op0)
1199               && (! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations))
1200             return CONST0_RTX (mode);
1201
1202           /* Change subtraction from zero into negation.  */
1203           if (trueop0 == CONST0_RTX (mode))
1204             return gen_rtx_NEG (mode, op1);
1205
1206           /* (-1 - a) is ~a.  */
1207           if (trueop0 == constm1_rtx)
1208             return gen_rtx_NOT (mode, op1);
1209
1210           /* Subtracting 0 has no effect.  */
1211           if (trueop1 == CONST0_RTX (mode))
1212             return op0;
1213
1214           /* See if this is something like X * C - X or vice versa or
1215              if the multiplication is written as a shift.  If so, we can
1216              distribute and make a new multiply, shift, or maybe just
1217              have X (if C is 2 in the example above).  But don't make
1218              real multiply if we didn't have one before.  */
1219
1220           if (! FLOAT_MODE_P (mode))
1221             {
1222               HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
1223               rtx lhs = op0, rhs = op1;
1224               int had_mult = 0;
1225
1226               if (GET_CODE (lhs) == NEG)
1227                 coeff0 = -1, lhs = XEXP (lhs, 0);
1228               else if (GET_CODE (lhs) == MULT
1229                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
1230                 {
1231                   coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
1232                   had_mult = 1;
1233                 }
1234               else if (GET_CODE (lhs) == ASHIFT
1235                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT
1236                        && INTVAL (XEXP (lhs, 1)) >= 0
1237                        && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
1238                 {
1239                   coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
1240                   lhs = XEXP (lhs, 0);
1241                 }
1242
1243               if (GET_CODE (rhs) == NEG)
1244                 coeff1 = - 1, rhs = XEXP (rhs, 0);
1245               else if (GET_CODE (rhs) == MULT
1246                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
1247                 {
1248                   coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
1249                   had_mult = 1;
1250                 }
1251               else if (GET_CODE (rhs) == ASHIFT
1252                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT
1253                        && INTVAL (XEXP (rhs, 1)) >= 0
1254                        && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
1255                 {
1256                   coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
1257                   rhs = XEXP (rhs, 0);
1258                 }
1259
1260               if (rtx_equal_p (lhs, rhs))
1261                 {
1262                   tem = simplify_gen_binary (MULT, mode, lhs,
1263                                              GEN_INT (coeff0 - coeff1));
1264                   return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
1265                 }
1266             }
1267
1268           /* (a - (-b)) -> (a + b).  */
1269           if (GET_CODE (op1) == NEG)
1270             return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
1271
1272           /* If one of the operands is a PLUS or a MINUS, see if we can
1273              simplify this by the associative law. 
1274              Don't use the associative law for floating point.
1275              The inaccuracy makes it nonassociative,
1276              and subtle programs can break if operations are associated.  */
1277
1278           if (INTEGRAL_MODE_P (mode)
1279               && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
1280                   || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS
1281                   || (GET_CODE (op0) == CONST
1282                       && GET_CODE (XEXP (op0, 0)) == PLUS)
1283                   || (GET_CODE (op1) == CONST
1284                       && GET_CODE (XEXP (op1, 0)) == PLUS))
1285               && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
1286             return tem;
1287
1288           /* Don't let a relocatable value get a negative coeff.  */
1289           if (GET_CODE (op1) == CONST_INT && GET_MODE (op0) != VOIDmode)
1290             return simplify_gen_binary (PLUS, mode,
1291                                         op0,
1292                                         neg_const_int (mode, op1));
1293
1294           /* (x - (x & y)) -> (x & ~y) */
1295           if (GET_CODE (op1) == AND)
1296             {
1297              if (rtx_equal_p (op0, XEXP (op1, 0)))
1298                return simplify_gen_binary (AND, mode, op0,
1299                                            gen_rtx_NOT (mode, XEXP (op1, 1)));
1300              if (rtx_equal_p (op0, XEXP (op1, 1)))
1301                return simplify_gen_binary (AND, mode, op0,
1302                                            gen_rtx_NOT (mode, XEXP (op1, 0)));
1303            }
1304           break;
1305
1306         case MULT:
1307           if (trueop1 == constm1_rtx)
1308             {
1309               tem = simplify_unary_operation (NEG, mode, op0, mode);
1310
1311               return tem ? tem : gen_rtx_NEG (mode, op0);
1312             }
1313
1314           /* In IEEE floating point, x*0 is not always 0.  */
1315           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1316                || ! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
1317               && trueop1 == CONST0_RTX (mode)
1318               && ! side_effects_p (op0))
1319             return op1;
1320
1321           /* In IEEE floating point, x*1 is not equivalent to x for nans.
1322              However, ANSI says we can drop signals,
1323              so we can do this anyway.  */
1324           if (trueop1 == CONST1_RTX (mode))
1325             return op0;
1326
1327           /* Convert multiply by constant power of two into shift unless
1328              we are still generating RTL.  This test is a kludge.  */
1329           if (GET_CODE (trueop1) == CONST_INT
1330               && (val = exact_log2 (INTVAL (trueop1))) >= 0
1331               /* If the mode is larger than the host word size, and the
1332                  uppermost bit is set, then this isn't a power of two due
1333                  to implicit sign extension.  */
1334               && (width <= HOST_BITS_PER_WIDE_INT
1335                   || val != HOST_BITS_PER_WIDE_INT - 1)
1336               && ! rtx_equal_function_value_matters)
1337             return gen_rtx_ASHIFT (mode, op0, GEN_INT (val));
1338
1339           if (GET_CODE (trueop1) == CONST_DOUBLE
1340               && GET_MODE_CLASS (GET_MODE (trueop1)) == MODE_FLOAT)
1341             {
1342               struct simplify_binary_is2orm1_args args;
1343
1344               args.value = trueop1;
1345               if (! do_float_handler (simplify_binary_is2orm1, (PTR) &args))
1346                 return 0;
1347
1348               /* x*2 is x+x and x*(-1) is -x */
1349               if (args.is_2 && GET_MODE (op0) == mode)
1350                 return gen_rtx_PLUS (mode, op0, copy_rtx (op0));
1351
1352               else if (args.is_m1 && GET_MODE (op0) == mode)
1353                 return gen_rtx_NEG (mode, op0);
1354             }
1355           break;
1356
1357         case IOR:
1358           if (trueop1 == const0_rtx)
1359             return op0;
1360           if (GET_CODE (trueop1) == CONST_INT
1361               && ((INTVAL (trueop1) & GET_MODE_MASK (mode))
1362                   == GET_MODE_MASK (mode)))
1363             return op1;
1364           if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
1365             return op0;
1366           /* A | (~A) -> -1 */
1367           if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
1368                || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
1369               && ! side_effects_p (op0)
1370               && GET_MODE_CLASS (mode) != MODE_CC)
1371             return constm1_rtx;
1372           break;
1373
1374         case XOR:
1375           if (trueop1 == const0_rtx)
1376             return op0;
1377           if (GET_CODE (trueop1) == CONST_INT
1378               && ((INTVAL (trueop1) & GET_MODE_MASK (mode))
1379                   == GET_MODE_MASK (mode)))
1380             return gen_rtx_NOT (mode, op0);
1381           if (trueop0 == trueop1 && ! side_effects_p (op0)
1382               && GET_MODE_CLASS (mode) != MODE_CC)
1383             return const0_rtx;
1384           break;
1385
1386         case AND:
1387           if (trueop1 == const0_rtx && ! side_effects_p (op0))
1388             return const0_rtx;
1389           if (GET_CODE (trueop1) == CONST_INT
1390               && ((INTVAL (trueop1) & GET_MODE_MASK (mode))
1391                   == GET_MODE_MASK (mode)))
1392             return op0;
1393           if (trueop0 == trueop1 && ! side_effects_p (op0)
1394               && GET_MODE_CLASS (mode) != MODE_CC)
1395             return op0;
1396           /* A & (~A) -> 0 */
1397           if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
1398                || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
1399               && ! side_effects_p (op0)
1400               && GET_MODE_CLASS (mode) != MODE_CC)
1401             return const0_rtx;
1402           break;
1403
1404         case UDIV:
1405           /* Convert divide by power of two into shift (divide by 1 handled
1406              below).  */
1407           if (GET_CODE (trueop1) == CONST_INT
1408               && (arg1 = exact_log2 (INTVAL (trueop1))) > 0)
1409             return gen_rtx_LSHIFTRT (mode, op0, GEN_INT (arg1));
1410
1411           /* ... fall through ...  */
1412
1413         case DIV:
1414           if (trueop1 == CONST1_RTX (mode))
1415             {
1416               /* On some platforms DIV uses narrower mode than its
1417                  operands.  */
1418               rtx x = gen_lowpart_common (mode, op0);
1419               if (x)
1420                 return x;
1421               else if (mode != GET_MODE (op0) && GET_MODE (op0) != VOIDmode)
1422                 return gen_lowpart_SUBREG (mode, op0);
1423               else
1424                 return op0;
1425             }
1426
1427           /* In IEEE floating point, 0/x is not always 0.  */
1428           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1429                || ! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
1430               && trueop0 == CONST0_RTX (mode)
1431               && ! side_effects_p (op1))
1432             return op0;
1433
1434 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1435           /* Change division by a constant into multiplication.  Only do
1436              this with -funsafe-math-optimizations.  */
1437           else if (GET_CODE (trueop1) == CONST_DOUBLE
1438                    && GET_MODE_CLASS (GET_MODE (trueop1)) == MODE_FLOAT
1439                    && trueop1 != CONST0_RTX (mode)
1440                    && flag_unsafe_math_optimizations)
1441             {
1442               REAL_VALUE_TYPE d;
1443               REAL_VALUE_FROM_CONST_DOUBLE (d, trueop1);
1444
1445               if (! REAL_VALUES_EQUAL (d, dconst0))
1446                 {
1447 #if defined (REAL_ARITHMETIC)
1448                   REAL_ARITHMETIC (d, rtx_to_tree_code (DIV), dconst1, d);
1449                   return gen_rtx_MULT (mode, op0, 
1450                                        CONST_DOUBLE_FROM_REAL_VALUE (d, mode));
1451 #else
1452                   return
1453                     gen_rtx_MULT (mode, op0, 
1454                                   CONST_DOUBLE_FROM_REAL_VALUE (1./d, mode));
1455 #endif
1456                 }
1457             }
1458 #endif
1459           break;
1460
1461         case UMOD:
1462           /* Handle modulus by power of two (mod with 1 handled below).  */
1463           if (GET_CODE (trueop1) == CONST_INT
1464               && exact_log2 (INTVAL (trueop1)) > 0)
1465             return gen_rtx_AND (mode, op0, GEN_INT (INTVAL (op1) - 1));
1466
1467           /* ... fall through ...  */
1468
1469         case MOD:
1470           if ((trueop0 == const0_rtx || trueop1 == const1_rtx)
1471               && ! side_effects_p (op0) && ! side_effects_p (op1))
1472             return const0_rtx;
1473           break;
1474
1475         case ROTATERT:
1476         case ROTATE:
1477           /* Rotating ~0 always results in ~0.  */
1478           if (GET_CODE (trueop0) == CONST_INT && width <= HOST_BITS_PER_WIDE_INT
1479               && (unsigned HOST_WIDE_INT) INTVAL (trueop0) == GET_MODE_MASK (mode)
1480               && ! side_effects_p (op1))
1481             return op0;
1482
1483           /* ... fall through ...  */
1484
1485         case ASHIFT:
1486         case ASHIFTRT:
1487         case LSHIFTRT:
1488           if (trueop1 == const0_rtx)
1489             return op0;
1490           if (trueop0 == const0_rtx && ! side_effects_p (op1))
1491             return op0;
1492           break;
1493
1494         case SMIN:
1495           if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (trueop1) == CONST_INT 
1496               && INTVAL (trueop1) == (HOST_WIDE_INT) 1 << (width -1)
1497               && ! side_effects_p (op0))
1498             return op1;
1499           else if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
1500             return op0;
1501           break;
1502            
1503         case SMAX:
1504           if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (trueop1) == CONST_INT
1505               && ((unsigned HOST_WIDE_INT) INTVAL (trueop1)
1506                   == (unsigned HOST_WIDE_INT) GET_MODE_MASK (mode) >> 1)
1507               && ! side_effects_p (op0))
1508             return op1;
1509           else if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
1510             return op0;
1511           break;
1512
1513         case UMIN:
1514           if (trueop1 == const0_rtx && ! side_effects_p (op0))
1515             return op1;
1516           else if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
1517             return op0;
1518           break;
1519             
1520         case UMAX:
1521           if (trueop1 == constm1_rtx && ! side_effects_p (op0))
1522             return op1;
1523           else if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
1524             return op0;
1525           break;
1526
1527         default:
1528           abort ();
1529         }
1530       
1531       return 0;
1532     }
1533
1534   /* Get the integer argument values in two forms:
1535      zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S.  */
1536
1537   arg0 = INTVAL (trueop0);
1538   arg1 = INTVAL (trueop1);
1539
1540   if (width < HOST_BITS_PER_WIDE_INT)
1541     {
1542       arg0 &= ((HOST_WIDE_INT) 1 << width) - 1;
1543       arg1 &= ((HOST_WIDE_INT) 1 << width) - 1;
1544
1545       arg0s = arg0;
1546       if (arg0s & ((HOST_WIDE_INT) 1 << (width - 1)))
1547         arg0s |= ((HOST_WIDE_INT) (-1) << width);
1548
1549       arg1s = arg1;
1550       if (arg1s & ((HOST_WIDE_INT) 1 << (width - 1)))
1551         arg1s |= ((HOST_WIDE_INT) (-1) << width);
1552     }
1553   else
1554     {
1555       arg0s = arg0;
1556       arg1s = arg1;
1557     }
1558
1559   /* Compute the value of the arithmetic.  */
1560
1561   switch (code)
1562     {
1563     case PLUS:
1564       val = arg0s + arg1s;
1565       break;
1566
1567     case MINUS:
1568       val = arg0s - arg1s;
1569       break;
1570
1571     case MULT:
1572       val = arg0s * arg1s;
1573       break;
1574
1575     case DIV:
1576       if (arg1s == 0
1577           || (arg0s == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)
1578               && arg1s == -1))
1579         return 0;
1580       val = arg0s / arg1s;
1581       break;
1582
1583     case MOD:
1584       if (arg1s == 0
1585           || (arg0s == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)
1586               && arg1s == -1))
1587         return 0;
1588       val = arg0s % arg1s;
1589       break;
1590
1591     case UDIV:
1592       if (arg1 == 0
1593           || (arg0s == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)
1594               && arg1s == -1))
1595         return 0;
1596       val = (unsigned HOST_WIDE_INT) arg0 / arg1;
1597       break;
1598
1599     case UMOD:
1600       if (arg1 == 0
1601           || (arg0s == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)
1602               && arg1s == -1))
1603         return 0;
1604       val = (unsigned HOST_WIDE_INT) arg0 % arg1;
1605       break;
1606
1607     case AND:
1608       val = arg0 & arg1;
1609       break;
1610
1611     case IOR:
1612       val = arg0 | arg1;
1613       break;
1614
1615     case XOR:
1616       val = arg0 ^ arg1;
1617       break;
1618
1619     case LSHIFTRT:
1620       /* If shift count is undefined, don't fold it; let the machine do
1621          what it wants.  But truncate it if the machine will do that.  */
1622       if (arg1 < 0)
1623         return 0;
1624
1625 #ifdef SHIFT_COUNT_TRUNCATED
1626       if (SHIFT_COUNT_TRUNCATED)
1627         arg1 %= width;
1628 #endif
1629
1630       val = ((unsigned HOST_WIDE_INT) arg0) >> arg1;
1631       break;
1632
1633     case ASHIFT:
1634       if (arg1 < 0)
1635         return 0;
1636
1637 #ifdef SHIFT_COUNT_TRUNCATED
1638       if (SHIFT_COUNT_TRUNCATED)
1639         arg1 %= width;
1640 #endif
1641
1642       val = ((unsigned HOST_WIDE_INT) arg0) << arg1;
1643       break;
1644
1645     case ASHIFTRT:
1646       if (arg1 < 0)
1647         return 0;
1648
1649 #ifdef SHIFT_COUNT_TRUNCATED
1650       if (SHIFT_COUNT_TRUNCATED)
1651         arg1 %= width;
1652 #endif
1653
1654       val = arg0s >> arg1;
1655
1656       /* Bootstrap compiler may not have sign extended the right shift.
1657          Manually extend the sign to insure bootstrap cc matches gcc.  */
1658       if (arg0s < 0 && arg1 > 0)
1659         val |= ((HOST_WIDE_INT) -1) << (HOST_BITS_PER_WIDE_INT - arg1);
1660
1661       break;
1662
1663     case ROTATERT:
1664       if (arg1 < 0)
1665         return 0;
1666
1667       arg1 %= width;
1668       val = ((((unsigned HOST_WIDE_INT) arg0) << (width - arg1))
1669              | (((unsigned HOST_WIDE_INT) arg0) >> arg1));
1670       break;
1671
1672     case ROTATE:
1673       if (arg1 < 0)
1674         return 0;
1675
1676       arg1 %= width;
1677       val = ((((unsigned HOST_WIDE_INT) arg0) << arg1)
1678              | (((unsigned HOST_WIDE_INT) arg0) >> (width - arg1)));
1679       break;
1680
1681     case COMPARE:
1682       /* Do nothing here.  */
1683       return 0;
1684
1685     case SMIN:
1686       val = arg0s <= arg1s ? arg0s : arg1s;
1687       break;
1688
1689     case UMIN:
1690       val = ((unsigned HOST_WIDE_INT) arg0
1691              <= (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1692       break;
1693
1694     case SMAX:
1695       val = arg0s > arg1s ? arg0s : arg1s;
1696       break;
1697
1698     case UMAX:
1699       val = ((unsigned HOST_WIDE_INT) arg0
1700              > (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1701       break;
1702
1703     default:
1704       abort ();
1705     }
1706
1707   val = trunc_int_for_mode (val, mode);
1708
1709   return GEN_INT (val);
1710 }
1711 \f
1712 /* Simplify a PLUS or MINUS, at least one of whose operands may be another
1713    PLUS or MINUS.
1714
1715    Rather than test for specific case, we do this by a brute-force method
1716    and do all possible simplifications until no more changes occur.  Then
1717    we rebuild the operation.  */
1718
1719 struct simplify_plus_minus_op_data
1720 {
1721   rtx op;
1722   int neg;
1723 };
1724
1725 static int
1726 simplify_plus_minus_op_data_cmp (p1, p2)
1727      const void *p1;
1728      const void *p2;
1729 {
1730   const struct simplify_plus_minus_op_data *d1 = p1;
1731   const struct simplify_plus_minus_op_data *d2 = p2;
1732
1733   return (commutative_operand_precedence (d2->op)
1734           - commutative_operand_precedence (d1->op));
1735 }
1736
1737 static rtx
1738 simplify_plus_minus (code, mode, op0, op1)
1739      enum rtx_code code;
1740      enum machine_mode mode;
1741      rtx op0, op1;
1742 {
1743   struct simplify_plus_minus_op_data ops[8];
1744   rtx result, tem;
1745   int n_ops = 2, input_ops = 2, input_consts = 0, n_consts;
1746   int first, negate, changed;
1747   int i, j;
1748
1749   memset ((char *) ops, 0, sizeof ops);
1750   
1751   /* Set up the two operands and then expand them until nothing has been
1752      changed.  If we run out of room in our array, give up; this should
1753      almost never happen.  */
1754
1755   ops[0].op = op0;
1756   ops[0].neg = 0;
1757   ops[1].op = op1;
1758   ops[1].neg = (code == MINUS);
1759
1760   do
1761     {
1762       changed = 0;
1763
1764       for (i = 0; i < n_ops; i++)
1765         {
1766           rtx this_op = ops[i].op;
1767           int this_neg = ops[i].neg;
1768           enum rtx_code this_code = GET_CODE (this_op);
1769
1770           switch (this_code)
1771             {
1772             case PLUS:
1773             case MINUS:
1774               if (n_ops == 7)
1775                 return 0;
1776
1777               ops[n_ops].op = XEXP (this_op, 1);
1778               ops[n_ops].neg = (this_code == MINUS) ^ this_neg;
1779               n_ops++;
1780
1781               ops[i].op = XEXP (this_op, 0);
1782               input_ops++;
1783               changed = 1;
1784               break;
1785
1786             case NEG:
1787               ops[i].op = XEXP (this_op, 0);
1788               ops[i].neg = ! this_neg;
1789               changed = 1;
1790               break;
1791
1792             case CONST:
1793               ops[i].op = XEXP (this_op, 0);
1794               input_consts++;
1795               changed = 1;
1796               break;
1797
1798             case NOT:
1799               /* ~a -> (-a - 1) */
1800               if (n_ops != 7)
1801                 {
1802                   ops[n_ops].op = constm1_rtx;
1803                   ops[n_ops++].neg = this_neg;
1804                   ops[i].op = XEXP (this_op, 0);
1805                   ops[i].neg = !this_neg;
1806                   changed = 1;
1807                 }
1808               break;
1809
1810             case CONST_INT:
1811               if (this_neg)
1812                 {
1813                   ops[i].op = neg_const_int (mode, this_op);
1814                   ops[i].neg = 0;
1815                   changed = 1;
1816                 }
1817               break;
1818
1819             default:
1820               break;
1821             }
1822         }
1823     }
1824   while (changed);
1825
1826   /* If we only have two operands, we can't do anything.  */
1827   if (n_ops <= 2)
1828     return NULL_RTX;
1829
1830   /* Now simplify each pair of operands until nothing changes.  The first
1831      time through just simplify constants against each other.  */
1832
1833   first = 1;
1834   do
1835     {
1836       changed = first;
1837
1838       for (i = 0; i < n_ops - 1; i++)
1839         for (j = i + 1; j < n_ops; j++)
1840           {
1841             rtx lhs = ops[i].op, rhs = ops[j].op;
1842             int lneg = ops[i].neg, rneg = ops[j].neg;
1843
1844             if (lhs != 0 && rhs != 0
1845                 && (! first || (CONSTANT_P (lhs) && CONSTANT_P (rhs))))
1846               {
1847                 enum rtx_code ncode = PLUS;
1848
1849                 if (lneg != rneg)
1850                   {
1851                     ncode = MINUS;
1852                     if (lneg)
1853                       tem = lhs, lhs = rhs, rhs = tem;
1854                   }
1855                 else if (swap_commutative_operands_p (lhs, rhs))
1856                   tem = lhs, lhs = rhs, rhs = tem;
1857
1858                 tem = simplify_binary_operation (ncode, mode, lhs, rhs);
1859
1860                 /* Reject "simplifications" that just wrap the two 
1861                    arguments in a CONST.  Failure to do so can result
1862                    in infinite recursion with simplify_binary_operation
1863                    when it calls us to simplify CONST operations.  */
1864                 if (tem
1865                     && ! (GET_CODE (tem) == CONST
1866                           && GET_CODE (XEXP (tem, 0)) == ncode
1867                           && XEXP (XEXP (tem, 0), 0) == lhs
1868                           && XEXP (XEXP (tem, 0), 1) == rhs)
1869                     /* Don't allow -x + -1 -> ~x simplifications in the
1870                        first pass.  This allows us the chance to combine
1871                        the -1 with other constants.  */
1872                     && ! (first
1873                           && GET_CODE (tem) == NOT
1874                           && XEXP (tem, 0) == rhs))
1875                   {
1876                     lneg &= rneg;
1877                     if (GET_CODE (tem) == NEG)
1878                       tem = XEXP (tem, 0), lneg = !lneg;
1879                     if (GET_CODE (tem) == CONST_INT && lneg)
1880                       tem = neg_const_int (mode, tem), lneg = 0;
1881
1882                     ops[i].op = tem;
1883                     ops[i].neg = lneg;
1884                     ops[j].op = NULL_RTX;
1885                     changed = 1;
1886                   }
1887               }
1888           }
1889
1890       first = 0;
1891     }
1892   while (changed);
1893
1894   /* Pack all the operands to the lower-numbered entries.  */
1895   for (i = 0, j = 0; j < n_ops; j++)
1896     if (ops[j].op)
1897       ops[i++] = ops[j];
1898   n_ops = i;
1899
1900   /* Sort the operations based on swap_commutative_operands_p.  */
1901   qsort (ops, n_ops, sizeof (*ops), simplify_plus_minus_op_data_cmp);
1902
1903   /* We suppressed creation of trivial CONST expressions in the
1904      combination loop to avoid recursion.  Create one manually now.
1905      The combination loop should have ensured that there is exactly
1906      one CONST_INT, and the sort will have ensured that it is last
1907      in the array and that any other constant will be next-to-last.  */
1908
1909   if (n_ops > 1
1910       && GET_CODE (ops[n_ops - 1].op) == CONST_INT
1911       && CONSTANT_P (ops[n_ops - 2].op))
1912     {
1913       rtx value = ops[n_ops - 1].op;
1914       if (ops[n_ops - 1].neg ^ ops[n_ops - 2].neg)
1915         value = neg_const_int (mode, value);
1916       ops[n_ops - 2].op = plus_constant (ops[n_ops - 2].op, INTVAL (value));
1917       n_ops--;
1918     }
1919
1920   /* Count the number of CONSTs that we generated.  */
1921   n_consts = 0;
1922   for (i = 0; i < n_ops; i++)
1923     if (GET_CODE (ops[i].op) == CONST)
1924       n_consts++;
1925
1926   /* Give up if we didn't reduce the number of operands we had.  Make
1927      sure we count a CONST as two operands.  If we have the same
1928      number of operands, but have made more CONSTs than before, this
1929      is also an improvement, so accept it.  */
1930   if (n_ops + n_consts > input_ops
1931       || (n_ops + n_consts == input_ops && n_consts <= input_consts))
1932     return NULL_RTX;
1933
1934   /* Put a non-negated operand first.  If there aren't any, make all
1935      operands positive and negate the whole thing later.  */
1936
1937   negate = 0;
1938   for (i = 0; i < n_ops && ops[i].neg; i++)
1939     continue;
1940   if (i == n_ops)
1941     {
1942       for (i = 0; i < n_ops; i++)
1943         ops[i].neg = 0;
1944       negate = 1;
1945     }
1946   else if (i != 0)
1947     {
1948       tem = ops[0].op;
1949       ops[0] = ops[i];
1950       ops[i].op = tem;
1951       ops[i].neg = 1;
1952     }
1953
1954   /* Now make the result by performing the requested operations.  */
1955   result = ops[0].op;
1956   for (i = 1; i < n_ops; i++)
1957     result = gen_rtx_fmt_ee (ops[i].neg ? MINUS : PLUS,
1958                              mode, result, ops[i].op);
1959
1960   return negate ? gen_rtx_NEG (mode, result) : result;
1961 }
1962
1963 struct cfc_args
1964 {
1965   rtx op0, op1;                 /* Input */
1966   int equal, op0lt, op1lt;      /* Output */
1967   int unordered;
1968 };
1969
1970 static void
1971 check_fold_consts (data)
1972   PTR data;
1973 {
1974   struct cfc_args *args = (struct cfc_args *) data;
1975   REAL_VALUE_TYPE d0, d1;
1976
1977   /* We may possibly raise an exception while reading the value.  */
1978   args->unordered = 1;
1979   REAL_VALUE_FROM_CONST_DOUBLE (d0, args->op0);
1980   REAL_VALUE_FROM_CONST_DOUBLE (d1, args->op1);
1981
1982   /* Comparisons of Inf versus Inf are ordered.  */
1983   if (REAL_VALUE_ISNAN (d0)
1984       || REAL_VALUE_ISNAN (d1))
1985     return;
1986   args->equal = REAL_VALUES_EQUAL (d0, d1);
1987   args->op0lt = REAL_VALUES_LESS (d0, d1);
1988   args->op1lt = REAL_VALUES_LESS (d1, d0);
1989   args->unordered = 0;
1990 }
1991
1992 /* Like simplify_binary_operation except used for relational operators.
1993    MODE is the mode of the operands, not that of the result.  If MODE
1994    is VOIDmode, both operands must also be VOIDmode and we compare the
1995    operands in "infinite precision".
1996
1997    If no simplification is possible, this function returns zero.  Otherwise,
1998    it returns either const_true_rtx or const0_rtx.  */
1999
2000 rtx
2001 simplify_relational_operation (code, mode, op0, op1)
2002      enum rtx_code code;
2003      enum machine_mode mode;
2004      rtx op0, op1;
2005 {
2006   int equal, op0lt, op0ltu, op1lt, op1ltu;
2007   rtx tem;
2008   rtx trueop0;
2009   rtx trueop1;
2010
2011   if (mode == VOIDmode
2012       && (GET_MODE (op0) != VOIDmode
2013           || GET_MODE (op1) != VOIDmode))
2014     abort ();
2015
2016   /* If op0 is a compare, extract the comparison arguments from it.  */
2017   if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
2018     op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
2019
2020   trueop0 = avoid_constant_pool_reference (op0);
2021   trueop1 = avoid_constant_pool_reference (op1);
2022
2023   /* We can't simplify MODE_CC values since we don't know what the
2024      actual comparison is.  */
2025   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC
2026 #ifdef HAVE_cc0
2027       || op0 == cc0_rtx
2028 #endif
2029       )
2030     return 0;
2031
2032   /* Make sure the constant is second.  */
2033   if (swap_commutative_operands_p (trueop0, trueop1))
2034     {
2035       tem = op0, op0 = op1, op1 = tem;
2036       tem = trueop0, trueop0 = trueop1, trueop1 = tem;
2037       code = swap_condition (code);
2038     }
2039
2040   /* For integer comparisons of A and B maybe we can simplify A - B and can
2041      then simplify a comparison of that with zero.  If A and B are both either
2042      a register or a CONST_INT, this can't help; testing for these cases will
2043      prevent infinite recursion here and speed things up.
2044
2045      If CODE is an unsigned comparison, then we can never do this optimization,
2046      because it gives an incorrect result if the subtraction wraps around zero.
2047      ANSI C defines unsigned operations such that they never overflow, and
2048      thus such cases can not be ignored.  */
2049
2050   if (INTEGRAL_MODE_P (mode) && trueop1 != const0_rtx
2051       && ! ((GET_CODE (op0) == REG || GET_CODE (trueop0) == CONST_INT)
2052             && (GET_CODE (op1) == REG || GET_CODE (trueop1) == CONST_INT))
2053       && 0 != (tem = simplify_binary_operation (MINUS, mode, op0, op1))
2054       && code != GTU && code != GEU && code != LTU && code != LEU)
2055     return simplify_relational_operation (signed_condition (code),
2056                                           mode, tem, const0_rtx);
2057
2058   if (flag_unsafe_math_optimizations && code == ORDERED)
2059     return const_true_rtx;
2060
2061   if (flag_unsafe_math_optimizations && code == UNORDERED)
2062     return const0_rtx;
2063
2064   /* For non-IEEE floating-point, if the two operands are equal, we know the
2065      result.  */
2066   if (rtx_equal_p (trueop0, trueop1)
2067       && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
2068           || ! FLOAT_MODE_P (GET_MODE (trueop0)) 
2069           || flag_unsafe_math_optimizations))
2070     equal = 1, op0lt = 0, op0ltu = 0, op1lt = 0, op1ltu = 0;
2071
2072   /* If the operands are floating-point constants, see if we can fold
2073      the result.  */
2074 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2075   else if (GET_CODE (trueop0) == CONST_DOUBLE
2076            && GET_CODE (trueop1) == CONST_DOUBLE
2077            && GET_MODE_CLASS (GET_MODE (trueop0)) == MODE_FLOAT)
2078     {
2079       struct cfc_args args;
2080
2081       /* Setup input for check_fold_consts() */
2082       args.op0 = trueop0;
2083       args.op1 = trueop1;
2084       
2085       
2086       if (!do_float_handler (check_fold_consts, (PTR) &args))
2087         args.unordered = 1;
2088
2089       if (args.unordered)
2090         switch (code)
2091           {
2092           case UNEQ:
2093           case UNLT:
2094           case UNGT:
2095           case UNLE:
2096           case UNGE:
2097           case NE:
2098           case UNORDERED:
2099             return const_true_rtx;
2100           case EQ:
2101           case LT:
2102           case GT:
2103           case LE:
2104           case GE:
2105           case LTGT:
2106           case ORDERED:
2107             return const0_rtx;
2108           default:
2109             return 0;
2110           }
2111
2112       /* Receive output from check_fold_consts() */
2113       equal = args.equal;
2114       op0lt = op0ltu = args.op0lt;
2115       op1lt = op1ltu = args.op1lt;
2116     }
2117 #endif  /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2118
2119   /* Otherwise, see if the operands are both integers.  */
2120   else if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
2121            && (GET_CODE (trueop0) == CONST_DOUBLE
2122                || GET_CODE (trueop0) == CONST_INT)
2123            && (GET_CODE (trueop1) == CONST_DOUBLE
2124                || GET_CODE (trueop1) == CONST_INT))
2125     {
2126       int width = GET_MODE_BITSIZE (mode);
2127       HOST_WIDE_INT l0s, h0s, l1s, h1s;
2128       unsigned HOST_WIDE_INT l0u, h0u, l1u, h1u;
2129
2130       /* Get the two words comprising each integer constant.  */
2131       if (GET_CODE (trueop0) == CONST_DOUBLE)
2132         {
2133           l0u = l0s = CONST_DOUBLE_LOW (trueop0);
2134           h0u = h0s = CONST_DOUBLE_HIGH (trueop0);
2135         }
2136       else
2137         {
2138           l0u = l0s = INTVAL (trueop0);
2139           h0u = h0s = HWI_SIGN_EXTEND (l0s);
2140         }
2141           
2142       if (GET_CODE (trueop1) == CONST_DOUBLE)
2143         {
2144           l1u = l1s = CONST_DOUBLE_LOW (trueop1);
2145           h1u = h1s = CONST_DOUBLE_HIGH (trueop1);
2146         }
2147       else
2148         {
2149           l1u = l1s = INTVAL (trueop1);
2150           h1u = h1s = HWI_SIGN_EXTEND (l1s);
2151         }
2152
2153       /* If WIDTH is nonzero and smaller than HOST_BITS_PER_WIDE_INT,
2154          we have to sign or zero-extend the values.  */
2155       if (width != 0 && width < HOST_BITS_PER_WIDE_INT)
2156         {
2157           l0u &= ((HOST_WIDE_INT) 1 << width) - 1;
2158           l1u &= ((HOST_WIDE_INT) 1 << width) - 1;
2159
2160           if (l0s & ((HOST_WIDE_INT) 1 << (width - 1)))
2161             l0s |= ((HOST_WIDE_INT) (-1) << width);
2162
2163           if (l1s & ((HOST_WIDE_INT) 1 << (width - 1)))
2164             l1s |= ((HOST_WIDE_INT) (-1) << width);
2165         }
2166       if (width != 0 && width <= HOST_BITS_PER_WIDE_INT)
2167         h0u = h1u = 0, h0s = HWI_SIGN_EXTEND (l0s), h1s = HWI_SIGN_EXTEND (l1s);
2168
2169       equal = (h0u == h1u && l0u == l1u);
2170       op0lt = (h0s < h1s || (h0s == h1s && l0u < l1u));
2171       op1lt = (h1s < h0s || (h1s == h0s && l1u < l0u));
2172       op0ltu = (h0u < h1u || (h0u == h1u && l0u < l1u));
2173       op1ltu = (h1u < h0u || (h1u == h0u && l1u < l0u));
2174     }
2175
2176   /* Otherwise, there are some code-specific tests we can make.  */
2177   else
2178     {
2179       switch (code)
2180         {
2181         case EQ:
2182           /* References to the frame plus a constant or labels cannot
2183              be zero, but a SYMBOL_REF can due to #pragma weak.  */
2184           if (((NONZERO_BASE_PLUS_P (op0) && trueop1 == const0_rtx)
2185                || GET_CODE (trueop0) == LABEL_REF)
2186 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2187               /* On some machines, the ap reg can be 0 sometimes.  */
2188               && op0 != arg_pointer_rtx
2189 #endif
2190                 )
2191             return const0_rtx;
2192           break;
2193
2194         case NE:
2195           if (((NONZERO_BASE_PLUS_P (op0) && trueop1 == const0_rtx)
2196                || GET_CODE (trueop0) == LABEL_REF)
2197 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2198               && op0 != arg_pointer_rtx
2199 #endif
2200               )
2201             return const_true_rtx;
2202           break;
2203
2204         case GEU:
2205           /* Unsigned values are never negative.  */
2206           if (trueop1 == const0_rtx)
2207             return const_true_rtx;
2208           break;
2209
2210         case LTU:
2211           if (trueop1 == const0_rtx)
2212             return const0_rtx;
2213           break;
2214
2215         case LEU:
2216           /* Unsigned values are never greater than the largest
2217              unsigned value.  */
2218           if (GET_CODE (trueop1) == CONST_INT
2219               && (unsigned HOST_WIDE_INT) INTVAL (trueop1) == GET_MODE_MASK (mode)
2220             && INTEGRAL_MODE_P (mode))
2221           return const_true_rtx;
2222           break;
2223
2224         case GTU:
2225           if (GET_CODE (trueop1) == CONST_INT
2226               && (unsigned HOST_WIDE_INT) INTVAL (trueop1) == GET_MODE_MASK (mode)
2227               && INTEGRAL_MODE_P (mode))
2228             return const0_rtx;
2229           break;
2230           
2231         default:
2232           break;
2233         }
2234
2235       return 0;
2236     }
2237
2238   /* If we reach here, EQUAL, OP0LT, OP0LTU, OP1LT, and OP1LTU are set
2239      as appropriate.  */
2240   switch (code)
2241     {
2242     case EQ:
2243     case UNEQ:
2244       return equal ? const_true_rtx : const0_rtx;
2245     case NE:
2246     case LTGT:
2247       return ! equal ? const_true_rtx : const0_rtx;
2248     case LT:
2249     case UNLT:
2250       return op0lt ? const_true_rtx : const0_rtx;
2251     case GT:
2252     case UNGT:
2253       return op1lt ? const_true_rtx : const0_rtx;
2254     case LTU:
2255       return op0ltu ? const_true_rtx : const0_rtx;
2256     case GTU:
2257       return op1ltu ? const_true_rtx : const0_rtx;
2258     case LE:
2259     case UNLE:
2260       return equal || op0lt ? const_true_rtx : const0_rtx;
2261     case GE:
2262     case UNGE:
2263       return equal || op1lt ? const_true_rtx : const0_rtx;
2264     case LEU:
2265       return equal || op0ltu ? const_true_rtx : const0_rtx;
2266     case GEU:
2267       return equal || op1ltu ? const_true_rtx : const0_rtx;
2268     case ORDERED:
2269       return const_true_rtx;
2270     case UNORDERED:
2271       return const0_rtx;
2272     default:
2273       abort ();
2274     }
2275 }
2276 \f
2277 /* Simplify CODE, an operation with result mode MODE and three operands,
2278    OP0, OP1, and OP2.  OP0_MODE was the mode of OP0 before it became
2279    a constant.  Return 0 if no simplifications is possible.  */
2280
2281 rtx
2282 simplify_ternary_operation (code, mode, op0_mode, op0, op1, op2)
2283      enum rtx_code code;
2284      enum machine_mode mode, op0_mode;
2285      rtx op0, op1, op2;
2286 {
2287   unsigned int width = GET_MODE_BITSIZE (mode);
2288
2289   /* VOIDmode means "infinite" precision.  */
2290   if (width == 0)
2291     width = HOST_BITS_PER_WIDE_INT;
2292
2293   switch (code)
2294     {
2295     case SIGN_EXTRACT:
2296     case ZERO_EXTRACT:
2297       if (GET_CODE (op0) == CONST_INT
2298           && GET_CODE (op1) == CONST_INT
2299           && GET_CODE (op2) == CONST_INT
2300           && ((unsigned) INTVAL (op1) + (unsigned) INTVAL (op2) <= width)
2301           && width <= (unsigned) HOST_BITS_PER_WIDE_INT)
2302         {
2303           /* Extracting a bit-field from a constant */
2304           HOST_WIDE_INT val = INTVAL (op0);
2305
2306           if (BITS_BIG_ENDIAN)
2307             val >>= (GET_MODE_BITSIZE (op0_mode)
2308                      - INTVAL (op2) - INTVAL (op1));
2309           else
2310             val >>= INTVAL (op2);
2311
2312           if (HOST_BITS_PER_WIDE_INT != INTVAL (op1))
2313             {
2314               /* First zero-extend.  */
2315               val &= ((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1;
2316               /* If desired, propagate sign bit.  */
2317               if (code == SIGN_EXTRACT
2318                   && (val & ((HOST_WIDE_INT) 1 << (INTVAL (op1) - 1))))
2319                 val |= ~ (((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1);
2320             }
2321
2322           /* Clear the bits that don't belong in our mode,
2323              unless they and our sign bit are all one.
2324              So we get either a reasonable negative value or a reasonable
2325              unsigned value for this mode.  */
2326           if (width < HOST_BITS_PER_WIDE_INT
2327               && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
2328                   != ((HOST_WIDE_INT) (-1) << (width - 1))))
2329             val &= ((HOST_WIDE_INT) 1 << width) - 1;
2330
2331           return GEN_INT (val);
2332         }
2333       break;
2334
2335     case IF_THEN_ELSE:
2336       if (GET_CODE (op0) == CONST_INT)
2337         return op0 != const0_rtx ? op1 : op2;
2338
2339       /* Convert a == b ? b : a to "a".  */
2340       if (GET_CODE (op0) == NE && ! side_effects_p (op0)
2341           && (! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
2342           && rtx_equal_p (XEXP (op0, 0), op1)
2343           && rtx_equal_p (XEXP (op0, 1), op2))
2344         return op1;
2345       else if (GET_CODE (op0) == EQ && ! side_effects_p (op0)
2346           && (! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
2347           && rtx_equal_p (XEXP (op0, 1), op1)
2348           && rtx_equal_p (XEXP (op0, 0), op2))
2349         return op2;
2350       else if (GET_RTX_CLASS (GET_CODE (op0)) == '<' && ! side_effects_p (op0))
2351         {
2352           enum machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
2353                                         ? GET_MODE (XEXP (op0, 1))
2354                                         : GET_MODE (XEXP (op0, 0)));
2355           rtx temp;
2356           if (cmp_mode == VOIDmode)
2357             cmp_mode = op0_mode;
2358           temp = simplify_relational_operation (GET_CODE (op0), cmp_mode,
2359                                                 XEXP (op0, 0), XEXP (op0, 1));
2360
2361           /* See if any simplifications were possible.  */
2362           if (temp == const0_rtx)
2363             return op2;
2364           else if (temp == const1_rtx)
2365             return op1;
2366           else if (temp)
2367             op0 = temp;
2368
2369           /* Look for happy constants in op1 and op2.  */
2370           if (GET_CODE (op1) == CONST_INT && GET_CODE (op2) == CONST_INT)
2371             {
2372               HOST_WIDE_INT t = INTVAL (op1);
2373               HOST_WIDE_INT f = INTVAL (op2);
2374               
2375               if (t == STORE_FLAG_VALUE && f == 0)
2376                 code = GET_CODE (op0);
2377               else if (t == 0 && f == STORE_FLAG_VALUE)
2378                 {
2379                   enum rtx_code tmp;
2380                   tmp = reversed_comparison_code (op0, NULL_RTX);
2381                   if (tmp == UNKNOWN)
2382                     break;
2383                   code = tmp;
2384                 }
2385               else
2386                 break;
2387
2388               return gen_rtx_fmt_ee (code, mode, XEXP (op0, 0), XEXP (op0, 1));
2389             }
2390         }
2391       break;
2392
2393     default:
2394       abort ();
2395     }
2396
2397   return 0;
2398 }
2399
2400 /* Simplify SUBREG:OUTERMODE(OP:INNERMODE, BYTE)
2401    Return 0 if no simplifications is possible.  */
2402 rtx
2403 simplify_subreg (outermode, op, innermode, byte)
2404      rtx op;
2405      unsigned int byte;
2406      enum machine_mode outermode, innermode;
2407 {
2408   /* Little bit of sanity checking.  */
2409   if (innermode == VOIDmode || outermode == VOIDmode
2410       || innermode == BLKmode || outermode == BLKmode)
2411     abort ();
2412
2413   if (GET_MODE (op) != innermode
2414       && GET_MODE (op) != VOIDmode)
2415     abort ();
2416
2417   if (byte % GET_MODE_SIZE (outermode)
2418       || byte >= GET_MODE_SIZE (innermode))
2419     abort ();
2420
2421   if (outermode == innermode && !byte)
2422     return op;
2423
2424   /* Attempt to simplify constant to non-SUBREG expression.  */
2425   if (CONSTANT_P (op))
2426     {
2427       int offset, part;
2428       unsigned HOST_WIDE_INT val = 0;
2429
2430       /* ??? This code is partly redundant with code below, but can handle
2431          the subregs of floats and similar corner cases.
2432          Later it we should move all simplification code here and rewrite
2433          GEN_LOWPART_IF_POSSIBLE, GEN_HIGHPART, OPERAND_SUBWORD and friends
2434          using SIMPLIFY_SUBREG.  */
2435       if (subreg_lowpart_offset (outermode, innermode) == byte)
2436         {
2437           rtx new = gen_lowpart_if_possible (outermode, op);
2438           if (new)
2439             return new;
2440         }
2441
2442       /* Similar comment as above apply here.  */
2443       if (GET_MODE_SIZE (outermode) == UNITS_PER_WORD
2444           && GET_MODE_SIZE (innermode) > UNITS_PER_WORD
2445           && GET_MODE_CLASS (outermode) == MODE_INT)
2446         {
2447           rtx new = constant_subword (op,
2448                                       (byte / UNITS_PER_WORD),
2449                                       innermode);
2450           if (new)
2451             return new;
2452         }
2453
2454       offset = byte * BITS_PER_UNIT;
2455       switch (GET_CODE (op))
2456         {
2457         case CONST_DOUBLE:
2458           if (GET_MODE (op) != VOIDmode)
2459             break;
2460
2461           /* We can't handle this case yet.  */
2462           if (GET_MODE_BITSIZE (outermode) >= HOST_BITS_PER_WIDE_INT)
2463             return NULL_RTX;
2464
2465           part = offset >= HOST_BITS_PER_WIDE_INT;
2466           if ((BITS_PER_WORD > HOST_BITS_PER_WIDE_INT
2467                && BYTES_BIG_ENDIAN)
2468               || (BITS_PER_WORD <= HOST_BITS_PER_WIDE_INT
2469                   && WORDS_BIG_ENDIAN))
2470             part = !part;
2471           val = part ? CONST_DOUBLE_HIGH (op) : CONST_DOUBLE_LOW (op);
2472           offset %= HOST_BITS_PER_WIDE_INT;
2473
2474           /* We've already picked the word we want from a double, so 
2475              pretend this is actually an integer.  */
2476           innermode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
2477
2478           /* FALLTHROUGH */
2479         case CONST_INT:
2480           if (GET_CODE (op) == CONST_INT)
2481             val = INTVAL (op);
2482
2483           /* We don't handle synthetizing of non-integral constants yet.  */
2484           if (GET_MODE_CLASS (outermode) != MODE_INT)
2485             return NULL_RTX;
2486
2487           if (BYTES_BIG_ENDIAN || WORDS_BIG_ENDIAN)
2488             {
2489               if (WORDS_BIG_ENDIAN)
2490                 offset = (GET_MODE_BITSIZE (innermode)
2491                           - GET_MODE_BITSIZE (outermode) - offset);
2492               if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
2493                   && GET_MODE_SIZE (outermode) < UNITS_PER_WORD)
2494                 offset = (offset + BITS_PER_WORD - GET_MODE_BITSIZE (outermode)
2495                           - 2 * (offset % BITS_PER_WORD));
2496             }
2497
2498           if (offset >= HOST_BITS_PER_WIDE_INT)
2499             return ((HOST_WIDE_INT) val < 0) ? constm1_rtx : const0_rtx;
2500           else
2501             {
2502               val >>= offset;
2503               if (GET_MODE_BITSIZE (outermode) < HOST_BITS_PER_WIDE_INT)
2504                 val = trunc_int_for_mode (val, outermode);
2505               return GEN_INT (val);
2506             }
2507         default:
2508           break;
2509         }
2510     }
2511
2512   /* Changing mode twice with SUBREG => just change it once,
2513      or not at all if changing back op starting mode.  */
2514   if (GET_CODE (op) == SUBREG)
2515     {
2516       enum machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
2517       int final_offset = byte + SUBREG_BYTE (op);
2518       rtx new;
2519
2520       if (outermode == innermostmode
2521           && byte == 0 && SUBREG_BYTE (op) == 0)
2522         return SUBREG_REG (op);
2523
2524       /* The SUBREG_BYTE represents offset, as if the value were stored
2525          in memory.  Irritating exception is paradoxical subreg, where
2526          we define SUBREG_BYTE to be 0.  On big endian machines, this
2527          value should be negative.  For a moment, undo this exception.  */
2528       if (byte == 0 && GET_MODE_SIZE (innermode) < GET_MODE_SIZE (outermode))
2529         {
2530           int difference = (GET_MODE_SIZE (innermode) - GET_MODE_SIZE (outermode));
2531           if (WORDS_BIG_ENDIAN)
2532             final_offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
2533           if (BYTES_BIG_ENDIAN)
2534             final_offset += difference % UNITS_PER_WORD;
2535         }
2536       if (SUBREG_BYTE (op) == 0
2537           && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
2538         {
2539           int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
2540           if (WORDS_BIG_ENDIAN)
2541             final_offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
2542           if (BYTES_BIG_ENDIAN)
2543             final_offset += difference % UNITS_PER_WORD;
2544         }
2545
2546       /* See whether resulting subreg will be paradoxical.  */
2547       if (GET_MODE_SIZE (innermostmode) > GET_MODE_SIZE (outermode))
2548         {
2549           /* In nonparadoxical subregs we can't handle negative offsets.  */
2550           if (final_offset < 0)
2551             return NULL_RTX;
2552           /* Bail out in case resulting subreg would be incorrect.  */
2553           if (final_offset % GET_MODE_SIZE (outermode)
2554               || (unsigned) final_offset >= GET_MODE_SIZE (innermostmode))
2555             return NULL_RTX;
2556         }
2557       else
2558         {
2559           int offset = 0;
2560           int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (outermode));
2561
2562           /* In paradoxical subreg, see if we are still looking on lower part.
2563              If so, our SUBREG_BYTE will be 0.  */
2564           if (WORDS_BIG_ENDIAN)
2565             offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
2566           if (BYTES_BIG_ENDIAN)
2567             offset += difference % UNITS_PER_WORD;
2568           if (offset == final_offset)
2569             final_offset = 0;
2570           else
2571             return NULL_RTX;
2572         }
2573
2574       /* Recurse for futher possible simplifications.  */
2575       new = simplify_subreg (outermode, SUBREG_REG (op),
2576                              GET_MODE (SUBREG_REG (op)),
2577                              final_offset);
2578       if (new)
2579         return new;
2580       return gen_rtx_SUBREG (outermode, SUBREG_REG (op), final_offset);
2581     }
2582
2583   /* SUBREG of a hard register => just change the register number
2584      and/or mode.  If the hard register is not valid in that mode,
2585      suppress this simplification.  If the hard register is the stack,
2586      frame, or argument pointer, leave this as a SUBREG.  */
2587
2588   if (REG_P (op)
2589       && (! REG_FUNCTION_VALUE_P (op)
2590           || ! rtx_equal_function_value_matters)
2591 #ifdef CLASS_CANNOT_CHANGE_MODE
2592       && ! (CLASS_CANNOT_CHANGE_MODE_P (outermode, innermode)
2593             && GET_MODE_CLASS (innermode) != MODE_COMPLEX_INT
2594             && GET_MODE_CLASS (innermode) != MODE_COMPLEX_FLOAT
2595             && (TEST_HARD_REG_BIT
2596                 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_MODE],
2597                  REGNO (op))))
2598 #endif
2599       && REGNO (op) < FIRST_PSEUDO_REGISTER
2600       && ((reload_completed && !frame_pointer_needed)
2601           || (REGNO (op) != FRAME_POINTER_REGNUM
2602 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2603               && REGNO (op) != HARD_FRAME_POINTER_REGNUM
2604 #endif
2605              ))
2606 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2607       && REGNO (op) != ARG_POINTER_REGNUM
2608 #endif
2609       && REGNO (op) != STACK_POINTER_REGNUM)
2610     {
2611       int final_regno = subreg_hard_regno (gen_rtx_SUBREG (outermode, op, byte),
2612                                            0);
2613
2614       /* ??? We do allow it if the current REG is not valid for
2615          its mode.  This is a kludge to work around how float/complex
2616          arguments are passed on 32-bit Sparc and should be fixed.  */
2617       if (HARD_REGNO_MODE_OK (final_regno, outermode)
2618           || ! HARD_REGNO_MODE_OK (REGNO (op), innermode))
2619         {
2620           rtx x = gen_rtx_REG (outermode, final_regno);
2621
2622           /* Propagate original regno.  We don't have any way to specify
2623              the offset inside orignal regno, so do so only for lowpart.
2624              The information is used only by alias analysis that can not
2625              grog partial register anyway.  */
2626
2627           if (subreg_lowpart_offset (outermode, innermode) == byte)
2628             ORIGINAL_REGNO (x) = ORIGINAL_REGNO (op);
2629           return x;
2630         }
2631     }
2632
2633   /* If we have a SUBREG of a register that we are replacing and we are
2634      replacing it with a MEM, make a new MEM and try replacing the
2635      SUBREG with it.  Don't do this if the MEM has a mode-dependent address
2636      or if we would be widening it.  */
2637
2638   if (GET_CODE (op) == MEM
2639       && ! mode_dependent_address_p (XEXP (op, 0))
2640       /* Allow splitting of volatile memory references in case we don't
2641          have instruction to move the whole thing.  */
2642       && (! MEM_VOLATILE_P (op)
2643           || ! have_insn_for (SET, innermode))
2644       && GET_MODE_SIZE (outermode) <= GET_MODE_SIZE (GET_MODE (op)))
2645     return adjust_address_nv (op, outermode, byte);
2646
2647   /* Handle complex values represented as CONCAT
2648      of real and imaginary part.  */
2649   if (GET_CODE (op) == CONCAT)
2650     {
2651       int is_realpart = byte < GET_MODE_UNIT_SIZE (innermode);
2652       rtx part = is_realpart ? XEXP (op, 0) : XEXP (op, 1);
2653       unsigned int final_offset;
2654       rtx res;
2655
2656       final_offset = byte % (GET_MODE_UNIT_SIZE (innermode));
2657       res = simplify_subreg (outermode, part, GET_MODE (part), final_offset);
2658       if (res)
2659         return res;
2660       /* We can at least simplify it by referring directly to the relevant part.  */
2661       return gen_rtx_SUBREG (outermode, part, final_offset);
2662     }
2663
2664   return NULL_RTX;
2665 }
2666 /* Make a SUBREG operation or equivalent if it folds.  */
2667
2668 rtx
2669 simplify_gen_subreg (outermode, op, innermode, byte)
2670      rtx op;
2671      unsigned int byte;
2672      enum machine_mode outermode, innermode;
2673 {
2674   rtx new;
2675   /* Little bit of sanity checking.  */
2676   if (innermode == VOIDmode || outermode == VOIDmode
2677       || innermode == BLKmode || outermode == BLKmode)
2678     abort ();
2679
2680   if (GET_MODE (op) != innermode
2681       && GET_MODE (op) != VOIDmode)
2682     abort ();
2683
2684   if (byte % GET_MODE_SIZE (outermode)
2685       || byte >= GET_MODE_SIZE (innermode))
2686     abort ();
2687
2688   if (GET_CODE (op) == QUEUED)
2689     return NULL_RTX;
2690
2691   new = simplify_subreg (outermode, op, innermode, byte);
2692   if (new)
2693     return new;
2694
2695   if (GET_CODE (op) == SUBREG || GET_MODE (op) == VOIDmode)
2696     return NULL_RTX;
2697
2698   return gen_rtx_SUBREG (outermode, op, byte);
2699 }
2700 /* Simplify X, an rtx expression.
2701
2702    Return the simplified expression or NULL if no simplifications
2703    were possible.
2704
2705    This is the preferred entry point into the simplification routines;
2706    however, we still allow passes to call the more specific routines.
2707
2708    Right now GCC has three (yes, three) major bodies of RTL simplficiation
2709    code that need to be unified.
2710
2711         1. fold_rtx in cse.c.  This code uses various CSE specific
2712            information to aid in RTL simplification.
2713
2714         2. simplify_rtx in combine.c.  Similar to fold_rtx, except that
2715            it uses combine specific information to aid in RTL
2716            simplification.
2717
2718         3. The routines in this file.
2719
2720
2721    Long term we want to only have one body of simplification code; to
2722    get to that state I recommend the following steps:
2723
2724         1. Pour over fold_rtx & simplify_rtx and move any simplifications
2725            which are not pass dependent state into these routines.
2726
2727         2. As code is moved by #1, change fold_rtx & simplify_rtx to
2728            use this routine whenever possible.
2729
2730         3. Allow for pass dependent state to be provided to these
2731            routines and add simplifications based on the pass dependent
2732            state.  Remove code from cse.c & combine.c that becomes
2733            redundant/dead.
2734
2735     It will take time, but ultimately the compiler will be easier to
2736     maintain and improve.  It's totally silly that when we add a
2737     simplification that it needs to be added to 4 places (3 for RTL
2738     simplification and 1 for tree simplification.  */
2739            
2740 rtx
2741 simplify_rtx (x)
2742      rtx x;
2743 {
2744   enum rtx_code code = GET_CODE (x);
2745   enum machine_mode mode = GET_MODE (x);
2746
2747   switch (GET_RTX_CLASS (code))
2748     {
2749     case '1':
2750       return simplify_unary_operation (code, mode,
2751                                        XEXP (x, 0), GET_MODE (XEXP (x, 0)));
2752     case 'c':
2753       if (swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
2754         {
2755           rtx tem;
2756
2757           tem = XEXP (x, 0);
2758           XEXP (x, 0) = XEXP (x, 1);
2759           XEXP (x, 1) = tem;
2760           return simplify_binary_operation (code, mode,
2761                                             XEXP (x, 0), XEXP (x, 1));
2762         }
2763
2764     case '2':
2765       return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
2766
2767     case '3':
2768     case 'b':
2769       return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
2770                                          XEXP (x, 0), XEXP (x, 1),
2771                                          XEXP (x, 2));
2772
2773     case '<':
2774       return simplify_relational_operation (code,
2775                                             ((GET_MODE (XEXP (x, 0))
2776                                               != VOIDmode)
2777                                              ? GET_MODE (XEXP (x, 0))
2778                                              : GET_MODE (XEXP (x, 1))),
2779                                             XEXP (x, 0), XEXP (x, 1));
2780     case 'x':
2781       /* The only case we try to handle is a SUBREG.  */
2782       if (code == SUBREG)
2783         return simplify_gen_subreg (mode, SUBREG_REG (x),
2784                                     GET_MODE (SUBREG_REG (x)),
2785                                     SUBREG_BYTE (x));
2786       return NULL;
2787     default:
2788       return NULL;
2789     }
2790 }