OSDN Git Service

* simplify-rtx.c (mode_signbit_p): New function to check whether
[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, 2002, 2003, 2004 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 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "tm_p.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "flags.h"
33 #include "real.h"
34 #include "insn-config.h"
35 #include "recog.h"
36 #include "function.h"
37 #include "expr.h"
38 #include "toplev.h"
39 #include "output.h"
40 #include "ggc.h"
41 #include "target.h"
42
43 /* Simplification and canonicalization of RTL.  */
44
45 /* Much code operates on (low, high) pairs; the low value is an
46    unsigned wide int, the high value a signed wide int.  We
47    occasionally need to sign extend from low to high as if low were a
48    signed wide int.  */
49 #define HWI_SIGN_EXTEND(low) \
50  ((((HOST_WIDE_INT) low) < 0) ? ((HOST_WIDE_INT) -1) : ((HOST_WIDE_INT) 0))
51
52 static rtx neg_const_int (enum machine_mode, rtx);
53 static bool mode_signbit_p (enum machine_mode, rtx);
54 static int simplify_plus_minus_op_data_cmp (const void *, const void *);
55 static rtx simplify_plus_minus (enum rtx_code, enum machine_mode, rtx,
56                                 rtx, int);
57 static rtx simplify_immed_subreg (enum machine_mode, rtx, enum machine_mode,
58                                   unsigned int);
59 static rtx simplify_associative_operation (enum rtx_code, enum machine_mode,
60                                            rtx, rtx);
61 \f
62 /* Negate a CONST_INT rtx, truncating (because a conversion from a
63    maximally negative number can overflow).  */
64 static rtx
65 neg_const_int (enum machine_mode mode, rtx i)
66 {
67   return gen_int_mode (- INTVAL (i), mode);
68 }
69
70 /* Test whether expression, X, is an immediate constant that represents
71    the most significant bit of machine mode MODE.  */
72
73 static bool
74 mode_signbit_p (enum machine_mode mode, rtx x)
75 {
76   unsigned HOST_WIDE_INT val;
77   unsigned int width;
78
79   if (GET_MODE_CLASS (mode) != MODE_INT)
80     return false;
81
82   width = GET_MODE_BITSIZE (mode);
83   if (width == 0)
84     return false;
85   
86   if (width <= HOST_BITS_PER_WIDE_INT
87       && GET_CODE (x) == CONST_INT)
88     val = INTVAL (x);
89   else if (width <= 2 * HOST_BITS_PER_WIDE_INT
90            && GET_CODE (x) == CONST_DOUBLE
91            && CONST_DOUBLE_LOW (x) == 0)
92     {
93       val = CONST_DOUBLE_HIGH (x);
94       width -= HOST_BITS_PER_WIDE_INT;
95     }
96   else
97     return false;
98
99   if (width < HOST_BITS_PER_WIDE_INT)
100     val &= ((unsigned HOST_WIDE_INT) 1 << width) - 1;
101   return val == ((unsigned HOST_WIDE_INT) 1 << (width - 1));
102 }
103 \f
104 /* Make a binary operation by properly ordering the operands and
105    seeing if the expression folds.  */
106
107 rtx
108 simplify_gen_binary (enum rtx_code code, enum machine_mode mode, rtx op0,
109                      rtx op1)
110 {
111   rtx tem;
112
113   /* Put complex operands first and constants second if commutative.  */
114   if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
115       && swap_commutative_operands_p (op0, op1))
116     tem = op0, op0 = op1, op1 = tem;
117
118   /* If this simplifies, do it.  */
119   tem = simplify_binary_operation (code, mode, op0, op1);
120   if (tem)
121     return tem;
122
123   /* Handle addition and subtraction specially.  Otherwise, just form
124      the operation.  */
125
126   if (code == PLUS || code == MINUS)
127     {
128       tem = simplify_plus_minus (code, mode, op0, op1, 1);
129       if (tem)
130         return tem;
131     }
132
133   return gen_rtx_fmt_ee (code, mode, op0, op1);
134 }
135 \f
136 /* If X is a MEM referencing the constant pool, return the real value.
137    Otherwise return X.  */
138 rtx
139 avoid_constant_pool_reference (rtx x)
140 {
141   rtx c, tmp, addr;
142   enum machine_mode cmode;
143
144   switch (GET_CODE (x))
145     {
146     case MEM:
147       break;
148
149     case FLOAT_EXTEND:
150       /* Handle float extensions of constant pool references.  */
151       tmp = XEXP (x, 0);
152       c = avoid_constant_pool_reference (tmp);
153       if (c != tmp && GET_CODE (c) == CONST_DOUBLE)
154         {
155           REAL_VALUE_TYPE d;
156
157           REAL_VALUE_FROM_CONST_DOUBLE (d, c);
158           return CONST_DOUBLE_FROM_REAL_VALUE (d, GET_MODE (x));
159         }
160       return x;
161
162     default:
163       return x;
164     }
165
166   addr = XEXP (x, 0);
167
168   /* Call target hook to avoid the effects of -fpic etc....  */
169   addr = targetm.delegitimize_address (addr);
170
171   if (GET_CODE (addr) == LO_SUM)
172     addr = XEXP (addr, 1);
173
174   if (GET_CODE (addr) != SYMBOL_REF
175       || ! CONSTANT_POOL_ADDRESS_P (addr))
176     return x;
177
178   c = get_pool_constant (addr);
179   cmode = get_pool_mode (addr);
180
181   /* If we're accessing the constant in a different mode than it was
182      originally stored, attempt to fix that up via subreg simplifications.
183      If that fails we have no choice but to return the original memory.  */
184   if (cmode != GET_MODE (x))
185     {
186       c = simplify_subreg (GET_MODE (x), c, cmode, 0);
187       return c ? c : x;
188     }
189
190   return c;
191 }
192 \f
193 /* Make a unary operation by first seeing if it folds and otherwise making
194    the specified operation.  */
195
196 rtx
197 simplify_gen_unary (enum rtx_code code, enum machine_mode mode, rtx op,
198                     enum machine_mode op_mode)
199 {
200   rtx tem;
201
202   /* If this simplifies, use it.  */
203   if ((tem = simplify_unary_operation (code, mode, op, op_mode)) != 0)
204     return tem;
205
206   return gen_rtx_fmt_e (code, mode, op);
207 }
208
209 /* Likewise for ternary operations.  */
210
211 rtx
212 simplify_gen_ternary (enum rtx_code code, enum machine_mode mode,
213                       enum machine_mode op0_mode, rtx op0, rtx op1, rtx op2)
214 {
215   rtx tem;
216
217   /* If this simplifies, use it.  */
218   if (0 != (tem = simplify_ternary_operation (code, mode, op0_mode,
219                                               op0, op1, op2)))
220     return tem;
221
222   return gen_rtx_fmt_eee (code, mode, op0, op1, op2);
223 }
224 \f
225 /* Likewise, for relational operations.
226    CMP_MODE specifies mode comparison is done in.
227   */
228
229 rtx
230 simplify_gen_relational (enum rtx_code code, enum machine_mode mode,
231                          enum machine_mode cmp_mode, rtx op0, rtx op1)
232 {
233   rtx tem;
234
235   if (cmp_mode == VOIDmode)
236     cmp_mode = GET_MODE (op0);
237   if (cmp_mode == VOIDmode)
238     cmp_mode = GET_MODE (op1);
239
240   if (cmp_mode != VOIDmode)
241     {
242       tem = simplify_relational_operation (code, mode, cmp_mode, op0, op1);
243       if (tem)
244         return tem;
245     }
246
247   /* For the following tests, ensure const0_rtx is op1.  */
248   if (swap_commutative_operands_p (op0, op1)
249       || (op0 == const0_rtx && op1 != const0_rtx))
250     tem = op0, op0 = op1, op1 = tem, code = swap_condition (code);
251
252   /* If op0 is a compare, extract the comparison arguments from it.  */
253   if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
254     return simplify_gen_relational (code, mode, VOIDmode,
255                                     XEXP (op0, 0), XEXP (op0, 1));
256
257   /* If op0 is a comparison, extract the comparison arguments form it.  */
258   if (COMPARISON_P (op0) && op1 == const0_rtx)
259     {
260       if (code == NE)
261         {
262           if (GET_MODE (op0) == mode)
263             return op0;
264           return simplify_gen_relational (GET_CODE (op0), mode, VOIDmode,
265                                           XEXP (op0, 0), XEXP (op0, 1));
266         }
267       else if (code == EQ)
268         {
269           enum rtx_code new = reversed_comparison_code (op0, NULL_RTX);
270           if (new != UNKNOWN)
271             return simplify_gen_relational (new, mode, VOIDmode,
272                                             XEXP (op0, 0), XEXP (op0, 1));
273         }
274     }
275
276   return gen_rtx_fmt_ee (code, mode, op0, op1);
277 }
278 \f
279 /* Replace all occurrences of OLD in X with NEW and try to simplify the
280    resulting RTX.  Return a new RTX which is as simplified as possible.  */
281
282 rtx
283 simplify_replace_rtx (rtx x, rtx old, rtx new)
284 {
285   enum rtx_code code = GET_CODE (x);
286   enum machine_mode mode = GET_MODE (x);
287   enum machine_mode op_mode;
288   rtx op0, op1, op2;
289
290   /* If X is OLD, return NEW.  Otherwise, if this is an expression, try
291      to build a new expression substituting recursively.  If we can't do
292      anything, return our input.  */
293
294   if (x == old)
295     return new;
296
297   switch (GET_RTX_CLASS (code))
298     {
299     case RTX_UNARY:
300       op0 = XEXP (x, 0);
301       op_mode = GET_MODE (op0);
302       op0 = simplify_replace_rtx (op0, old, new);
303       if (op0 == XEXP (x, 0))
304         return x;
305       return simplify_gen_unary (code, mode, op0, op_mode);
306
307     case RTX_BIN_ARITH:
308     case RTX_COMM_ARITH:
309       op0 = simplify_replace_rtx (XEXP (x, 0), old, new);
310       op1 = simplify_replace_rtx (XEXP (x, 1), old, new);
311       if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
312         return x;
313       return simplify_gen_binary (code, mode, op0, op1);
314
315     case RTX_COMPARE:
316     case RTX_COMM_COMPARE:
317       op0 = XEXP (x, 0);
318       op1 = XEXP (x, 1);
319       op_mode = GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
320       op0 = simplify_replace_rtx (op0, old, new);
321       op1 = simplify_replace_rtx (op1, old, new);
322       if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
323         return x;
324       return simplify_gen_relational (code, mode, op_mode, op0, op1);
325
326     case RTX_TERNARY:
327     case RTX_BITFIELD_OPS:
328       op0 = XEXP (x, 0);
329       op_mode = GET_MODE (op0);
330       op0 = simplify_replace_rtx (op0, old, new);
331       op1 = simplify_replace_rtx (XEXP (x, 1), old, new);
332       op2 = simplify_replace_rtx (XEXP (x, 2), old, new);
333       if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1) && op2 == XEXP (x, 2))
334         return x;
335       if (op_mode == VOIDmode)
336         op_mode = GET_MODE (op0);
337       return simplify_gen_ternary (code, mode, op_mode, op0, op1, op2);
338
339     case RTX_EXTRA:
340       /* The only case we try to handle is a SUBREG.  */
341       if (code == SUBREG)
342         {
343           op0 = simplify_replace_rtx (SUBREG_REG (x), old, new);
344           if (op0 == SUBREG_REG (x))
345             return x;
346           op0 = simplify_gen_subreg (GET_MODE (x), op0,
347                                      GET_MODE (SUBREG_REG (x)),
348                                      SUBREG_BYTE (x));
349           return op0 ? op0 : x;
350         }
351       break;
352
353     case RTX_OBJ:
354       if (code == MEM)
355         {
356           op0 = simplify_replace_rtx (XEXP (x, 0), old, new);
357           if (op0 == XEXP (x, 0))
358             return x;
359           return replace_equiv_address_nv (x, op0);
360         }
361       else if (code == LO_SUM)
362         {
363           op0 = simplify_replace_rtx (XEXP (x, 0), old, new);
364           op1 = simplify_replace_rtx (XEXP (x, 1), old, new);
365
366           /* (lo_sum (high x) x) -> x  */
367           if (GET_CODE (op0) == HIGH && rtx_equal_p (XEXP (op0, 0), op1))
368             return op1;
369
370           if (op0 == XEXP (x, 0) && op1 == XEXP (x, 1))
371             return x;
372           return gen_rtx_LO_SUM (mode, op0, op1);
373         }
374       else if (code == REG)
375         {
376           if (REG_P (old) && REGNO (x) == REGNO (old))
377             return new;
378         }
379       break;
380
381     default:
382       break;
383     }
384   return x;
385 }
386 \f
387 /* Try to simplify a unary operation CODE whose output mode is to be
388    MODE with input operand OP whose mode was originally OP_MODE.
389    Return zero if no simplification can be made.  */
390 rtx
391 simplify_unary_operation (enum rtx_code code, enum machine_mode mode,
392                           rtx op, enum machine_mode op_mode)
393 {
394   unsigned int width = GET_MODE_BITSIZE (mode);
395   rtx trueop = avoid_constant_pool_reference (op);
396
397   if (code == VEC_DUPLICATE)
398     {
399       if (!VECTOR_MODE_P (mode))
400         abort ();
401       if (GET_MODE (trueop) != VOIDmode
402           && !VECTOR_MODE_P (GET_MODE (trueop))
403           && GET_MODE_INNER (mode) != GET_MODE (trueop))
404         abort ();
405       if (GET_MODE (trueop) != VOIDmode
406           && VECTOR_MODE_P (GET_MODE (trueop))
407           && GET_MODE_INNER (mode) != GET_MODE_INNER (GET_MODE (trueop)))
408         abort ();
409       if (GET_CODE (trueop) == CONST_INT || GET_CODE (trueop) == CONST_DOUBLE
410           || GET_CODE (trueop) == CONST_VECTOR)
411         {
412           int elt_size = GET_MODE_SIZE (GET_MODE_INNER (mode));
413           unsigned n_elts = (GET_MODE_SIZE (mode) / elt_size);
414           rtvec v = rtvec_alloc (n_elts);
415           unsigned int i;
416
417           if (GET_CODE (trueop) != CONST_VECTOR)
418             for (i = 0; i < n_elts; i++)
419               RTVEC_ELT (v, i) = trueop;
420           else
421             {
422               enum machine_mode inmode = GET_MODE (trueop);
423               int in_elt_size = GET_MODE_SIZE (GET_MODE_INNER (inmode));
424               unsigned in_n_elts = (GET_MODE_SIZE (inmode) / in_elt_size);
425
426               if (in_n_elts >= n_elts || n_elts % in_n_elts)
427                 abort ();
428               for (i = 0; i < n_elts; i++)
429                 RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop, i % in_n_elts);
430             }
431           return gen_rtx_CONST_VECTOR (mode, v);
432         }
433     }
434   else if (GET_CODE (op) == CONST)
435     return simplify_unary_operation (code, mode, XEXP (op, 0), op_mode);
436
437   if (VECTOR_MODE_P (mode) && GET_CODE (trueop) == CONST_VECTOR)
438     {
439       int elt_size = GET_MODE_SIZE (GET_MODE_INNER (mode));
440       unsigned n_elts = (GET_MODE_SIZE (mode) / elt_size);
441       enum machine_mode opmode = GET_MODE (trueop);
442       int op_elt_size = GET_MODE_SIZE (GET_MODE_INNER (opmode));
443       unsigned op_n_elts = (GET_MODE_SIZE (opmode) / op_elt_size);
444       rtvec v = rtvec_alloc (n_elts);
445       unsigned int i;
446
447       if (op_n_elts != n_elts)
448         abort ();
449
450       for (i = 0; i < n_elts; i++)
451         {
452           rtx x = simplify_unary_operation (code, GET_MODE_INNER (mode),
453                                             CONST_VECTOR_ELT (trueop, i),
454                                             GET_MODE_INNER (opmode));
455           if (!x)
456             return 0;
457           RTVEC_ELT (v, i) = x;
458         }
459       return gen_rtx_CONST_VECTOR (mode, v);
460     }
461
462   /* The order of these tests is critical so that, for example, we don't
463      check the wrong mode (input vs. output) for a conversion operation,
464      such as FIX.  At some point, this should be simplified.  */
465
466   if (code == FLOAT && GET_MODE (trueop) == VOIDmode
467       && (GET_CODE (trueop) == CONST_DOUBLE || GET_CODE (trueop) == CONST_INT))
468     {
469       HOST_WIDE_INT hv, lv;
470       REAL_VALUE_TYPE d;
471
472       if (GET_CODE (trueop) == CONST_INT)
473         lv = INTVAL (trueop), hv = HWI_SIGN_EXTEND (lv);
474       else
475         lv = CONST_DOUBLE_LOW (trueop),  hv = CONST_DOUBLE_HIGH (trueop);
476
477       REAL_VALUE_FROM_INT (d, lv, hv, mode);
478       d = real_value_truncate (mode, d);
479       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
480     }
481   else if (code == UNSIGNED_FLOAT && GET_MODE (trueop) == VOIDmode
482            && (GET_CODE (trueop) == CONST_DOUBLE
483                || GET_CODE (trueop) == CONST_INT))
484     {
485       HOST_WIDE_INT hv, lv;
486       REAL_VALUE_TYPE d;
487
488       if (GET_CODE (trueop) == CONST_INT)
489         lv = INTVAL (trueop), hv = HWI_SIGN_EXTEND (lv);
490       else
491         lv = CONST_DOUBLE_LOW (trueop),  hv = CONST_DOUBLE_HIGH (trueop);
492
493       if (op_mode == VOIDmode)
494         {
495           /* We don't know how to interpret negative-looking numbers in
496              this case, so don't try to fold those.  */
497           if (hv < 0)
498             return 0;
499         }
500       else if (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT * 2)
501         ;
502       else
503         hv = 0, lv &= GET_MODE_MASK (op_mode);
504
505       REAL_VALUE_FROM_UNSIGNED_INT (d, lv, hv, mode);
506       d = real_value_truncate (mode, d);
507       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
508     }
509
510   if (GET_CODE (trueop) == CONST_INT
511       && width <= HOST_BITS_PER_WIDE_INT && width > 0)
512     {
513       HOST_WIDE_INT arg0 = INTVAL (trueop);
514       HOST_WIDE_INT val;
515
516       switch (code)
517         {
518         case NOT:
519           val = ~ arg0;
520           break;
521
522         case NEG:
523           val = - arg0;
524           break;
525
526         case ABS:
527           val = (arg0 >= 0 ? arg0 : - arg0);
528           break;
529
530         case FFS:
531           /* Don't use ffs here.  Instead, get low order bit and then its
532              number.  If arg0 is zero, this will return 0, as desired.  */
533           arg0 &= GET_MODE_MASK (mode);
534           val = exact_log2 (arg0 & (- arg0)) + 1;
535           break;
536
537         case CLZ:
538           arg0 &= GET_MODE_MASK (mode);
539           if (arg0 == 0 && CLZ_DEFINED_VALUE_AT_ZERO (mode, val))
540             ;
541           else
542             val = GET_MODE_BITSIZE (mode) - floor_log2 (arg0) - 1;
543           break;
544
545         case CTZ:
546           arg0 &= GET_MODE_MASK (mode);
547           if (arg0 == 0)
548             {
549               /* Even if the value at zero is undefined, we have to come
550                  up with some replacement.  Seems good enough.  */
551               if (! CTZ_DEFINED_VALUE_AT_ZERO (mode, val))
552                 val = GET_MODE_BITSIZE (mode);
553             }
554           else
555             val = exact_log2 (arg0 & -arg0);
556           break;
557
558         case POPCOUNT:
559           arg0 &= GET_MODE_MASK (mode);
560           val = 0;
561           while (arg0)
562             val++, arg0 &= arg0 - 1;
563           break;
564
565         case PARITY:
566           arg0 &= GET_MODE_MASK (mode);
567           val = 0;
568           while (arg0)
569             val++, arg0 &= arg0 - 1;
570           val &= 1;
571           break;
572
573         case TRUNCATE:
574           val = arg0;
575           break;
576
577         case ZERO_EXTEND:
578           /* When zero-extending a CONST_INT, we need to know its
579              original mode.  */
580           if (op_mode == VOIDmode)
581             abort ();
582           if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
583             {
584               /* If we were really extending the mode,
585                  we would have to distinguish between zero-extension
586                  and sign-extension.  */
587               if (width != GET_MODE_BITSIZE (op_mode))
588                 abort ();
589               val = arg0;
590             }
591           else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
592             val = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
593           else
594             return 0;
595           break;
596
597         case SIGN_EXTEND:
598           if (op_mode == VOIDmode)
599             op_mode = mode;
600           if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
601             {
602               /* If we were really extending the mode,
603                  we would have to distinguish between zero-extension
604                  and sign-extension.  */
605               if (width != GET_MODE_BITSIZE (op_mode))
606                 abort ();
607               val = arg0;
608             }
609           else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
610             {
611               val
612                 = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
613               if (val
614                   & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (op_mode) - 1)))
615                 val -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
616             }
617           else
618             return 0;
619           break;
620
621         case SQRT:
622         case FLOAT_EXTEND:
623         case FLOAT_TRUNCATE:
624         case SS_TRUNCATE:
625         case US_TRUNCATE:
626           return 0;
627
628         default:
629           abort ();
630         }
631
632       val = trunc_int_for_mode (val, mode);
633
634       return GEN_INT (val);
635     }
636
637   /* We can do some operations on integer CONST_DOUBLEs.  Also allow
638      for a DImode operation on a CONST_INT.  */
639   else if (GET_MODE (trueop) == VOIDmode
640            && width <= HOST_BITS_PER_WIDE_INT * 2
641            && (GET_CODE (trueop) == CONST_DOUBLE
642                || GET_CODE (trueop) == CONST_INT))
643     {
644       unsigned HOST_WIDE_INT l1, lv;
645       HOST_WIDE_INT h1, hv;
646
647       if (GET_CODE (trueop) == CONST_DOUBLE)
648         l1 = CONST_DOUBLE_LOW (trueop), h1 = CONST_DOUBLE_HIGH (trueop);
649       else
650         l1 = INTVAL (trueop), h1 = HWI_SIGN_EXTEND (l1);
651
652       switch (code)
653         {
654         case NOT:
655           lv = ~ l1;
656           hv = ~ h1;
657           break;
658
659         case NEG:
660           neg_double (l1, h1, &lv, &hv);
661           break;
662
663         case ABS:
664           if (h1 < 0)
665             neg_double (l1, h1, &lv, &hv);
666           else
667             lv = l1, hv = h1;
668           break;
669
670         case FFS:
671           hv = 0;
672           if (l1 == 0)
673             {
674               if (h1 == 0)
675                 lv = 0;
676               else
677                 lv = HOST_BITS_PER_WIDE_INT + exact_log2 (h1 & -h1) + 1;
678             }
679           else
680             lv = exact_log2 (l1 & -l1) + 1;
681           break;
682
683         case CLZ:
684           hv = 0;
685           if (h1 != 0)
686             lv = GET_MODE_BITSIZE (mode) - floor_log2 (h1) - 1
687               - HOST_BITS_PER_WIDE_INT;
688           else if (l1 != 0)
689             lv = GET_MODE_BITSIZE (mode) - floor_log2 (l1) - 1;
690           else if (! CLZ_DEFINED_VALUE_AT_ZERO (mode, lv))
691             lv = GET_MODE_BITSIZE (mode);
692           break;
693
694         case CTZ:
695           hv = 0;
696           if (l1 != 0)
697             lv = exact_log2 (l1 & -l1);
698           else if (h1 != 0)
699             lv = HOST_BITS_PER_WIDE_INT + exact_log2 (h1 & -h1);
700           else if (! CTZ_DEFINED_VALUE_AT_ZERO (mode, lv))
701             lv = GET_MODE_BITSIZE (mode);
702           break;
703
704         case POPCOUNT:
705           hv = 0;
706           lv = 0;
707           while (l1)
708             lv++, l1 &= l1 - 1;
709           while (h1)
710             lv++, h1 &= h1 - 1;
711           break;
712
713         case PARITY:
714           hv = 0;
715           lv = 0;
716           while (l1)
717             lv++, l1 &= l1 - 1;
718           while (h1)
719             lv++, h1 &= h1 - 1;
720           lv &= 1;
721           break;
722
723         case TRUNCATE:
724           /* This is just a change-of-mode, so do nothing.  */
725           lv = l1, hv = h1;
726           break;
727
728         case ZERO_EXTEND:
729           if (op_mode == VOIDmode)
730             abort ();
731
732           if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
733             return 0;
734
735           hv = 0;
736           lv = l1 & GET_MODE_MASK (op_mode);
737           break;
738
739         case SIGN_EXTEND:
740           if (op_mode == VOIDmode
741               || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
742             return 0;
743           else
744             {
745               lv = l1 & GET_MODE_MASK (op_mode);
746               if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
747                   && (lv & ((HOST_WIDE_INT) 1
748                             << (GET_MODE_BITSIZE (op_mode) - 1))) != 0)
749                 lv -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
750
751               hv = HWI_SIGN_EXTEND (lv);
752             }
753           break;
754
755         case SQRT:
756           return 0;
757
758         default:
759           return 0;
760         }
761
762       return immed_double_const (lv, hv, mode);
763     }
764
765   else if (GET_CODE (trueop) == CONST_DOUBLE
766            && GET_MODE_CLASS (mode) == MODE_FLOAT)
767     {
768       REAL_VALUE_TYPE d, t;
769       REAL_VALUE_FROM_CONST_DOUBLE (d, trueop);
770
771       switch (code)
772         {
773         case SQRT:
774           if (HONOR_SNANS (mode) && real_isnan (&d))
775             return 0;
776           real_sqrt (&t, mode, &d);
777           d = t;
778           break;
779         case ABS:
780           d = REAL_VALUE_ABS (d);
781           break;
782         case NEG:
783           d = REAL_VALUE_NEGATE (d);
784           break;
785         case FLOAT_TRUNCATE:
786           d = real_value_truncate (mode, d);
787           break;
788         case FLOAT_EXTEND:
789           /* All this does is change the mode.  */
790           break;
791         case FIX:
792           real_arithmetic (&d, FIX_TRUNC_EXPR, &d, NULL);
793           break;
794         case NOT:
795           {
796             long tmp[4];
797             int i;
798
799             real_to_target (tmp, &d, GET_MODE (trueop));
800             for (i = 0; i < 4; i++)
801               tmp[i] = ~tmp[i];
802             real_from_target (&d, tmp, mode);
803           }
804         default:
805           abort ();
806         }
807       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
808     }
809
810   else if (GET_CODE (trueop) == CONST_DOUBLE
811            && GET_MODE_CLASS (GET_MODE (trueop)) == MODE_FLOAT
812            && GET_MODE_CLASS (mode) == MODE_INT
813            && width <= 2*HOST_BITS_PER_WIDE_INT && width > 0)
814     {
815       /* Although the overflow semantics of RTL's FIX and UNSIGNED_FIX
816          operators are intentionally left unspecified (to ease implementation
817          by target backends), for consistency, this routine implements the
818          same semantics for constant folding as used by the middle-end.  */
819
820       HOST_WIDE_INT xh, xl, th, tl;
821       REAL_VALUE_TYPE x, t;
822       REAL_VALUE_FROM_CONST_DOUBLE (x, trueop);
823       switch (code)
824         {
825         case FIX:
826           if (REAL_VALUE_ISNAN (x))
827             return const0_rtx;
828
829           /* Test against the signed upper bound.  */
830           if (width > HOST_BITS_PER_WIDE_INT)
831             {
832               th = ((unsigned HOST_WIDE_INT) 1
833                     << (width - HOST_BITS_PER_WIDE_INT - 1)) - 1;
834               tl = -1;
835             }
836           else
837             {
838               th = 0;
839               tl = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
840             }
841           real_from_integer (&t, VOIDmode, tl, th, 0);
842           if (REAL_VALUES_LESS (t, x))
843             {
844               xh = th;
845               xl = tl;
846               break;
847             }
848
849           /* Test against the signed lower bound.  */
850           if (width > HOST_BITS_PER_WIDE_INT)
851             {
852               th = (HOST_WIDE_INT) -1 << (width - HOST_BITS_PER_WIDE_INT - 1);
853               tl = 0;
854             }
855           else
856             {
857               th = -1;
858               tl = (HOST_WIDE_INT) -1 << (width - 1);
859             }
860           real_from_integer (&t, VOIDmode, tl, th, 0);
861           if (REAL_VALUES_LESS (x, t))
862             {
863               xh = th;
864               xl = tl;
865               break;
866             }
867           REAL_VALUE_TO_INT (&xl, &xh, x);
868           break;
869
870         case UNSIGNED_FIX:
871           if (REAL_VALUE_ISNAN (x) || REAL_VALUE_NEGATIVE (x))
872             return const0_rtx;
873
874           /* Test against the unsigned upper bound.  */
875           if (width == 2*HOST_BITS_PER_WIDE_INT)
876             {
877               th = -1;
878               tl = -1;
879             }
880           else if (width >= HOST_BITS_PER_WIDE_INT)
881             {
882               th = ((unsigned HOST_WIDE_INT) 1
883                     << (width - HOST_BITS_PER_WIDE_INT)) - 1;
884               tl = -1;
885             }
886           else
887             {
888               th = 0;
889               tl = ((unsigned HOST_WIDE_INT) 1 << width) - 1;
890             }
891           real_from_integer (&t, VOIDmode, tl, th, 1);
892           if (REAL_VALUES_LESS (t, x))
893             {
894               xh = th;
895               xl = tl;
896               break;
897             }
898
899           REAL_VALUE_TO_INT (&xl, &xh, x);
900           break;
901
902         default:
903           abort ();
904         }
905       return immed_double_const (xl, xh, mode);
906     }
907
908   /* This was formerly used only for non-IEEE float.
909      eggert@twinsun.com says it is safe for IEEE also.  */
910   else
911     {
912       enum rtx_code reversed;
913       rtx temp;
914
915       /* There are some simplifications we can do even if the operands
916          aren't constant.  */
917       switch (code)
918         {
919         case NOT:
920           /* (not (not X)) == X.  */
921           if (GET_CODE (op) == NOT)
922             return XEXP (op, 0);
923
924           /* (not (eq X Y)) == (ne X Y), etc.  */
925           if (COMPARISON_P (op)
926               && (mode == BImode || STORE_FLAG_VALUE == -1)
927               && ((reversed = reversed_comparison_code (op, NULL_RTX))
928                   != UNKNOWN))
929             return simplify_gen_relational (reversed, mode, VOIDmode,
930                                             XEXP (op, 0), XEXP (op, 1));
931
932           /* (not (plus X -1)) can become (neg X).  */
933           if (GET_CODE (op) == PLUS
934               && XEXP (op, 1) == constm1_rtx)
935             return simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
936
937           /* Similarly, (not (neg X)) is (plus X -1).  */
938           if (GET_CODE (op) == NEG)
939             return plus_constant (XEXP (op, 0), -1);
940
941           /* (not (xor X C)) for C constant is (xor X D) with D = ~C.  */
942           if (GET_CODE (op) == XOR
943               && GET_CODE (XEXP (op, 1)) == CONST_INT
944               && (temp = simplify_unary_operation (NOT, mode,
945                                                    XEXP (op, 1),
946                                                    mode)) != 0)
947             return simplify_gen_binary (XOR, mode, XEXP (op, 0), temp);
948
949           /* (not (plus X C)) for signbit C is (xor X D) with D = ~C.  */
950           if (GET_CODE (op) == PLUS
951               && GET_CODE (XEXP (op, 1)) == CONST_INT
952               && mode_signbit_p (mode, XEXP (op, 1))
953               && (temp = simplify_unary_operation (NOT, mode,
954                                                    XEXP (op, 1),
955                                                    mode)) != 0)
956             return simplify_gen_binary (XOR, mode, XEXP (op, 0), temp);
957
958
959
960           /* (not (ashift 1 X)) is (rotate ~1 X).  We used to do this for
961              operands other than 1, but that is not valid.  We could do a
962              similar simplification for (not (lshiftrt C X)) where C is
963              just the sign bit, but this doesn't seem common enough to
964              bother with.  */
965           if (GET_CODE (op) == ASHIFT
966               && XEXP (op, 0) == const1_rtx)
967             {
968               temp = simplify_gen_unary (NOT, mode, const1_rtx, mode);
969               return simplify_gen_binary (ROTATE, mode, temp, XEXP (op, 1));
970             }
971
972           /* If STORE_FLAG_VALUE is -1, (not (comparison X Y)) can be done
973              by reversing the comparison code if valid.  */
974           if (STORE_FLAG_VALUE == -1
975               && COMPARISON_P (op)
976               && (reversed = reversed_comparison_code (op, NULL_RTX))
977                  != UNKNOWN)
978             return simplify_gen_relational (reversed, mode, VOIDmode,
979                                             XEXP (op, 0), XEXP (op, 1));
980
981           /* (not (ashiftrt foo C)) where C is the number of bits in FOO
982              minus 1 is (ge foo (const_int 0)) if STORE_FLAG_VALUE is -1,
983              so we can perform the above simplification.  */
984
985           if (STORE_FLAG_VALUE == -1
986               && GET_CODE (op) == ASHIFTRT
987               && GET_CODE (XEXP (op, 1)) == CONST_INT
988               && INTVAL (XEXP (op, 1)) == GET_MODE_BITSIZE (mode) - 1)
989             return simplify_gen_relational (GE, mode, VOIDmode,
990                                             XEXP (op, 0), const0_rtx);
991
992           break;
993
994         case NEG:
995           /* (neg (neg X)) == X.  */
996           if (GET_CODE (op) == NEG)
997             return XEXP (op, 0);
998
999           /* (neg (plus X 1)) can become (not X).  */
1000           if (GET_CODE (op) == PLUS
1001               && XEXP (op, 1) == const1_rtx)
1002             return simplify_gen_unary (NOT, mode, XEXP (op, 0), mode);
1003
1004           /* Similarly, (neg (not X)) is (plus X 1).  */
1005           if (GET_CODE (op) == NOT)
1006             return plus_constant (XEXP (op, 0), 1);
1007
1008           /* (neg (minus X Y)) can become (minus Y X).  This transformation
1009              isn't safe for modes with signed zeros, since if X and Y are
1010              both +0, (minus Y X) is the same as (minus X Y).  If the
1011              rounding mode is towards +infinity (or -infinity) then the two
1012              expressions will be rounded differently.  */
1013           if (GET_CODE (op) == MINUS
1014               && !HONOR_SIGNED_ZEROS (mode)
1015               && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1016             return simplify_gen_binary (MINUS, mode, XEXP (op, 1),
1017                                         XEXP (op, 0));
1018
1019           if (GET_CODE (op) == PLUS
1020               && !HONOR_SIGNED_ZEROS (mode)
1021               && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1022             {
1023               /* (neg (plus A C)) is simplified to (minus -C A).  */
1024               if (GET_CODE (XEXP (op, 1)) == CONST_INT
1025                   || GET_CODE (XEXP (op, 1)) == CONST_DOUBLE)
1026                 {
1027                   temp = simplify_unary_operation (NEG, mode, XEXP (op, 1),
1028                                                    mode);
1029                   if (temp)
1030                     return simplify_gen_binary (MINUS, mode, temp,
1031                                                 XEXP (op, 0));
1032                 }
1033
1034               /* (neg (plus A B)) is canonicalized to (minus (neg A) B).  */
1035               temp = simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
1036               return simplify_gen_binary (MINUS, mode, temp, XEXP (op, 1));
1037             }
1038
1039           /* (neg (mult A B)) becomes (mult (neg A) B).
1040              This works even for floating-point values.  */
1041           if (GET_CODE (op) == MULT
1042               && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1043             {
1044               temp = simplify_gen_unary (NEG, mode, XEXP (op, 0), mode);
1045               return simplify_gen_binary (MULT, mode, temp, XEXP (op, 1));
1046             }
1047
1048           /* NEG commutes with ASHIFT since it is multiplication.  Only do
1049              this if we can then eliminate the NEG (e.g., if the operand
1050              is a constant).  */
1051           if (GET_CODE (op) == ASHIFT)
1052             {
1053               temp = simplify_unary_operation (NEG, mode, XEXP (op, 0),
1054                                                mode);
1055               if (temp)
1056                 return simplify_gen_binary (ASHIFT, mode, temp,
1057                                             XEXP (op, 1));
1058             }
1059
1060           /* (neg (ashiftrt X C)) can be replaced by (lshiftrt X C) when
1061              C is equal to the width of MODE minus 1.  */
1062           if (GET_CODE (op) == ASHIFTRT
1063               && GET_CODE (XEXP (op, 1)) == CONST_INT
1064               && INTVAL (XEXP (op, 1)) == GET_MODE_BITSIZE (mode) - 1)
1065                 return simplify_gen_binary (LSHIFTRT, mode,
1066                                             XEXP (op, 0), XEXP (op, 1));
1067
1068           /* (neg (lshiftrt X C)) can be replaced by (ashiftrt X C) when
1069              C is equal to the width of MODE minus 1.  */
1070           if (GET_CODE (op) == LSHIFTRT
1071               && GET_CODE (XEXP (op, 1)) == CONST_INT
1072               && INTVAL (XEXP (op, 1)) == GET_MODE_BITSIZE (mode) - 1)
1073                 return simplify_gen_binary (ASHIFTRT, mode,
1074                                             XEXP (op, 0), XEXP (op, 1));
1075
1076           break;
1077
1078         case SIGN_EXTEND:
1079           /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
1080              becomes just the MINUS if its mode is MODE.  This allows
1081              folding switch statements on machines using casesi (such as
1082              the VAX).  */
1083           if (GET_CODE (op) == TRUNCATE
1084               && GET_MODE (XEXP (op, 0)) == mode
1085               && GET_CODE (XEXP (op, 0)) == MINUS
1086               && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
1087               && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
1088             return XEXP (op, 0);
1089
1090           /* Check for a sign extension of a subreg of a promoted
1091              variable, where the promotion is sign-extended, and the
1092              target mode is the same as the variable's promotion.  */
1093           if (GET_CODE (op) == SUBREG
1094               && SUBREG_PROMOTED_VAR_P (op)
1095               && ! SUBREG_PROMOTED_UNSIGNED_P (op)
1096               && GET_MODE (XEXP (op, 0)) == mode)
1097             return XEXP (op, 0);
1098
1099 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
1100           if (! POINTERS_EXTEND_UNSIGNED
1101               && mode == Pmode && GET_MODE (op) == ptr_mode
1102               && (CONSTANT_P (op)
1103                   || (GET_CODE (op) == SUBREG
1104                       && GET_CODE (SUBREG_REG (op)) == REG
1105                       && REG_POINTER (SUBREG_REG (op))
1106                       && GET_MODE (SUBREG_REG (op)) == Pmode)))
1107             return convert_memory_address (Pmode, op);
1108 #endif
1109           break;
1110
1111         case ZERO_EXTEND:
1112           /* Check for a zero extension of a subreg of a promoted
1113              variable, where the promotion is zero-extended, and the
1114              target mode is the same as the variable's promotion.  */
1115           if (GET_CODE (op) == SUBREG
1116               && SUBREG_PROMOTED_VAR_P (op)
1117               && SUBREG_PROMOTED_UNSIGNED_P (op)
1118               && GET_MODE (XEXP (op, 0)) == mode)
1119             return XEXP (op, 0);
1120
1121 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
1122           if (POINTERS_EXTEND_UNSIGNED > 0
1123               && mode == Pmode && GET_MODE (op) == ptr_mode
1124               && (CONSTANT_P (op)
1125                   || (GET_CODE (op) == SUBREG
1126                       && GET_CODE (SUBREG_REG (op)) == REG
1127                       && REG_POINTER (SUBREG_REG (op))
1128                       && GET_MODE (SUBREG_REG (op)) == Pmode)))
1129             return convert_memory_address (Pmode, op);
1130 #endif
1131           break;
1132
1133         default:
1134           break;
1135         }
1136
1137       return 0;
1138     }
1139 }
1140 \f
1141 /* Subroutine of simplify_binary_operation to simplify a commutative,
1142    associative binary operation CODE with result mode MODE, operating
1143    on OP0 and OP1.  CODE is currently one of PLUS, MULT, AND, IOR, XOR,
1144    SMIN, SMAX, UMIN or UMAX.  Return zero if no simplification or
1145    canonicalization is possible.  */
1146
1147 static rtx
1148 simplify_associative_operation (enum rtx_code code, enum machine_mode mode,
1149                                 rtx op0, rtx op1)
1150 {
1151   rtx tem;
1152
1153   /* Linearize the operator to the left.  */
1154   if (GET_CODE (op1) == code)
1155     {
1156       /* "(a op b) op (c op d)" becomes "((a op b) op c) op d)".  */
1157       if (GET_CODE (op0) == code)
1158         {
1159           tem = simplify_gen_binary (code, mode, op0, XEXP (op1, 0));
1160           return simplify_gen_binary (code, mode, tem, XEXP (op1, 1));
1161         }
1162
1163       /* "a op (b op c)" becomes "(b op c) op a".  */
1164       if (! swap_commutative_operands_p (op1, op0))
1165         return simplify_gen_binary (code, mode, op1, op0);
1166
1167       tem = op0;
1168       op0 = op1;
1169       op1 = tem;
1170     }
1171
1172   if (GET_CODE (op0) == code)
1173     {
1174       /* Canonicalize "(x op c) op y" as "(x op y) op c".  */
1175       if (swap_commutative_operands_p (XEXP (op0, 1), op1))
1176         {
1177           tem = simplify_gen_binary (code, mode, XEXP (op0, 0), op1);
1178           return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
1179         }
1180
1181       /* Attempt to simplify "(a op b) op c" as "a op (b op c)".  */
1182       tem = swap_commutative_operands_p (XEXP (op0, 1), op1)
1183             ? simplify_binary_operation (code, mode, op1, XEXP (op0, 1))
1184             : simplify_binary_operation (code, mode, XEXP (op0, 1), op1);
1185       if (tem != 0)
1186         return simplify_gen_binary (code, mode, XEXP (op0, 0), tem);
1187
1188       /* Attempt to simplify "(a op b) op c" as "(a op c) op b".  */
1189       tem = swap_commutative_operands_p (XEXP (op0, 0), op1)
1190             ? simplify_binary_operation (code, mode, op1, XEXP (op0, 0))
1191             : simplify_binary_operation (code, mode, XEXP (op0, 0), op1);
1192       if (tem != 0)
1193         return simplify_gen_binary (code, mode, tem, XEXP (op0, 1));
1194     }
1195
1196   return 0;
1197 }
1198
1199 /* Simplify a binary operation CODE with result mode MODE, operating on OP0
1200    and OP1.  Return 0 if no simplification is possible.
1201
1202    Don't use this for relational operations such as EQ or LT.
1203    Use simplify_relational_operation instead.  */
1204
1205 rtx
1206 simplify_binary_operation (enum rtx_code code, enum machine_mode mode,
1207                            rtx op0, rtx op1)
1208 {
1209   HOST_WIDE_INT arg0, arg1, arg0s, arg1s;
1210   HOST_WIDE_INT val;
1211   unsigned int width = GET_MODE_BITSIZE (mode);
1212   rtx trueop0, trueop1;
1213   rtx tem;
1214
1215 #ifdef ENABLE_CHECKING
1216   /* Relational operations don't work here.  We must know the mode
1217      of the operands in order to do the comparison correctly.
1218      Assuming a full word can give incorrect results.
1219      Consider comparing 128 with -128 in QImode.  */
1220
1221   if (GET_RTX_CLASS (code) == RTX_COMPARE
1222       || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
1223     abort ();
1224 #endif
1225
1226   /* Make sure the constant is second.  */
1227   if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
1228       && swap_commutative_operands_p (op0, op1))
1229     {
1230       tem = op0, op0 = op1, op1 = tem;
1231     }
1232
1233   trueop0 = avoid_constant_pool_reference (op0);
1234   trueop1 = avoid_constant_pool_reference (op1);
1235
1236   if (VECTOR_MODE_P (mode)
1237       && GET_CODE (trueop0) == CONST_VECTOR
1238       && GET_CODE (trueop1) == CONST_VECTOR)
1239     {
1240       int elt_size = GET_MODE_SIZE (GET_MODE_INNER (mode));
1241       unsigned n_elts = (GET_MODE_SIZE (mode) / elt_size);
1242       enum machine_mode op0mode = GET_MODE (trueop0);
1243       int op0_elt_size = GET_MODE_SIZE (GET_MODE_INNER (op0mode));
1244       unsigned op0_n_elts = (GET_MODE_SIZE (op0mode) / op0_elt_size);
1245       enum machine_mode op1mode = GET_MODE (trueop1);
1246       int op1_elt_size = GET_MODE_SIZE (GET_MODE_INNER (op1mode));
1247       unsigned op1_n_elts = (GET_MODE_SIZE (op1mode) / op1_elt_size);
1248       rtvec v = rtvec_alloc (n_elts);
1249       unsigned int i;
1250
1251       if (op0_n_elts != n_elts || op1_n_elts != n_elts)
1252         abort ();
1253
1254       for (i = 0; i < n_elts; i++)
1255         {
1256           rtx x = simplify_binary_operation (code, GET_MODE_INNER (mode),
1257                                              CONST_VECTOR_ELT (trueop0, i),
1258                                              CONST_VECTOR_ELT (trueop1, i));
1259           if (!x)
1260             return 0;
1261           RTVEC_ELT (v, i) = x;
1262         }
1263
1264       return gen_rtx_CONST_VECTOR (mode, v);
1265     }
1266
1267   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1268       && GET_CODE (trueop0) == CONST_DOUBLE
1269       && GET_CODE (trueop1) == CONST_DOUBLE
1270       && mode == GET_MODE (op0) && mode == GET_MODE (op1))
1271     {
1272       if (code == AND
1273           || code == IOR
1274           || code == XOR)
1275         {
1276           long tmp0[4];
1277           long tmp1[4];
1278           REAL_VALUE_TYPE r;
1279           int i;
1280
1281           real_to_target (tmp0, CONST_DOUBLE_REAL_VALUE (op0),
1282                           GET_MODE (op0));
1283           real_to_target (tmp1, CONST_DOUBLE_REAL_VALUE (op1),
1284                           GET_MODE (op1));
1285           for (i = 0; i < 4; i++)
1286             {
1287               if (code == AND)
1288                 tmp0[i] &= tmp1[i];
1289               else if (code == IOR)
1290                 tmp0[i] |= tmp1[i];
1291               else if (code == XOR)
1292                 tmp0[i] ^= tmp1[i];
1293               else
1294                 abort ();
1295             }
1296            real_from_target (&r, tmp0, mode);
1297            return CONST_DOUBLE_FROM_REAL_VALUE (r, mode);
1298         }
1299       else
1300         {
1301           REAL_VALUE_TYPE f0, f1, value;
1302
1303           REAL_VALUE_FROM_CONST_DOUBLE (f0, trueop0);
1304           REAL_VALUE_FROM_CONST_DOUBLE (f1, trueop1);
1305           f0 = real_value_truncate (mode, f0);
1306           f1 = real_value_truncate (mode, f1);
1307
1308           if (HONOR_SNANS (mode)
1309               && (REAL_VALUE_ISNAN (f0) || REAL_VALUE_ISNAN (f1)))
1310             return 0;
1311
1312           if (code == DIV
1313               && REAL_VALUES_EQUAL (f1, dconst0)
1314               && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1315             return 0;
1316
1317           REAL_ARITHMETIC (value, rtx_to_tree_code (code), f0, f1);
1318
1319           value = real_value_truncate (mode, value);
1320           return CONST_DOUBLE_FROM_REAL_VALUE (value, mode);
1321         }
1322     }
1323
1324   /* We can fold some multi-word operations.  */
1325   if (GET_MODE_CLASS (mode) == MODE_INT
1326       && width == HOST_BITS_PER_WIDE_INT * 2
1327       && (GET_CODE (trueop0) == CONST_DOUBLE
1328           || GET_CODE (trueop0) == CONST_INT)
1329       && (GET_CODE (trueop1) == CONST_DOUBLE
1330           || GET_CODE (trueop1) == CONST_INT))
1331     {
1332       unsigned HOST_WIDE_INT l1, l2, lv, lt;
1333       HOST_WIDE_INT h1, h2, hv, ht;
1334
1335       if (GET_CODE (trueop0) == CONST_DOUBLE)
1336         l1 = CONST_DOUBLE_LOW (trueop0), h1 = CONST_DOUBLE_HIGH (trueop0);
1337       else
1338         l1 = INTVAL (trueop0), h1 = HWI_SIGN_EXTEND (l1);
1339
1340       if (GET_CODE (trueop1) == CONST_DOUBLE)
1341         l2 = CONST_DOUBLE_LOW (trueop1), h2 = CONST_DOUBLE_HIGH (trueop1);
1342       else
1343         l2 = INTVAL (trueop1), h2 = HWI_SIGN_EXTEND (l2);
1344
1345       switch (code)
1346         {
1347         case MINUS:
1348           /* A - B == A + (-B).  */
1349           neg_double (l2, h2, &lv, &hv);
1350           l2 = lv, h2 = hv;
1351
1352           /* Fall through....  */
1353
1354         case PLUS:
1355           add_double (l1, h1, l2, h2, &lv, &hv);
1356           break;
1357
1358         case MULT:
1359           mul_double (l1, h1, l2, h2, &lv, &hv);
1360           break;
1361
1362         case DIV:
1363           if (div_and_round_double (TRUNC_DIV_EXPR, 0, l1, h1, l2, h2,
1364                                     &lv, &hv, &lt, &ht))
1365             return 0;
1366           break;
1367
1368         case MOD:
1369           if (div_and_round_double (TRUNC_DIV_EXPR, 0, l1, h1, l2, h2,
1370                                     &lt, &ht, &lv, &hv))
1371             return 0;
1372           break;
1373
1374         case UDIV:
1375           if (div_and_round_double (TRUNC_DIV_EXPR, 1, l1, h1, l2, h2,
1376                                     &lv, &hv, &lt, &ht))
1377             return 0;
1378           break;
1379
1380         case UMOD:
1381           if (div_and_round_double (TRUNC_DIV_EXPR, 1, l1, h1, l2, h2,
1382                                     &lt, &ht, &lv, &hv))
1383             return 0;
1384           break;
1385
1386         case AND:
1387           lv = l1 & l2, hv = h1 & h2;
1388           break;
1389
1390         case IOR:
1391           lv = l1 | l2, hv = h1 | h2;
1392           break;
1393
1394         case XOR:
1395           lv = l1 ^ l2, hv = h1 ^ h2;
1396           break;
1397
1398         case SMIN:
1399           if (h1 < h2
1400               || (h1 == h2
1401                   && ((unsigned HOST_WIDE_INT) l1
1402                       < (unsigned HOST_WIDE_INT) l2)))
1403             lv = l1, hv = h1;
1404           else
1405             lv = l2, hv = h2;
1406           break;
1407
1408         case SMAX:
1409           if (h1 > h2
1410               || (h1 == h2
1411                   && ((unsigned HOST_WIDE_INT) l1
1412                       > (unsigned HOST_WIDE_INT) l2)))
1413             lv = l1, hv = h1;
1414           else
1415             lv = l2, hv = h2;
1416           break;
1417
1418         case UMIN:
1419           if ((unsigned HOST_WIDE_INT) h1 < (unsigned HOST_WIDE_INT) h2
1420               || (h1 == h2
1421                   && ((unsigned HOST_WIDE_INT) l1
1422                       < (unsigned HOST_WIDE_INT) l2)))
1423             lv = l1, hv = h1;
1424           else
1425             lv = l2, hv = h2;
1426           break;
1427
1428         case UMAX:
1429           if ((unsigned HOST_WIDE_INT) h1 > (unsigned HOST_WIDE_INT) h2
1430               || (h1 == h2
1431                   && ((unsigned HOST_WIDE_INT) l1
1432                       > (unsigned HOST_WIDE_INT) l2)))
1433             lv = l1, hv = h1;
1434           else
1435             lv = l2, hv = h2;
1436           break;
1437
1438         case LSHIFTRT:   case ASHIFTRT:
1439         case ASHIFT:
1440         case ROTATE:     case ROTATERT:
1441           if (SHIFT_COUNT_TRUNCATED)
1442             l2 &= (GET_MODE_BITSIZE (mode) - 1), h2 = 0;
1443
1444           if (h2 != 0 || l2 >= GET_MODE_BITSIZE (mode))
1445             return 0;
1446
1447           if (code == LSHIFTRT || code == ASHIFTRT)
1448             rshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv,
1449                            code == ASHIFTRT);
1450           else if (code == ASHIFT)
1451             lshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv, 1);
1452           else if (code == ROTATE)
1453             lrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
1454           else /* code == ROTATERT */
1455             rrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
1456           break;
1457
1458         default:
1459           return 0;
1460         }
1461
1462       return immed_double_const (lv, hv, mode);
1463     }
1464
1465   if (GET_CODE (op0) != CONST_INT || GET_CODE (op1) != CONST_INT
1466       || width > HOST_BITS_PER_WIDE_INT || width == 0)
1467     {
1468       /* Even if we can't compute a constant result,
1469          there are some cases worth simplifying.  */
1470
1471       switch (code)
1472         {
1473         case PLUS:
1474           /* Maybe simplify x + 0 to x.  The two expressions are equivalent
1475              when x is NaN, infinite, or finite and nonzero.  They aren't
1476              when x is -0 and the rounding mode is not towards -infinity,
1477              since (-0) + 0 is then 0.  */
1478           if (!HONOR_SIGNED_ZEROS (mode) && trueop1 == CONST0_RTX (mode))
1479             return op0;
1480
1481           /* ((-a) + b) -> (b - a) and similarly for (a + (-b)).  These
1482              transformations are safe even for IEEE.  */
1483           if (GET_CODE (op0) == NEG)
1484             return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
1485           else if (GET_CODE (op1) == NEG)
1486             return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
1487
1488           /* (~a) + 1 -> -a */
1489           if (INTEGRAL_MODE_P (mode)
1490               && GET_CODE (op0) == NOT
1491               && trueop1 == const1_rtx)
1492             return simplify_gen_unary (NEG, mode, XEXP (op0, 0), mode);
1493
1494           /* Handle both-operands-constant cases.  We can only add
1495              CONST_INTs to constants since the sum of relocatable symbols
1496              can't be handled by most assemblers.  Don't add CONST_INT
1497              to CONST_INT since overflow won't be computed properly if wider
1498              than HOST_BITS_PER_WIDE_INT.  */
1499
1500           if (CONSTANT_P (op0) && GET_MODE (op0) != VOIDmode
1501               && GET_CODE (op1) == CONST_INT)
1502             return plus_constant (op0, INTVAL (op1));
1503           else if (CONSTANT_P (op1) && GET_MODE (op1) != VOIDmode
1504                    && GET_CODE (op0) == CONST_INT)
1505             return plus_constant (op1, INTVAL (op0));
1506
1507           /* See if this is something like X * C - X or vice versa or
1508              if the multiplication is written as a shift.  If so, we can
1509              distribute and make a new multiply, shift, or maybe just
1510              have X (if C is 2 in the example above).  But don't make
1511              real multiply if we didn't have one before.  */
1512
1513           if (! FLOAT_MODE_P (mode))
1514             {
1515               HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
1516               rtx lhs = op0, rhs = op1;
1517               int had_mult = 0;
1518
1519               if (GET_CODE (lhs) == NEG)
1520                 coeff0 = -1, lhs = XEXP (lhs, 0);
1521               else if (GET_CODE (lhs) == MULT
1522                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
1523                 {
1524                   coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
1525                   had_mult = 1;
1526                 }
1527               else if (GET_CODE (lhs) == ASHIFT
1528                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT
1529                        && INTVAL (XEXP (lhs, 1)) >= 0
1530                        && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
1531                 {
1532                   coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
1533                   lhs = XEXP (lhs, 0);
1534                 }
1535
1536               if (GET_CODE (rhs) == NEG)
1537                 coeff1 = -1, rhs = XEXP (rhs, 0);
1538               else if (GET_CODE (rhs) == MULT
1539                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
1540                 {
1541                   coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
1542                   had_mult = 1;
1543                 }
1544               else if (GET_CODE (rhs) == ASHIFT
1545                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT
1546                        && INTVAL (XEXP (rhs, 1)) >= 0
1547                        && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
1548                 {
1549                   coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
1550                   rhs = XEXP (rhs, 0);
1551                 }
1552
1553               if (rtx_equal_p (lhs, rhs))
1554                 {
1555                   tem = simplify_gen_binary (MULT, mode, lhs,
1556                                         GEN_INT (coeff0 + coeff1));
1557                   return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
1558                 }
1559             }
1560
1561           /* (plus (xor X C1) C2) is (xor X (C1^C2)) if C2 is signbit.  */
1562           if ((GET_CODE (op1) == CONST_INT
1563                || GET_CODE (op1) == CONST_DOUBLE)
1564               && GET_CODE (op0) == XOR
1565               && (GET_CODE (XEXP (op0, 1)) == CONST_INT
1566                   || GET_CODE (XEXP (op0, 1)) == CONST_DOUBLE)
1567               && mode_signbit_p (mode, op1))
1568             return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
1569                                         simplify_gen_binary (XOR, mode, op1,
1570                                                              XEXP (op0, 1)));
1571
1572           /* If one of the operands is a PLUS or a MINUS, see if we can
1573              simplify this by the associative law.
1574              Don't use the associative law for floating point.
1575              The inaccuracy makes it nonassociative,
1576              and subtle programs can break if operations are associated.  */
1577
1578           if (INTEGRAL_MODE_P (mode)
1579               && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
1580                   || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS
1581                   || (GET_CODE (op0) == CONST
1582                       && GET_CODE (XEXP (op0, 0)) == PLUS)
1583                   || (GET_CODE (op1) == CONST
1584                       && GET_CODE (XEXP (op1, 0)) == PLUS))
1585               && (tem = simplify_plus_minus (code, mode, op0, op1, 0)) != 0)
1586             return tem;
1587
1588           /* Reassociate floating point addition only when the user
1589              specifies unsafe math optimizations.  */
1590           if (FLOAT_MODE_P (mode)
1591               && flag_unsafe_math_optimizations)
1592             {
1593               tem = simplify_associative_operation (code, mode, op0, op1);
1594               if (tem)
1595                 return tem;
1596             }
1597           break;
1598
1599         case COMPARE:
1600 #ifdef HAVE_cc0
1601           /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
1602              using cc0, in which case we want to leave it as a COMPARE
1603              so we can distinguish it from a register-register-copy.
1604
1605              In IEEE floating point, x-0 is not the same as x.  */
1606
1607           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1608                || ! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
1609               && trueop1 == CONST0_RTX (mode))
1610             return op0;
1611 #endif
1612
1613           /* Convert (compare (gt (flags) 0) (lt (flags) 0)) to (flags).  */
1614           if (((GET_CODE (op0) == GT && GET_CODE (op1) == LT)
1615                || (GET_CODE (op0) == GTU && GET_CODE (op1) == LTU))
1616               && XEXP (op0, 1) == const0_rtx && XEXP (op1, 1) == const0_rtx)
1617             {
1618               rtx xop00 = XEXP (op0, 0);
1619               rtx xop10 = XEXP (op1, 0);
1620
1621 #ifdef HAVE_cc0
1622               if (GET_CODE (xop00) == CC0 && GET_CODE (xop10) == CC0)
1623 #else
1624               if (GET_CODE (xop00) == REG && GET_CODE (xop10) == REG
1625                   && GET_MODE (xop00) == GET_MODE (xop10)
1626                   && REGNO (xop00) == REGNO (xop10)
1627                   && GET_MODE_CLASS (GET_MODE (xop00)) == MODE_CC
1628                   && GET_MODE_CLASS (GET_MODE (xop10)) == MODE_CC)
1629 #endif
1630                 return xop00;
1631             }
1632           break;
1633
1634         case MINUS:
1635           /* We can't assume x-x is 0 even with non-IEEE floating point,
1636              but since it is zero except in very strange circumstances, we
1637              will treat it as zero with -funsafe-math-optimizations.  */
1638           if (rtx_equal_p (trueop0, trueop1)
1639               && ! side_effects_p (op0)
1640               && (! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations))
1641             return CONST0_RTX (mode);
1642
1643           /* Change subtraction from zero into negation.  (0 - x) is the
1644              same as -x when x is NaN, infinite, or finite and nonzero.
1645              But if the mode has signed zeros, and does not round towards
1646              -infinity, then 0 - 0 is 0, not -0.  */
1647           if (!HONOR_SIGNED_ZEROS (mode) && trueop0 == CONST0_RTX (mode))
1648             return simplify_gen_unary (NEG, mode, op1, mode);
1649
1650           /* (-1 - a) is ~a.  */
1651           if (trueop0 == constm1_rtx)
1652             return simplify_gen_unary (NOT, mode, op1, mode);
1653
1654           /* Subtracting 0 has no effect unless the mode has signed zeros
1655              and supports rounding towards -infinity.  In such a case,
1656              0 - 0 is -0.  */
1657           if (!(HONOR_SIGNED_ZEROS (mode)
1658                 && HONOR_SIGN_DEPENDENT_ROUNDING (mode))
1659               && trueop1 == CONST0_RTX (mode))
1660             return op0;
1661
1662           /* See if this is something like X * C - X or vice versa or
1663              if the multiplication is written as a shift.  If so, we can
1664              distribute and make a new multiply, shift, or maybe just
1665              have X (if C is 2 in the example above).  But don't make
1666              real multiply if we didn't have one before.  */
1667
1668           if (! FLOAT_MODE_P (mode))
1669             {
1670               HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
1671               rtx lhs = op0, rhs = op1;
1672               int had_mult = 0;
1673
1674               if (GET_CODE (lhs) == NEG)
1675                 coeff0 = -1, lhs = XEXP (lhs, 0);
1676               else if (GET_CODE (lhs) == MULT
1677                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
1678                 {
1679                   coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
1680                   had_mult = 1;
1681                 }
1682               else if (GET_CODE (lhs) == ASHIFT
1683                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT
1684                        && INTVAL (XEXP (lhs, 1)) >= 0
1685                        && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
1686                 {
1687                   coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
1688                   lhs = XEXP (lhs, 0);
1689                 }
1690
1691               if (GET_CODE (rhs) == NEG)
1692                 coeff1 = - 1, rhs = XEXP (rhs, 0);
1693               else if (GET_CODE (rhs) == MULT
1694                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
1695                 {
1696                   coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
1697                   had_mult = 1;
1698                 }
1699               else if (GET_CODE (rhs) == ASHIFT
1700                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT
1701                        && INTVAL (XEXP (rhs, 1)) >= 0
1702                        && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
1703                 {
1704                   coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
1705                   rhs = XEXP (rhs, 0);
1706                 }
1707
1708               if (rtx_equal_p (lhs, rhs))
1709                 {
1710                   tem = simplify_gen_binary (MULT, mode, lhs,
1711                                              GEN_INT (coeff0 - coeff1));
1712                   return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
1713                 }
1714             }
1715
1716           /* (a - (-b)) -> (a + b).  True even for IEEE.  */
1717           if (GET_CODE (op1) == NEG)
1718             return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
1719
1720           /* (-x - c) may be simplified as (-c - x).  */
1721           if (GET_CODE (op0) == NEG
1722               && (GET_CODE (op1) == CONST_INT
1723                   || GET_CODE (op1) == CONST_DOUBLE))
1724             {
1725               tem = simplify_unary_operation (NEG, mode, op1, mode);
1726               if (tem)
1727                 return simplify_gen_binary (MINUS, mode, tem, XEXP (op0, 0));
1728             }
1729
1730           /* If one of the operands is a PLUS or a MINUS, see if we can
1731              simplify this by the associative law.
1732              Don't use the associative law for floating point.
1733              The inaccuracy makes it nonassociative,
1734              and subtle programs can break if operations are associated.  */
1735
1736           if (INTEGRAL_MODE_P (mode)
1737               && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
1738                   || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS
1739                   || (GET_CODE (op0) == CONST
1740                       && GET_CODE (XEXP (op0, 0)) == PLUS)
1741                   || (GET_CODE (op1) == CONST
1742                       && GET_CODE (XEXP (op1, 0)) == PLUS))
1743               && (tem = simplify_plus_minus (code, mode, op0, op1, 0)) != 0)
1744             return tem;
1745
1746           /* Don't let a relocatable value get a negative coeff.  */
1747           if (GET_CODE (op1) == CONST_INT && GET_MODE (op0) != VOIDmode)
1748             return simplify_gen_binary (PLUS, mode,
1749                                         op0,
1750                                         neg_const_int (mode, op1));
1751
1752           /* (x - (x & y)) -> (x & ~y) */
1753           if (GET_CODE (op1) == AND)
1754             {
1755               if (rtx_equal_p (op0, XEXP (op1, 0)))
1756                 {
1757                   tem = simplify_gen_unary (NOT, mode, XEXP (op1, 1),
1758                                             GET_MODE (XEXP (op1, 1)));
1759                   return simplify_gen_binary (AND, mode, op0, tem);
1760                 }
1761               if (rtx_equal_p (op0, XEXP (op1, 1)))
1762                 {
1763                   tem = simplify_gen_unary (NOT, mode, XEXP (op1, 0),
1764                                             GET_MODE (XEXP (op1, 0)));
1765                   return simplify_gen_binary (AND, mode, op0, tem);
1766                 }
1767             }
1768           break;
1769
1770         case MULT:
1771           if (trueop1 == constm1_rtx)
1772             return simplify_gen_unary (NEG, mode, op0, mode);
1773
1774           /* Maybe simplify x * 0 to 0.  The reduction is not valid if
1775              x is NaN, since x * 0 is then also NaN.  Nor is it valid
1776              when the mode has signed zeros, since multiplying a negative
1777              number by 0 will give -0, not 0.  */
1778           if (!HONOR_NANS (mode)
1779               && !HONOR_SIGNED_ZEROS (mode)
1780               && trueop1 == CONST0_RTX (mode)
1781               && ! side_effects_p (op0))
1782             return op1;
1783
1784           /* In IEEE floating point, x*1 is not equivalent to x for
1785              signalling NaNs.  */
1786           if (!HONOR_SNANS (mode)
1787               && trueop1 == CONST1_RTX (mode))
1788             return op0;
1789
1790           /* Convert multiply by constant power of two into shift unless
1791              we are still generating RTL.  This test is a kludge.  */
1792           if (GET_CODE (trueop1) == CONST_INT
1793               && (val = exact_log2 (INTVAL (trueop1))) >= 0
1794               /* If the mode is larger than the host word size, and the
1795                  uppermost bit is set, then this isn't a power of two due
1796                  to implicit sign extension.  */
1797               && (width <= HOST_BITS_PER_WIDE_INT
1798                   || val != HOST_BITS_PER_WIDE_INT - 1)
1799               && ! rtx_equal_function_value_matters)
1800             return simplify_gen_binary (ASHIFT, mode, op0, GEN_INT (val));
1801
1802           /* x*2 is x+x and x*(-1) is -x */
1803           if (GET_CODE (trueop1) == CONST_DOUBLE
1804               && GET_MODE_CLASS (GET_MODE (trueop1)) == MODE_FLOAT
1805               && GET_MODE (op0) == mode)
1806             {
1807               REAL_VALUE_TYPE d;
1808               REAL_VALUE_FROM_CONST_DOUBLE (d, trueop1);
1809
1810               if (REAL_VALUES_EQUAL (d, dconst2))
1811                 return simplify_gen_binary (PLUS, mode, op0, copy_rtx (op0));
1812
1813               if (REAL_VALUES_EQUAL (d, dconstm1))
1814                 return simplify_gen_unary (NEG, mode, op0, mode);
1815             }
1816
1817           /* Reassociate multiplication, but for floating point MULTs
1818              only when the user specifies unsafe math optimizations.  */
1819           if (! FLOAT_MODE_P (mode)
1820               || flag_unsafe_math_optimizations)
1821             {
1822               tem = simplify_associative_operation (code, mode, op0, op1);
1823               if (tem)
1824                 return tem;
1825             }
1826           break;
1827
1828         case IOR:
1829           if (trueop1 == const0_rtx)
1830             return op0;
1831           if (GET_CODE (trueop1) == CONST_INT
1832               && ((INTVAL (trueop1) & GET_MODE_MASK (mode))
1833                   == GET_MODE_MASK (mode)))
1834             return op1;
1835           if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
1836             return op0;
1837           /* A | (~A) -> -1 */
1838           if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
1839                || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
1840               && ! side_effects_p (op0)
1841               && GET_MODE_CLASS (mode) != MODE_CC)
1842             return constm1_rtx;
1843           tem = simplify_associative_operation (code, mode, op0, op1);
1844           if (tem)
1845             return tem;
1846           break;
1847
1848         case XOR:
1849           if (trueop1 == const0_rtx)
1850             return op0;
1851           if (GET_CODE (trueop1) == CONST_INT
1852               && ((INTVAL (trueop1) & GET_MODE_MASK (mode))
1853                   == GET_MODE_MASK (mode)))
1854             return simplify_gen_unary (NOT, mode, op0, mode);
1855           if (trueop0 == trueop1
1856               && ! side_effects_p (op0)
1857               && GET_MODE_CLASS (mode) != MODE_CC)
1858             return const0_rtx;
1859
1860           /* Canonicalize XOR of the most significant bit to PLUS.  */
1861           if ((GET_CODE (op1) == CONST_INT
1862                || GET_CODE (op1) == CONST_DOUBLE)
1863               && mode_signbit_p (mode, op1))
1864             return simplify_gen_binary (PLUS, mode, op0, op1);
1865           /* (xor (plus X C1) C2) is (xor X (C1^C2)) if C1 is signbit.  */
1866           if ((GET_CODE (op1) == CONST_INT
1867                || GET_CODE (op1) == CONST_DOUBLE)
1868               && GET_CODE (op0) == PLUS
1869               && (GET_CODE (XEXP (op0, 1)) == CONST_INT
1870                   || GET_CODE (XEXP (op0, 1)) == CONST_DOUBLE)
1871               && mode_signbit_p (mode, XEXP (op0, 1)))
1872             return simplify_gen_binary (XOR, mode, XEXP (op0, 0),
1873                                         simplify_gen_binary (XOR, mode, op1,
1874                                                              XEXP (op0, 1)));
1875               
1876           tem = simplify_associative_operation (code, mode, op0, op1);
1877           if (tem)
1878             return tem;
1879           break;
1880
1881         case AND:
1882           if (trueop1 == const0_rtx && ! side_effects_p (op0))
1883             return const0_rtx;
1884           if (GET_CODE (trueop1) == CONST_INT
1885               && ((INTVAL (trueop1) & GET_MODE_MASK (mode))
1886                   == GET_MODE_MASK (mode)))
1887             return op0;
1888           if (trueop0 == trueop1 && ! side_effects_p (op0)
1889               && GET_MODE_CLASS (mode) != MODE_CC)
1890             return op0;
1891           /* A & (~A) -> 0 */
1892           if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
1893                || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
1894               && ! side_effects_p (op0)
1895               && GET_MODE_CLASS (mode) != MODE_CC)
1896             return const0_rtx;
1897           tem = simplify_associative_operation (code, mode, op0, op1);
1898           if (tem)
1899             return tem;
1900           break;
1901
1902         case UDIV:
1903           /* Convert divide by power of two into shift (divide by 1 handled
1904              below).  */
1905           if (GET_CODE (trueop1) == CONST_INT
1906               && (arg1 = exact_log2 (INTVAL (trueop1))) > 0)
1907             return simplify_gen_binary (LSHIFTRT, mode, op0, GEN_INT (arg1));
1908
1909           /* Fall through....  */
1910
1911         case DIV:
1912           if (trueop1 == CONST1_RTX (mode))
1913             {
1914               /* On some platforms DIV uses narrower mode than its
1915                  operands.  */
1916               rtx x = gen_lowpart_common (mode, op0);
1917               if (x)
1918                 return x;
1919               else if (mode != GET_MODE (op0) && GET_MODE (op0) != VOIDmode)
1920                 return gen_lowpart_SUBREG (mode, op0);
1921               else
1922                 return op0;
1923             }
1924
1925           /* Maybe change 0 / x to 0.  This transformation isn't safe for
1926              modes with NaNs, since 0 / 0 will then be NaN rather than 0.
1927              Nor is it safe for modes with signed zeros, since dividing
1928              0 by a negative number gives -0, not 0.  */
1929           if (!HONOR_NANS (mode)
1930               && !HONOR_SIGNED_ZEROS (mode)
1931               && trueop0 == CONST0_RTX (mode)
1932               && ! side_effects_p (op1))
1933             return op0;
1934
1935           /* Change division by a constant into multiplication.  Only do
1936              this with -funsafe-math-optimizations.  */
1937           else if (GET_CODE (trueop1) == CONST_DOUBLE
1938                    && GET_MODE_CLASS (GET_MODE (trueop1)) == MODE_FLOAT
1939                    && trueop1 != CONST0_RTX (mode)
1940                    && flag_unsafe_math_optimizations)
1941             {
1942               REAL_VALUE_TYPE d;
1943               REAL_VALUE_FROM_CONST_DOUBLE (d, trueop1);
1944
1945               if (! REAL_VALUES_EQUAL (d, dconst0))
1946                 {
1947                   REAL_ARITHMETIC (d, rtx_to_tree_code (DIV), dconst1, d);
1948                   tem = CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
1949                   return simplify_gen_binary (MULT, mode, op0, tem);
1950                 }
1951             }
1952           break;
1953
1954         case UMOD:
1955           /* Handle modulus by power of two (mod with 1 handled below).  */
1956           if (GET_CODE (trueop1) == CONST_INT
1957               && exact_log2 (INTVAL (trueop1)) > 0)
1958             return simplify_gen_binary (AND, mode, op0,
1959                                         GEN_INT (INTVAL (op1) - 1));
1960
1961           /* Fall through....  */
1962
1963         case MOD:
1964           if ((trueop0 == const0_rtx || trueop1 == const1_rtx)
1965               && ! side_effects_p (op0) && ! side_effects_p (op1))
1966             return const0_rtx;
1967           break;
1968
1969         case ROTATERT:
1970         case ROTATE:
1971         case ASHIFTRT:
1972           /* Rotating ~0 always results in ~0.  */
1973           if (GET_CODE (trueop0) == CONST_INT && width <= HOST_BITS_PER_WIDE_INT
1974               && (unsigned HOST_WIDE_INT) INTVAL (trueop0) == GET_MODE_MASK (mode)
1975               && ! side_effects_p (op1))
1976             return op0;
1977
1978           /* Fall through....  */
1979
1980         case ASHIFT:
1981         case LSHIFTRT:
1982           if (trueop1 == const0_rtx)
1983             return op0;
1984           if (trueop0 == const0_rtx && ! side_effects_p (op1))
1985             return op0;
1986           break;
1987
1988         case SMIN:
1989           if (width <= HOST_BITS_PER_WIDE_INT
1990               && GET_CODE (trueop1) == CONST_INT
1991               && INTVAL (trueop1) == (HOST_WIDE_INT) 1 << (width -1)
1992               && ! side_effects_p (op0))
1993             return op1;
1994           if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
1995             return op0;
1996           tem = simplify_associative_operation (code, mode, op0, op1);
1997           if (tem)
1998             return tem;
1999           break;
2000
2001         case SMAX:
2002           if (width <= HOST_BITS_PER_WIDE_INT
2003               && GET_CODE (trueop1) == CONST_INT
2004               && ((unsigned HOST_WIDE_INT) INTVAL (trueop1)
2005                   == (unsigned HOST_WIDE_INT) GET_MODE_MASK (mode) >> 1)
2006               && ! side_effects_p (op0))
2007             return op1;
2008           if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
2009             return op0;
2010           tem = simplify_associative_operation (code, mode, op0, op1);
2011           if (tem)
2012             return tem;
2013           break;
2014
2015         case UMIN:
2016           if (trueop1 == const0_rtx && ! side_effects_p (op0))
2017             return op1;
2018           if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
2019             return op0;
2020           tem = simplify_associative_operation (code, mode, op0, op1);
2021           if (tem)
2022             return tem;
2023           break;
2024
2025         case UMAX:
2026           if (trueop1 == constm1_rtx && ! side_effects_p (op0))
2027             return op1;
2028           if (rtx_equal_p (trueop0, trueop1) && ! side_effects_p (op0))
2029             return op0;
2030           tem = simplify_associative_operation (code, mode, op0, op1);
2031           if (tem)
2032             return tem;
2033           break;
2034
2035         case SS_PLUS:
2036         case US_PLUS:
2037         case SS_MINUS:
2038         case US_MINUS:
2039           /* ??? There are simplifications that can be done.  */
2040           return 0;
2041
2042         case VEC_SELECT:
2043           if (!VECTOR_MODE_P (mode))
2044             {
2045               if (!VECTOR_MODE_P (GET_MODE (trueop0))
2046                   || (mode
2047                       != GET_MODE_INNER (GET_MODE (trueop0)))
2048                   || GET_CODE (trueop1) != PARALLEL
2049                   || XVECLEN (trueop1, 0) != 1
2050                   || GET_CODE (XVECEXP (trueop1, 0, 0)) != CONST_INT)
2051                 abort ();
2052
2053               if (GET_CODE (trueop0) == CONST_VECTOR)
2054                 return CONST_VECTOR_ELT (trueop0, INTVAL (XVECEXP (trueop1, 0, 0)));
2055             }
2056           else
2057             {
2058               if (!VECTOR_MODE_P (GET_MODE (trueop0))
2059                   || (GET_MODE_INNER (mode)
2060                       != GET_MODE_INNER (GET_MODE (trueop0)))
2061                   || GET_CODE (trueop1) != PARALLEL)
2062                 abort ();
2063
2064               if (GET_CODE (trueop0) == CONST_VECTOR)
2065                 {
2066                   int elt_size = GET_MODE_SIZE (GET_MODE_INNER (mode));
2067                   unsigned n_elts = (GET_MODE_SIZE (mode) / elt_size);
2068                   rtvec v = rtvec_alloc (n_elts);
2069                   unsigned int i;
2070
2071                   if (XVECLEN (trueop1, 0) != (int) n_elts)
2072                     abort ();
2073                   for (i = 0; i < n_elts; i++)
2074                     {
2075                       rtx x = XVECEXP (trueop1, 0, i);
2076
2077                       if (GET_CODE (x) != CONST_INT)
2078                         abort ();
2079                       RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0, INTVAL (x));
2080                     }
2081
2082                   return gen_rtx_CONST_VECTOR (mode, v);
2083                 }
2084             }
2085           return 0;
2086         case VEC_CONCAT:
2087           {
2088             enum machine_mode op0_mode = (GET_MODE (trueop0) != VOIDmode
2089                                           ? GET_MODE (trueop0)
2090                                           : GET_MODE_INNER (mode));
2091             enum machine_mode op1_mode = (GET_MODE (trueop1) != VOIDmode
2092                                           ? GET_MODE (trueop1)
2093                                           : GET_MODE_INNER (mode));
2094
2095             if (!VECTOR_MODE_P (mode)
2096                 || (GET_MODE_SIZE (op0_mode) + GET_MODE_SIZE (op1_mode)
2097                     != GET_MODE_SIZE (mode)))
2098               abort ();
2099
2100             if ((VECTOR_MODE_P (op0_mode)
2101                  && (GET_MODE_INNER (mode)
2102                      != GET_MODE_INNER (op0_mode)))
2103                 || (!VECTOR_MODE_P (op0_mode)
2104                     && GET_MODE_INNER (mode) != op0_mode))
2105               abort ();
2106
2107             if ((VECTOR_MODE_P (op1_mode)
2108                  && (GET_MODE_INNER (mode)
2109                      != GET_MODE_INNER (op1_mode)))
2110                 || (!VECTOR_MODE_P (op1_mode)
2111                     && GET_MODE_INNER (mode) != op1_mode))
2112               abort ();
2113
2114             if ((GET_CODE (trueop0) == CONST_VECTOR
2115                  || GET_CODE (trueop0) == CONST_INT
2116                  || GET_CODE (trueop0) == CONST_DOUBLE)
2117                 && (GET_CODE (trueop1) == CONST_VECTOR
2118                     || GET_CODE (trueop1) == CONST_INT
2119                     || GET_CODE (trueop1) == CONST_DOUBLE))
2120               {
2121                 int elt_size = GET_MODE_SIZE (GET_MODE_INNER (mode));
2122                 unsigned n_elts = (GET_MODE_SIZE (mode) / elt_size);
2123                 rtvec v = rtvec_alloc (n_elts);
2124                 unsigned int i;
2125                 unsigned in_n_elts = 1;
2126
2127                 if (VECTOR_MODE_P (op0_mode))
2128                   in_n_elts = (GET_MODE_SIZE (op0_mode) / elt_size);
2129                 for (i = 0; i < n_elts; i++)
2130                   {
2131                     if (i < in_n_elts)
2132                       {
2133                         if (!VECTOR_MODE_P (op0_mode))
2134                           RTVEC_ELT (v, i) = trueop0;
2135                         else
2136                           RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop0, i);
2137                       }
2138                     else
2139                       {
2140                         if (!VECTOR_MODE_P (op1_mode))
2141                           RTVEC_ELT (v, i) = trueop1;
2142                         else
2143                           RTVEC_ELT (v, i) = CONST_VECTOR_ELT (trueop1,
2144                                                                i - in_n_elts);
2145                       }
2146                   }
2147
2148                 return gen_rtx_CONST_VECTOR (mode, v);
2149               }
2150           }
2151           return 0;
2152
2153         default:
2154           abort ();
2155         }
2156
2157       return 0;
2158     }
2159
2160   /* Get the integer argument values in two forms:
2161      zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S.  */
2162
2163   arg0 = INTVAL (trueop0);
2164   arg1 = INTVAL (trueop1);
2165
2166   if (width < HOST_BITS_PER_WIDE_INT)
2167     {
2168       arg0 &= ((HOST_WIDE_INT) 1 << width) - 1;
2169       arg1 &= ((HOST_WIDE_INT) 1 << width) - 1;
2170
2171       arg0s = arg0;
2172       if (arg0s & ((HOST_WIDE_INT) 1 << (width - 1)))
2173         arg0s |= ((HOST_WIDE_INT) (-1) << width);
2174
2175       arg1s = arg1;
2176       if (arg1s & ((HOST_WIDE_INT) 1 << (width - 1)))
2177         arg1s |= ((HOST_WIDE_INT) (-1) << width);
2178     }
2179   else
2180     {
2181       arg0s = arg0;
2182       arg1s = arg1;
2183     }
2184
2185   /* Compute the value of the arithmetic.  */
2186
2187   switch (code)
2188     {
2189     case PLUS:
2190       val = arg0s + arg1s;
2191       break;
2192
2193     case MINUS:
2194       val = arg0s - arg1s;
2195       break;
2196
2197     case MULT:
2198       val = arg0s * arg1s;
2199       break;
2200
2201     case DIV:
2202       if (arg1s == 0
2203           || (arg0s == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)
2204               && arg1s == -1))
2205         return 0;
2206       val = arg0s / arg1s;
2207       break;
2208
2209     case MOD:
2210       if (arg1s == 0
2211           || (arg0s == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)
2212               && arg1s == -1))
2213         return 0;
2214       val = arg0s % arg1s;
2215       break;
2216
2217     case UDIV:
2218       if (arg1 == 0
2219           || (arg0s == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)
2220               && arg1s == -1))
2221         return 0;
2222       val = (unsigned HOST_WIDE_INT) arg0 / arg1;
2223       break;
2224
2225     case UMOD:
2226       if (arg1 == 0
2227           || (arg0s == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)
2228               && arg1s == -1))
2229         return 0;
2230       val = (unsigned HOST_WIDE_INT) arg0 % arg1;
2231       break;
2232
2233     case AND:
2234       val = arg0 & arg1;
2235       break;
2236
2237     case IOR:
2238       val = arg0 | arg1;
2239       break;
2240
2241     case XOR:
2242       val = arg0 ^ arg1;
2243       break;
2244
2245     case LSHIFTRT:
2246       /* If shift count is undefined, don't fold it; let the machine do
2247          what it wants.  But truncate it if the machine will do that.  */
2248       if (arg1 < 0)
2249         return 0;
2250
2251       if (SHIFT_COUNT_TRUNCATED)
2252         arg1 %= width;
2253
2254       val = ((unsigned HOST_WIDE_INT) arg0) >> arg1;
2255       break;
2256
2257     case ASHIFT:
2258       if (arg1 < 0)
2259         return 0;
2260
2261       if (SHIFT_COUNT_TRUNCATED)
2262         arg1 %= width;
2263
2264       val = ((unsigned HOST_WIDE_INT) arg0) << arg1;
2265       break;
2266
2267     case ASHIFTRT:
2268       if (arg1 < 0)
2269         return 0;
2270
2271       if (SHIFT_COUNT_TRUNCATED)
2272         arg1 %= width;
2273
2274       val = arg0s >> arg1;
2275
2276       /* Bootstrap compiler may not have sign extended the right shift.
2277          Manually extend the sign to insure bootstrap cc matches gcc.  */
2278       if (arg0s < 0 && arg1 > 0)
2279         val |= ((HOST_WIDE_INT) -1) << (HOST_BITS_PER_WIDE_INT - arg1);
2280
2281       break;
2282
2283     case ROTATERT:
2284       if (arg1 < 0)
2285         return 0;
2286
2287       arg1 %= width;
2288       val = ((((unsigned HOST_WIDE_INT) arg0) << (width - arg1))
2289              | (((unsigned HOST_WIDE_INT) arg0) >> arg1));
2290       break;
2291
2292     case ROTATE:
2293       if (arg1 < 0)
2294         return 0;
2295
2296       arg1 %= width;
2297       val = ((((unsigned HOST_WIDE_INT) arg0) << arg1)
2298              | (((unsigned HOST_WIDE_INT) arg0) >> (width - arg1)));
2299       break;
2300
2301     case COMPARE:
2302       /* Do nothing here.  */
2303       return 0;
2304
2305     case SMIN:
2306       val = arg0s <= arg1s ? arg0s : arg1s;
2307       break;
2308
2309     case UMIN:
2310       val = ((unsigned HOST_WIDE_INT) arg0
2311              <= (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
2312       break;
2313
2314     case SMAX:
2315       val = arg0s > arg1s ? arg0s : arg1s;
2316       break;
2317
2318     case UMAX:
2319       val = ((unsigned HOST_WIDE_INT) arg0
2320              > (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
2321       break;
2322
2323     case SS_PLUS:
2324     case US_PLUS:
2325     case SS_MINUS:
2326     case US_MINUS:
2327       /* ??? There are simplifications that can be done.  */
2328       return 0;
2329
2330     default:
2331       abort ();
2332     }
2333
2334   val = trunc_int_for_mode (val, mode);
2335
2336   return GEN_INT (val);
2337 }
2338 \f
2339 /* Simplify a PLUS or MINUS, at least one of whose operands may be another
2340    PLUS or MINUS.
2341
2342    Rather than test for specific case, we do this by a brute-force method
2343    and do all possible simplifications until no more changes occur.  Then
2344    we rebuild the operation.
2345
2346    If FORCE is true, then always generate the rtx.  This is used to
2347    canonicalize stuff emitted from simplify_gen_binary.  Note that this
2348    can still fail if the rtx is too complex.  It won't fail just because
2349    the result is not 'simpler' than the input, however.  */
2350
2351 struct simplify_plus_minus_op_data
2352 {
2353   rtx op;
2354   int neg;
2355 };
2356
2357 static int
2358 simplify_plus_minus_op_data_cmp (const void *p1, const void *p2)
2359 {
2360   const struct simplify_plus_minus_op_data *d1 = p1;
2361   const struct simplify_plus_minus_op_data *d2 = p2;
2362
2363   return (commutative_operand_precedence (d2->op)
2364           - commutative_operand_precedence (d1->op));
2365 }
2366
2367 static rtx
2368 simplify_plus_minus (enum rtx_code code, enum machine_mode mode, rtx op0,
2369                      rtx op1, int force)
2370 {
2371   struct simplify_plus_minus_op_data ops[8];
2372   rtx result, tem;
2373   int n_ops = 2, input_ops = 2, input_consts = 0, n_consts;
2374   int first, changed;
2375   int i, j;
2376
2377   memset (ops, 0, sizeof ops);
2378
2379   /* Set up the two operands and then expand them until nothing has been
2380      changed.  If we run out of room in our array, give up; this should
2381      almost never happen.  */
2382
2383   ops[0].op = op0;
2384   ops[0].neg = 0;
2385   ops[1].op = op1;
2386   ops[1].neg = (code == MINUS);
2387
2388   do
2389     {
2390       changed = 0;
2391
2392       for (i = 0; i < n_ops; i++)
2393         {
2394           rtx this_op = ops[i].op;
2395           int this_neg = ops[i].neg;
2396           enum rtx_code this_code = GET_CODE (this_op);
2397
2398           switch (this_code)
2399             {
2400             case PLUS:
2401             case MINUS:
2402               if (n_ops == 7)
2403                 return NULL_RTX;
2404
2405               ops[n_ops].op = XEXP (this_op, 1);
2406               ops[n_ops].neg = (this_code == MINUS) ^ this_neg;
2407               n_ops++;
2408
2409               ops[i].op = XEXP (this_op, 0);
2410               input_ops++;
2411               changed = 1;
2412               break;
2413
2414             case NEG:
2415               ops[i].op = XEXP (this_op, 0);
2416               ops[i].neg = ! this_neg;
2417               changed = 1;
2418               break;
2419
2420             case CONST:
2421               if (n_ops < 7
2422                   && GET_CODE (XEXP (this_op, 0)) == PLUS
2423                   && CONSTANT_P (XEXP (XEXP (this_op, 0), 0))
2424                   && CONSTANT_P (XEXP (XEXP (this_op, 0), 1)))
2425                 {
2426                   ops[i].op = XEXP (XEXP (this_op, 0), 0);
2427                   ops[n_ops].op = XEXP (XEXP (this_op, 0), 1);
2428                   ops[n_ops].neg = this_neg;
2429                   n_ops++;
2430                   input_consts++;
2431                   changed = 1;
2432                 }
2433               break;
2434
2435             case NOT:
2436               /* ~a -> (-a - 1) */
2437               if (n_ops != 7)
2438                 {
2439                   ops[n_ops].op = constm1_rtx;
2440                   ops[n_ops++].neg = this_neg;
2441                   ops[i].op = XEXP (this_op, 0);
2442                   ops[i].neg = !this_neg;
2443                   changed = 1;
2444                 }
2445               break;
2446
2447             case CONST_INT:
2448               if (this_neg)
2449                 {
2450                   ops[i].op = neg_const_int (mode, this_op);
2451                   ops[i].neg = 0;
2452                   changed = 1;
2453                 }
2454               break;
2455
2456             default:
2457               break;
2458             }
2459         }
2460     }
2461   while (changed);
2462
2463   /* If we only have two operands, we can't do anything.  */
2464   if (n_ops <= 2 && !force)
2465     return NULL_RTX;
2466
2467   /* Count the number of CONSTs we didn't split above.  */
2468   for (i = 0; i < n_ops; i++)
2469     if (GET_CODE (ops[i].op) == CONST)
2470       input_consts++;
2471
2472   /* Now simplify each pair of operands until nothing changes.  The first
2473      time through just simplify constants against each other.  */
2474
2475   first = 1;
2476   do
2477     {
2478       changed = first;
2479
2480       for (i = 0; i < n_ops - 1; i++)
2481         for (j = i + 1; j < n_ops; j++)
2482           {
2483             rtx lhs = ops[i].op, rhs = ops[j].op;
2484             int lneg = ops[i].neg, rneg = ops[j].neg;
2485
2486             if (lhs != 0 && rhs != 0
2487                 && (! first || (CONSTANT_P (lhs) && CONSTANT_P (rhs))))
2488               {
2489                 enum rtx_code ncode = PLUS;
2490
2491                 if (lneg != rneg)
2492                   {
2493                     ncode = MINUS;
2494                     if (lneg)
2495                       tem = lhs, lhs = rhs, rhs = tem;
2496                   }
2497                 else if (swap_commutative_operands_p (lhs, rhs))
2498                   tem = lhs, lhs = rhs, rhs = tem;
2499
2500                 tem = simplify_binary_operation (ncode, mode, lhs, rhs);
2501
2502                 /* Reject "simplifications" that just wrap the two
2503                    arguments in a CONST.  Failure to do so can result
2504                    in infinite recursion with simplify_binary_operation
2505                    when it calls us to simplify CONST operations.  */
2506                 if (tem
2507                     && ! (GET_CODE (tem) == CONST
2508                           && GET_CODE (XEXP (tem, 0)) == ncode
2509                           && XEXP (XEXP (tem, 0), 0) == lhs
2510                           && XEXP (XEXP (tem, 0), 1) == rhs)
2511                     /* Don't allow -x + -1 -> ~x simplifications in the
2512                        first pass.  This allows us the chance to combine
2513                        the -1 with other constants.  */
2514                     && ! (first
2515                           && GET_CODE (tem) == NOT
2516                           && XEXP (tem, 0) == rhs))
2517                   {
2518                     lneg &= rneg;
2519                     if (GET_CODE (tem) == NEG)
2520                       tem = XEXP (tem, 0), lneg = !lneg;
2521                     if (GET_CODE (tem) == CONST_INT && lneg)
2522                       tem = neg_const_int (mode, tem), lneg = 0;
2523
2524                     ops[i].op = tem;
2525                     ops[i].neg = lneg;
2526                     ops[j].op = NULL_RTX;
2527                     changed = 1;
2528                   }
2529               }
2530           }
2531
2532       first = 0;
2533     }
2534   while (changed);
2535
2536   /* Pack all the operands to the lower-numbered entries.  */
2537   for (i = 0, j = 0; j < n_ops; j++)
2538     if (ops[j].op)
2539       ops[i++] = ops[j];
2540   n_ops = i;
2541
2542   /* Sort the operations based on swap_commutative_operands_p.  */
2543   qsort (ops, n_ops, sizeof (*ops), simplify_plus_minus_op_data_cmp);
2544
2545   /* Create (minus -C X) instead of (neg (const (plus X C))).  */
2546   if (n_ops == 2
2547       && GET_CODE (ops[1].op) == CONST_INT
2548       && CONSTANT_P (ops[0].op)
2549       && ops[0].neg)
2550     return gen_rtx_fmt_ee (MINUS, mode, ops[1].op, ops[0].op);
2551   
2552   /* We suppressed creation of trivial CONST expressions in the
2553      combination loop to avoid recursion.  Create one manually now.
2554      The combination loop should have ensured that there is exactly
2555      one CONST_INT, and the sort will have ensured that it is last
2556      in the array and that any other constant will be next-to-last.  */
2557
2558   if (n_ops > 1
2559       && GET_CODE (ops[n_ops - 1].op) == CONST_INT
2560       && CONSTANT_P (ops[n_ops - 2].op))
2561     {
2562       rtx value = ops[n_ops - 1].op;
2563       if (ops[n_ops - 1].neg ^ ops[n_ops - 2].neg)
2564         value = neg_const_int (mode, value);
2565       ops[n_ops - 2].op = plus_constant (ops[n_ops - 2].op, INTVAL (value));
2566       n_ops--;
2567     }
2568
2569   /* Count the number of CONSTs that we generated.  */
2570   n_consts = 0;
2571   for (i = 0; i < n_ops; i++)
2572     if (GET_CODE (ops[i].op) == CONST)
2573       n_consts++;
2574
2575   /* Give up if we didn't reduce the number of operands we had.  Make
2576      sure we count a CONST as two operands.  If we have the same
2577      number of operands, but have made more CONSTs than before, this
2578      is also an improvement, so accept it.  */
2579   if (!force
2580       && (n_ops + n_consts > input_ops
2581           || (n_ops + n_consts == input_ops && n_consts <= input_consts)))
2582     return NULL_RTX;
2583
2584   /* Put a non-negated operand first, if possible.  */
2585
2586   for (i = 0; i < n_ops && ops[i].neg; i++)
2587     continue;
2588   if (i == n_ops)
2589     ops[0].op = gen_rtx_NEG (mode, ops[0].op);
2590   else if (i != 0)
2591     {
2592       tem = ops[0].op;
2593       ops[0] = ops[i];
2594       ops[i].op = tem;
2595       ops[i].neg = 1;
2596     }
2597
2598   /* Now make the result by performing the requested operations.  */
2599   result = ops[0].op;
2600   for (i = 1; i < n_ops; i++)
2601     result = gen_rtx_fmt_ee (ops[i].neg ? MINUS : PLUS,
2602                              mode, result, ops[i].op);
2603
2604   return result;
2605 }
2606
2607 /* Like simplify_binary_operation except used for relational operators.
2608    MODE is the mode of the operands, not that of the result.  If MODE
2609    is VOIDmode, both operands must also be VOIDmode and we compare the
2610    operands in "infinite precision".
2611
2612    If no simplification is possible, this function returns zero.
2613    Otherwise, it returns either const_true_rtx or const0_rtx.  */
2614
2615 rtx
2616 simplify_const_relational_operation (enum rtx_code code,
2617                                      enum machine_mode mode,
2618                                      rtx op0, rtx op1)
2619 {
2620   int equal, op0lt, op0ltu, op1lt, op1ltu;
2621   rtx tem;
2622   rtx trueop0;
2623   rtx trueop1;
2624
2625   if (mode == VOIDmode
2626       && (GET_MODE (op0) != VOIDmode
2627           || GET_MODE (op1) != VOIDmode))
2628     abort ();
2629
2630   /* If op0 is a compare, extract the comparison arguments from it.  */
2631   if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
2632     op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
2633
2634   /* We can't simplify MODE_CC values since we don't know what the
2635      actual comparison is.  */
2636   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC || CC0_P (op0))
2637     return 0;
2638
2639   /* Make sure the constant is second.  */
2640   if (swap_commutative_operands_p (op0, op1))
2641     {
2642       tem = op0, op0 = op1, op1 = tem;
2643       code = swap_condition (code);
2644     }
2645
2646   trueop0 = avoid_constant_pool_reference (op0);
2647   trueop1 = avoid_constant_pool_reference (op1);
2648
2649   /* For integer comparisons of A and B maybe we can simplify A - B and can
2650      then simplify a comparison of that with zero.  If A and B are both either
2651      a register or a CONST_INT, this can't help; testing for these cases will
2652      prevent infinite recursion here and speed things up.
2653
2654      If CODE is an unsigned comparison, then we can never do this optimization,
2655      because it gives an incorrect result if the subtraction wraps around zero.
2656      ANSI C defines unsigned operations such that they never overflow, and
2657      thus such cases can not be ignored; but we cannot do it even for
2658      signed comparisons for languages such as Java, so test flag_wrapv.  */
2659
2660   if (!flag_wrapv && INTEGRAL_MODE_P (mode) && trueop1 != const0_rtx
2661       && ! ((GET_CODE (op0) == REG || GET_CODE (trueop0) == CONST_INT)
2662             && (GET_CODE (op1) == REG || GET_CODE (trueop1) == CONST_INT))
2663       && 0 != (tem = simplify_binary_operation (MINUS, mode, op0, op1))
2664       /* We cannot do this for == or != if tem is a nonzero address.  */
2665       && ((code != EQ && code != NE) || ! nonzero_address_p (tem))
2666       && code != GTU && code != GEU && code != LTU && code != LEU)
2667     return simplify_const_relational_operation (signed_condition (code),
2668                                                 mode, tem, const0_rtx);
2669
2670   if (flag_unsafe_math_optimizations && code == ORDERED)
2671     return const_true_rtx;
2672
2673   if (flag_unsafe_math_optimizations && code == UNORDERED)
2674     return const0_rtx;
2675
2676   /* For modes without NaNs, if the two operands are equal, we know the
2677      result except if they have side-effects.  */
2678   if (! HONOR_NANS (GET_MODE (trueop0))
2679       && rtx_equal_p (trueop0, trueop1)
2680       && ! side_effects_p (trueop0))
2681     equal = 1, op0lt = 0, op0ltu = 0, op1lt = 0, op1ltu = 0;
2682
2683   /* If the operands are floating-point constants, see if we can fold
2684      the result.  */
2685   else if (GET_CODE (trueop0) == CONST_DOUBLE
2686            && GET_CODE (trueop1) == CONST_DOUBLE
2687            && GET_MODE_CLASS (GET_MODE (trueop0)) == MODE_FLOAT)
2688     {
2689       REAL_VALUE_TYPE d0, d1;
2690
2691       REAL_VALUE_FROM_CONST_DOUBLE (d0, trueop0);
2692       REAL_VALUE_FROM_CONST_DOUBLE (d1, trueop1);
2693
2694       /* Comparisons are unordered iff at least one of the values is NaN.  */
2695       if (REAL_VALUE_ISNAN (d0) || REAL_VALUE_ISNAN (d1))
2696         switch (code)
2697           {
2698           case UNEQ:
2699           case UNLT:
2700           case UNGT:
2701           case UNLE:
2702           case UNGE:
2703           case NE:
2704           case UNORDERED:
2705             return const_true_rtx;
2706           case EQ:
2707           case LT:
2708           case GT:
2709           case LE:
2710           case GE:
2711           case LTGT:
2712           case ORDERED:
2713             return const0_rtx;
2714           default:
2715             return 0;
2716           }
2717
2718       equal = REAL_VALUES_EQUAL (d0, d1);
2719       op0lt = op0ltu = REAL_VALUES_LESS (d0, d1);
2720       op1lt = op1ltu = REAL_VALUES_LESS (d1, d0);
2721     }
2722
2723   /* Otherwise, see if the operands are both integers.  */
2724   else if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
2725            && (GET_CODE (trueop0) == CONST_DOUBLE
2726                || GET_CODE (trueop0) == CONST_INT)
2727            && (GET_CODE (trueop1) == CONST_DOUBLE
2728                || GET_CODE (trueop1) == CONST_INT))
2729     {
2730       int width = GET_MODE_BITSIZE (mode);
2731       HOST_WIDE_INT l0s, h0s, l1s, h1s;
2732       unsigned HOST_WIDE_INT l0u, h0u, l1u, h1u;
2733
2734       /* Get the two words comprising each integer constant.  */
2735       if (GET_CODE (trueop0) == CONST_DOUBLE)
2736         {
2737           l0u = l0s = CONST_DOUBLE_LOW (trueop0);
2738           h0u = h0s = CONST_DOUBLE_HIGH (trueop0);
2739         }
2740       else
2741         {
2742           l0u = l0s = INTVAL (trueop0);
2743           h0u = h0s = HWI_SIGN_EXTEND (l0s);
2744         }
2745
2746       if (GET_CODE (trueop1) == CONST_DOUBLE)
2747         {
2748           l1u = l1s = CONST_DOUBLE_LOW (trueop1);
2749           h1u = h1s = CONST_DOUBLE_HIGH (trueop1);
2750         }
2751       else
2752         {
2753           l1u = l1s = INTVAL (trueop1);
2754           h1u = h1s = HWI_SIGN_EXTEND (l1s);
2755         }
2756
2757       /* If WIDTH is nonzero and smaller than HOST_BITS_PER_WIDE_INT,
2758          we have to sign or zero-extend the values.  */
2759       if (width != 0 && width < HOST_BITS_PER_WIDE_INT)
2760         {
2761           l0u &= ((HOST_WIDE_INT) 1 << width) - 1;
2762           l1u &= ((HOST_WIDE_INT) 1 << width) - 1;
2763
2764           if (l0s & ((HOST_WIDE_INT) 1 << (width - 1)))
2765             l0s |= ((HOST_WIDE_INT) (-1) << width);
2766
2767           if (l1s & ((HOST_WIDE_INT) 1 << (width - 1)))
2768             l1s |= ((HOST_WIDE_INT) (-1) << width);
2769         }
2770       if (width != 0 && width <= HOST_BITS_PER_WIDE_INT)
2771         h0u = h1u = 0, h0s = HWI_SIGN_EXTEND (l0s), h1s = HWI_SIGN_EXTEND (l1s);
2772
2773       equal = (h0u == h1u && l0u == l1u);
2774       op0lt = (h0s < h1s || (h0s == h1s && l0u < l1u));
2775       op1lt = (h1s < h0s || (h1s == h0s && l1u < l0u));
2776       op0ltu = (h0u < h1u || (h0u == h1u && l0u < l1u));
2777       op1ltu = (h1u < h0u || (h1u == h0u && l1u < l0u));
2778     }
2779
2780   /* Otherwise, there are some code-specific tests we can make.  */
2781   else
2782     {
2783       switch (code)
2784         {
2785         case EQ:
2786           if (trueop1 == const0_rtx && nonzero_address_p (op0))
2787             return const0_rtx;
2788           break;
2789
2790         case NE:
2791           if (trueop1 == const0_rtx && nonzero_address_p (op0))
2792             return const_true_rtx;
2793           break;
2794
2795         case GEU:
2796           /* Unsigned values are never negative.  */
2797           if (trueop1 == const0_rtx)
2798             return const_true_rtx;
2799           break;
2800
2801         case LTU:
2802           if (trueop1 == const0_rtx)
2803             return const0_rtx;
2804           break;
2805
2806         case LEU:
2807           /* Unsigned values are never greater than the largest
2808              unsigned value.  */
2809           if (GET_CODE (trueop1) == CONST_INT
2810               && (unsigned HOST_WIDE_INT) INTVAL (trueop1) == GET_MODE_MASK (mode)
2811             && INTEGRAL_MODE_P (mode))
2812           return const_true_rtx;
2813           break;
2814
2815         case GTU:
2816           if (GET_CODE (trueop1) == CONST_INT
2817               && (unsigned HOST_WIDE_INT) INTVAL (trueop1) == GET_MODE_MASK (mode)
2818               && INTEGRAL_MODE_P (mode))
2819             return const0_rtx;
2820           break;
2821
2822         case LT:
2823           /* Optimize abs(x) < 0.0.  */
2824           if (trueop1 == CONST0_RTX (mode) && !HONOR_SNANS (mode))
2825             {
2826               tem = GET_CODE (trueop0) == FLOAT_EXTEND ? XEXP (trueop0, 0)
2827                                                        : trueop0;
2828               if (GET_CODE (tem) == ABS)
2829                 return const0_rtx;
2830             }
2831           break;
2832
2833         case GE:
2834           /* Optimize abs(x) >= 0.0.  */
2835           if (trueop1 == CONST0_RTX (mode) && !HONOR_NANS (mode))
2836             {
2837               tem = GET_CODE (trueop0) == FLOAT_EXTEND ? XEXP (trueop0, 0)
2838                                                        : trueop0;
2839               if (GET_CODE (tem) == ABS)
2840                 return const_true_rtx;
2841             }
2842           break;
2843
2844         case UNGE:
2845           /* Optimize ! (abs(x) < 0.0).  */
2846           if (trueop1 == CONST0_RTX (mode))
2847             {
2848               tem = GET_CODE (trueop0) == FLOAT_EXTEND ? XEXP (trueop0, 0)
2849                                                        : trueop0;
2850               if (GET_CODE (tem) == ABS)
2851                 return const_true_rtx;
2852             }
2853           break;
2854
2855         default:
2856           break;
2857         }
2858
2859       return 0;
2860     }
2861
2862   /* If we reach here, EQUAL, OP0LT, OP0LTU, OP1LT, and OP1LTU are set
2863      as appropriate.  */
2864   switch (code)
2865     {
2866     case EQ:
2867     case UNEQ:
2868       return equal ? const_true_rtx : const0_rtx;
2869     case NE:
2870     case LTGT:
2871       return ! equal ? const_true_rtx : const0_rtx;
2872     case LT:
2873     case UNLT:
2874       return op0lt ? const_true_rtx : const0_rtx;
2875     case GT:
2876     case UNGT:
2877       return op1lt ? const_true_rtx : const0_rtx;
2878     case LTU:
2879       return op0ltu ? const_true_rtx : const0_rtx;
2880     case GTU:
2881       return op1ltu ? const_true_rtx : const0_rtx;
2882     case LE:
2883     case UNLE:
2884       return equal || op0lt ? const_true_rtx : const0_rtx;
2885     case GE:
2886     case UNGE:
2887       return equal || op1lt ? const_true_rtx : const0_rtx;
2888     case LEU:
2889       return equal || op0ltu ? const_true_rtx : const0_rtx;
2890     case GEU:
2891       return equal || op1ltu ? const_true_rtx : const0_rtx;
2892     case ORDERED:
2893       return const_true_rtx;
2894     case UNORDERED:
2895       return const0_rtx;
2896     default:
2897       abort ();
2898     }
2899 }
2900
2901 /* Like simplify_binary_operation except used for relational operators.
2902    MODE is the mode of the result, and CMP_MODE is the mode of the operands.
2903    If CMP_MODE is VOIDmode, both operands must also be VOIDmode and we
2904    compare the operands in "infinite precision".  */
2905
2906 rtx
2907 simplify_relational_operation (enum rtx_code code,
2908                                enum machine_mode mode ATTRIBUTE_UNUSED,
2909                                enum machine_mode cmp_mode, rtx op0, rtx op1)
2910 {
2911   rtx tmp;
2912
2913   tmp = simplify_const_relational_operation (code, cmp_mode, op0, op1);
2914   if (tmp)
2915     {
2916 #ifdef FLOAT_STORE_FLAG_VALUE
2917       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2918         {
2919           if (tmp == const0_rtx)
2920             return CONST0_RTX (mode);
2921           return CONST_DOUBLE_FROM_REAL_VALUE (FLOAT_STORE_FLAG_VALUE (mode),
2922                                                mode);
2923         }
2924 #endif
2925       return tmp;
2926     }
2927
2928   return NULL_RTX;
2929 }
2930 \f
2931 /* Simplify CODE, an operation with result mode MODE and three operands,
2932    OP0, OP1, and OP2.  OP0_MODE was the mode of OP0 before it became
2933    a constant.  Return 0 if no simplifications is possible.  */
2934
2935 rtx
2936 simplify_ternary_operation (enum rtx_code code, enum machine_mode mode,
2937                             enum machine_mode op0_mode, rtx op0, rtx op1,
2938                             rtx op2)
2939 {
2940   unsigned int width = GET_MODE_BITSIZE (mode);
2941
2942   /* VOIDmode means "infinite" precision.  */
2943   if (width == 0)
2944     width = HOST_BITS_PER_WIDE_INT;
2945
2946   switch (code)
2947     {
2948     case SIGN_EXTRACT:
2949     case ZERO_EXTRACT:
2950       if (GET_CODE (op0) == CONST_INT
2951           && GET_CODE (op1) == CONST_INT
2952           && GET_CODE (op2) == CONST_INT
2953           && ((unsigned) INTVAL (op1) + (unsigned) INTVAL (op2) <= width)
2954           && width <= (unsigned) HOST_BITS_PER_WIDE_INT)
2955         {
2956           /* Extracting a bit-field from a constant */
2957           HOST_WIDE_INT val = INTVAL (op0);
2958
2959           if (BITS_BIG_ENDIAN)
2960             val >>= (GET_MODE_BITSIZE (op0_mode)
2961                      - INTVAL (op2) - INTVAL (op1));
2962           else
2963             val >>= INTVAL (op2);
2964
2965           if (HOST_BITS_PER_WIDE_INT != INTVAL (op1))
2966             {
2967               /* First zero-extend.  */
2968               val &= ((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1;
2969               /* If desired, propagate sign bit.  */
2970               if (code == SIGN_EXTRACT
2971                   && (val & ((HOST_WIDE_INT) 1 << (INTVAL (op1) - 1))))
2972                 val |= ~ (((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1);
2973             }
2974
2975           /* Clear the bits that don't belong in our mode,
2976              unless they and our sign bit are all one.
2977              So we get either a reasonable negative value or a reasonable
2978              unsigned value for this mode.  */
2979           if (width < HOST_BITS_PER_WIDE_INT
2980               && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
2981                   != ((HOST_WIDE_INT) (-1) << (width - 1))))
2982             val &= ((HOST_WIDE_INT) 1 << width) - 1;
2983
2984           return GEN_INT (val);
2985         }
2986       break;
2987
2988     case IF_THEN_ELSE:
2989       if (GET_CODE (op0) == CONST_INT)
2990         return op0 != const0_rtx ? op1 : op2;
2991
2992       /* Convert c ? a : a into "a".  */
2993       if (rtx_equal_p (op1, op2) && ! side_effects_p (op0))
2994         return op1;
2995
2996       /* Convert a != b ? a : b into "a".  */
2997       if (GET_CODE (op0) == NE
2998           && ! side_effects_p (op0)
2999           && ! HONOR_NANS (mode)
3000           && ! HONOR_SIGNED_ZEROS (mode)
3001           && ((rtx_equal_p (XEXP (op0, 0), op1)
3002                && rtx_equal_p (XEXP (op0, 1), op2))
3003               || (rtx_equal_p (XEXP (op0, 0), op2)
3004                   && rtx_equal_p (XEXP (op0, 1), op1))))
3005         return op1;
3006
3007       /* Convert a == b ? a : b into "b".  */
3008       if (GET_CODE (op0) == EQ
3009           && ! side_effects_p (op0)
3010           && ! HONOR_NANS (mode)
3011           && ! HONOR_SIGNED_ZEROS (mode)
3012           && ((rtx_equal_p (XEXP (op0, 0), op1)
3013                && rtx_equal_p (XEXP (op0, 1), op2))
3014               || (rtx_equal_p (XEXP (op0, 0), op2)
3015                   && rtx_equal_p (XEXP (op0, 1), op1))))
3016         return op2;
3017
3018       if (COMPARISON_P (op0) && ! side_effects_p (op0))
3019         {
3020           enum machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
3021                                         ? GET_MODE (XEXP (op0, 1))
3022                                         : GET_MODE (XEXP (op0, 0)));
3023           rtx temp;
3024           if (cmp_mode == VOIDmode)
3025             cmp_mode = op0_mode;
3026           temp = simplify_const_relational_operation (GET_CODE (op0),
3027                                                       cmp_mode,
3028                                                       XEXP (op0, 0),
3029                                                       XEXP (op0, 1));
3030
3031           /* See if any simplifications were possible.  */
3032           if (temp == const0_rtx)
3033             return op2;
3034           else if (temp == const_true_rtx)
3035             return op1;
3036           else if (temp)
3037             abort ();
3038
3039           /* Look for happy constants in op1 and op2.  */
3040           if (GET_CODE (op1) == CONST_INT && GET_CODE (op2) == CONST_INT)
3041             {
3042               HOST_WIDE_INT t = INTVAL (op1);
3043               HOST_WIDE_INT f = INTVAL (op2);
3044
3045               if (t == STORE_FLAG_VALUE && f == 0)
3046                 code = GET_CODE (op0);
3047               else if (t == 0 && f == STORE_FLAG_VALUE)
3048                 {
3049                   enum rtx_code tmp;
3050                   tmp = reversed_comparison_code (op0, NULL_RTX);
3051                   if (tmp == UNKNOWN)
3052                     break;
3053                   code = tmp;
3054                 }
3055               else
3056                 break;
3057
3058               return gen_rtx_fmt_ee (code, mode, XEXP (op0, 0), XEXP (op0, 1));
3059             }
3060         }
3061       break;
3062
3063     case VEC_MERGE:
3064       if (GET_MODE (op0) != mode
3065           || GET_MODE (op1) != mode
3066           || !VECTOR_MODE_P (mode))
3067         abort ();
3068       op2 = avoid_constant_pool_reference (op2);
3069       if (GET_CODE (op2) == CONST_INT)
3070         {
3071           int elt_size = GET_MODE_SIZE (GET_MODE_INNER (mode));
3072           unsigned n_elts = (GET_MODE_SIZE (mode) / elt_size);
3073           int mask = (1 << n_elts) - 1;
3074
3075           if (!(INTVAL (op2) & mask))
3076             return op1;
3077           if ((INTVAL (op2) & mask) == mask)
3078             return op0;
3079
3080           op0 = avoid_constant_pool_reference (op0);
3081           op1 = avoid_constant_pool_reference (op1);
3082           if (GET_CODE (op0) == CONST_VECTOR
3083               && GET_CODE (op1) == CONST_VECTOR)
3084             {
3085               rtvec v = rtvec_alloc (n_elts);
3086               unsigned int i;
3087
3088               for (i = 0; i < n_elts; i++)
3089                 RTVEC_ELT (v, i) = (INTVAL (op2) & (1 << i)
3090                                     ? CONST_VECTOR_ELT (op0, i)
3091                                     : CONST_VECTOR_ELT (op1, i));
3092               return gen_rtx_CONST_VECTOR (mode, v);
3093             }
3094         }
3095       break;
3096
3097     default:
3098       abort ();
3099     }
3100
3101   return 0;
3102 }
3103
3104 /* Evaluate a SUBREG of a CONST_INT or CONST_DOUBLE or CONST_VECTOR,
3105    returning another CONST_INT or CONST_DOUBLE or CONST_VECTOR.
3106
3107    Works by unpacking OP into a collection of 8-bit values
3108    represented as a little-endian array of 'unsigned char', selecting by BYTE,
3109    and then repacking them again for OUTERMODE.  */
3110
3111 static rtx
3112 simplify_immed_subreg (enum machine_mode outermode, rtx op, 
3113                        enum machine_mode innermode, unsigned int byte)
3114 {
3115   /* We support up to 512-bit values (for V8DFmode).  */
3116   enum {
3117     max_bitsize = 512,
3118     value_bit = 8,
3119     value_mask = (1 << value_bit) - 1
3120   };
3121   unsigned char value[max_bitsize / value_bit];
3122   int value_start;
3123   int i;
3124   int elem;
3125
3126   int num_elem;
3127   rtx * elems;
3128   int elem_bitsize;
3129   rtx result_s;
3130   rtvec result_v = NULL;
3131   enum mode_class outer_class;
3132   enum machine_mode outer_submode;
3133
3134   /* Some ports misuse CCmode.  */
3135   if (GET_MODE_CLASS (outermode) == MODE_CC && GET_CODE (op) == CONST_INT)
3136     return op;
3137
3138   /* Unpack the value.  */
3139
3140   if (GET_CODE (op) == CONST_VECTOR)
3141     {
3142       num_elem = CONST_VECTOR_NUNITS (op);
3143       elems = &CONST_VECTOR_ELT (op, 0);
3144       elem_bitsize = GET_MODE_BITSIZE (GET_MODE_INNER (innermode));
3145     }
3146   else
3147     {
3148       num_elem = 1;
3149       elems = &op;
3150       elem_bitsize = max_bitsize;
3151     }
3152
3153   if (BITS_PER_UNIT % value_bit != 0)
3154     abort ();  /* Too complicated; reducing value_bit may help.  */
3155   if (elem_bitsize % BITS_PER_UNIT != 0)
3156     abort ();  /* I don't know how to handle endianness of sub-units.  */
3157   
3158   for (elem = 0; elem < num_elem; elem++)
3159     {
3160       unsigned char * vp;
3161       rtx el = elems[elem];
3162       
3163       /* Vectors are kept in target memory order.  (This is probably
3164          a mistake.)  */
3165       {
3166         unsigned byte = (elem * elem_bitsize) / BITS_PER_UNIT;
3167         unsigned ibyte = (((num_elem - 1 - elem) * elem_bitsize) 
3168                           / BITS_PER_UNIT);
3169         unsigned word_byte = WORDS_BIG_ENDIAN ? ibyte : byte;
3170         unsigned subword_byte = BYTES_BIG_ENDIAN ? ibyte : byte;
3171         unsigned bytele = (subword_byte % UNITS_PER_WORD
3172                          + (word_byte / UNITS_PER_WORD) * UNITS_PER_WORD);
3173         vp = value + (bytele * BITS_PER_UNIT) / value_bit;
3174       }
3175         
3176       switch (GET_CODE (el))
3177         {
3178         case CONST_INT:
3179           for (i = 0;
3180                i < HOST_BITS_PER_WIDE_INT && i < elem_bitsize; 
3181                i += value_bit)
3182             *vp++ = INTVAL (el) >> i;
3183           /* CONST_INTs are always logically sign-extended.  */
3184           for (; i < elem_bitsize; i += value_bit)
3185             *vp++ = INTVAL (el) < 0 ? -1 : 0;
3186           break;
3187       
3188         case CONST_DOUBLE:
3189           if (GET_MODE (el) == VOIDmode)
3190             {
3191               /* If this triggers, someone should have generated a
3192                  CONST_INT instead.  */
3193               if (elem_bitsize <= HOST_BITS_PER_WIDE_INT)
3194                 abort ();
3195
3196               for (i = 0; i < HOST_BITS_PER_WIDE_INT; i += value_bit)
3197                 *vp++ = CONST_DOUBLE_LOW (el) >> i;
3198               while (i < HOST_BITS_PER_WIDE_INT * 2 && i < elem_bitsize)
3199                 {
3200                   *vp++
3201                     = CONST_DOUBLE_HIGH (el) >> (i - HOST_BITS_PER_WIDE_INT);
3202                   i += value_bit;
3203                 }
3204               /* It shouldn't matter what's done here, so fill it with
3205                  zero.  */
3206               for (; i < max_bitsize; i += value_bit)
3207                 *vp++ = 0;
3208             }
3209           else if (GET_MODE_CLASS (GET_MODE (el)) == MODE_FLOAT)
3210             {
3211               long tmp[max_bitsize / 32];
3212               int bitsize = GET_MODE_BITSIZE (GET_MODE (el));
3213               
3214               if (bitsize > elem_bitsize)
3215                 abort ();
3216               if (bitsize % value_bit != 0)
3217                 abort ();
3218
3219               real_to_target (tmp, CONST_DOUBLE_REAL_VALUE (el),
3220                               GET_MODE (el));
3221
3222               /* real_to_target produces its result in words affected by
3223                  FLOAT_WORDS_BIG_ENDIAN.  However, we ignore this,
3224                  and use WORDS_BIG_ENDIAN instead; see the documentation
3225                  of SUBREG in rtl.texi.  */
3226               for (i = 0; i < bitsize; i += value_bit)
3227                 {
3228                   int ibase;
3229                   if (WORDS_BIG_ENDIAN)
3230                     ibase = bitsize - 1 - i;
3231                   else
3232                     ibase = i;
3233                   *vp++ = tmp[ibase / 32] >> i % 32;
3234                 }
3235               
3236               /* It shouldn't matter what's done here, so fill it with
3237                  zero.  */
3238               for (; i < elem_bitsize; i += value_bit)
3239                 *vp++ = 0;
3240             }
3241           else
3242             abort ();
3243           break;
3244           
3245         default:
3246           abort ();
3247         }
3248     }
3249
3250   /* Now, pick the right byte to start with.  */
3251   /* Renumber BYTE so that the least-significant byte is byte 0.  A special
3252      case is paradoxical SUBREGs, which shouldn't be adjusted since they
3253      will already have offset 0.  */
3254   if (GET_MODE_SIZE (innermode) >= GET_MODE_SIZE (outermode))
3255     {
3256       unsigned ibyte = (GET_MODE_SIZE (innermode) - GET_MODE_SIZE (outermode) 
3257                         - byte);
3258       unsigned word_byte = WORDS_BIG_ENDIAN ? ibyte : byte;
3259       unsigned subword_byte = BYTES_BIG_ENDIAN ? ibyte : byte;
3260       byte = (subword_byte % UNITS_PER_WORD
3261               + (word_byte / UNITS_PER_WORD) * UNITS_PER_WORD);
3262     }
3263
3264   /* BYTE should still be inside OP.  (Note that BYTE is unsigned,
3265      so if it's become negative it will instead be very large.)  */
3266   if (byte >= GET_MODE_SIZE (innermode))
3267     abort ();
3268
3269   /* Convert from bytes to chunks of size value_bit.  */
3270   value_start = byte * (BITS_PER_UNIT / value_bit);
3271
3272   /* Re-pack the value.  */
3273     
3274   if (VECTOR_MODE_P (outermode))
3275     {
3276       num_elem = GET_MODE_NUNITS (outermode);
3277       result_v = rtvec_alloc (num_elem);
3278       elems = &RTVEC_ELT (result_v, 0);
3279       outer_submode = GET_MODE_INNER (outermode);
3280     }
3281   else
3282     {
3283       num_elem = 1;
3284       elems = &result_s;
3285       outer_submode = outermode;
3286     }
3287
3288   outer_class = GET_MODE_CLASS (outer_submode);
3289   elem_bitsize = GET_MODE_BITSIZE (outer_submode);
3290
3291   if (elem_bitsize % value_bit != 0)
3292     abort ();
3293   if (elem_bitsize + value_start * value_bit > max_bitsize)
3294     abort ();
3295
3296   for (elem = 0; elem < num_elem; elem++)
3297     {
3298       unsigned char *vp;
3299       
3300       /* Vectors are stored in target memory order.  (This is probably
3301          a mistake.)  */
3302       {
3303         unsigned byte = (elem * elem_bitsize) / BITS_PER_UNIT;
3304         unsigned ibyte = (((num_elem - 1 - elem) * elem_bitsize) 
3305                           / BITS_PER_UNIT);
3306         unsigned word_byte = WORDS_BIG_ENDIAN ? ibyte : byte;
3307         unsigned subword_byte = BYTES_BIG_ENDIAN ? ibyte : byte;
3308         unsigned bytele = (subword_byte % UNITS_PER_WORD
3309                          + (word_byte / UNITS_PER_WORD) * UNITS_PER_WORD);
3310         vp = value + value_start + (bytele * BITS_PER_UNIT) / value_bit;
3311       }
3312
3313       switch (outer_class)
3314         {
3315         case MODE_INT:
3316         case MODE_PARTIAL_INT:
3317           {
3318             unsigned HOST_WIDE_INT hi = 0, lo = 0;
3319
3320             for (i = 0;
3321                  i < HOST_BITS_PER_WIDE_INT && i < elem_bitsize;
3322                  i += value_bit)
3323               lo |= (HOST_WIDE_INT)(*vp++ & value_mask) << i;
3324             for (; i < elem_bitsize; i += value_bit)
3325               hi |= ((HOST_WIDE_INT)(*vp++ & value_mask)
3326                      << (i - HOST_BITS_PER_WIDE_INT));
3327             
3328             /* immed_double_const doesn't call trunc_int_for_mode.  I don't
3329                know why.  */
3330             if (elem_bitsize <= HOST_BITS_PER_WIDE_INT)
3331               elems[elem] = gen_int_mode (lo, outer_submode);
3332             else
3333               elems[elem] = immed_double_const (lo, hi, outer_submode);
3334           }
3335           break;
3336       
3337         case MODE_FLOAT:
3338           {
3339             REAL_VALUE_TYPE r;
3340             long tmp[max_bitsize / 32];
3341             
3342             /* real_from_target wants its input in words affected by
3343                FLOAT_WORDS_BIG_ENDIAN.  However, we ignore this,
3344                and use WORDS_BIG_ENDIAN instead; see the documentation
3345                of SUBREG in rtl.texi.  */
3346             for (i = 0; i < max_bitsize / 32; i++)
3347               tmp[i] = 0;
3348             for (i = 0; i < elem_bitsize; i += value_bit)
3349               {
3350                 int ibase;
3351                 if (WORDS_BIG_ENDIAN)
3352                   ibase = elem_bitsize - 1 - i;
3353                 else
3354                   ibase = i;
3355                 tmp[ibase / 32] |= (*vp++ & value_mask) << i % 32;
3356               }
3357
3358             real_from_target (&r, tmp, outer_submode);
3359             elems[elem] = CONST_DOUBLE_FROM_REAL_VALUE (r, outer_submode);
3360           }
3361           break;
3362             
3363         default:
3364           abort ();
3365         }
3366     }
3367   if (VECTOR_MODE_P (outermode))
3368     return gen_rtx_CONST_VECTOR (outermode, result_v);
3369   else
3370     return result_s;
3371 }
3372
3373 /* Simplify SUBREG:OUTERMODE(OP:INNERMODE, BYTE)
3374    Return 0 if no simplifications are possible.  */
3375 rtx
3376 simplify_subreg (enum machine_mode outermode, rtx op,
3377                  enum machine_mode innermode, unsigned int byte)
3378 {
3379   /* Little bit of sanity checking.  */
3380   if (innermode == VOIDmode || outermode == VOIDmode
3381       || innermode == BLKmode || outermode == BLKmode)
3382     abort ();
3383
3384   if (GET_MODE (op) != innermode
3385       && GET_MODE (op) != VOIDmode)
3386     abort ();
3387
3388   if (byte % GET_MODE_SIZE (outermode)
3389       || byte >= GET_MODE_SIZE (innermode))
3390     abort ();
3391
3392   if (outermode == innermode && !byte)
3393     return op;
3394
3395   if (GET_CODE (op) == CONST_INT
3396       || GET_CODE (op) == CONST_DOUBLE
3397       || GET_CODE (op) == CONST_VECTOR)
3398     return simplify_immed_subreg (outermode, op, innermode, byte);
3399
3400   /* Changing mode twice with SUBREG => just change it once,
3401      or not at all if changing back op starting mode.  */
3402   if (GET_CODE (op) == SUBREG)
3403     {
3404       enum machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3405       int final_offset = byte + SUBREG_BYTE (op);
3406       rtx new;
3407
3408       if (outermode == innermostmode
3409           && byte == 0 && SUBREG_BYTE (op) == 0)
3410         return SUBREG_REG (op);
3411
3412       /* The SUBREG_BYTE represents offset, as if the value were stored
3413          in memory.  Irritating exception is paradoxical subreg, where
3414          we define SUBREG_BYTE to be 0.  On big endian machines, this
3415          value should be negative.  For a moment, undo this exception.  */
3416       if (byte == 0 && GET_MODE_SIZE (innermode) < GET_MODE_SIZE (outermode))
3417         {
3418           int difference = (GET_MODE_SIZE (innermode) - GET_MODE_SIZE (outermode));
3419           if (WORDS_BIG_ENDIAN)
3420             final_offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3421           if (BYTES_BIG_ENDIAN)
3422             final_offset += difference % UNITS_PER_WORD;
3423         }
3424       if (SUBREG_BYTE (op) == 0
3425           && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3426         {
3427           int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3428           if (WORDS_BIG_ENDIAN)
3429             final_offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3430           if (BYTES_BIG_ENDIAN)
3431             final_offset += difference % UNITS_PER_WORD;
3432         }
3433
3434       /* See whether resulting subreg will be paradoxical.  */
3435       if (GET_MODE_SIZE (innermostmode) > GET_MODE_SIZE (outermode))
3436         {
3437           /* In nonparadoxical subregs we can't handle negative offsets.  */
3438           if (final_offset < 0)
3439             return NULL_RTX;
3440           /* Bail out in case resulting subreg would be incorrect.  */
3441           if (final_offset % GET_MODE_SIZE (outermode)
3442               || (unsigned) final_offset >= GET_MODE_SIZE (innermostmode))
3443             return NULL_RTX;
3444         }
3445       else
3446         {
3447           int offset = 0;
3448           int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (outermode));
3449
3450           /* In paradoxical subreg, see if we are still looking on lower part.
3451              If so, our SUBREG_BYTE will be 0.  */
3452           if (WORDS_BIG_ENDIAN)
3453             offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3454           if (BYTES_BIG_ENDIAN)
3455             offset += difference % UNITS_PER_WORD;
3456           if (offset == final_offset)
3457             final_offset = 0;
3458           else
3459             return NULL_RTX;
3460         }
3461
3462       /* Recurse for further possible simplifications.  */
3463       new = simplify_subreg (outermode, SUBREG_REG (op),
3464                              GET_MODE (SUBREG_REG (op)),
3465                              final_offset);
3466       if (new)
3467         return new;
3468       return gen_rtx_SUBREG (outermode, SUBREG_REG (op), final_offset);
3469     }
3470
3471   /* SUBREG of a hard register => just change the register number
3472      and/or mode.  If the hard register is not valid in that mode,
3473      suppress this simplification.  If the hard register is the stack,
3474      frame, or argument pointer, leave this as a SUBREG.  */
3475
3476   if (REG_P (op)
3477       && (! REG_FUNCTION_VALUE_P (op)
3478           || ! rtx_equal_function_value_matters)
3479       && REGNO (op) < FIRST_PSEUDO_REGISTER
3480 #ifdef CANNOT_CHANGE_MODE_CLASS
3481       && ! (REG_CANNOT_CHANGE_MODE_P (REGNO (op), innermode, outermode)
3482             && GET_MODE_CLASS (innermode) != MODE_COMPLEX_INT
3483             && GET_MODE_CLASS (innermode) != MODE_COMPLEX_FLOAT)
3484 #endif
3485       && ((reload_completed && !frame_pointer_needed)
3486           || (REGNO (op) != FRAME_POINTER_REGNUM
3487 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3488               && REGNO (op) != HARD_FRAME_POINTER_REGNUM
3489 #endif
3490              ))
3491 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3492       && REGNO (op) != ARG_POINTER_REGNUM
3493 #endif
3494       && REGNO (op) != STACK_POINTER_REGNUM
3495       && subreg_offset_representable_p (REGNO (op), innermode,
3496                                         byte, outermode))
3497     {
3498       rtx tem = gen_rtx_SUBREG (outermode, op, byte);
3499       int final_regno = subreg_hard_regno (tem, 0);
3500
3501       /* ??? We do allow it if the current REG is not valid for
3502          its mode.  This is a kludge to work around how float/complex
3503          arguments are passed on 32-bit SPARC and should be fixed.  */
3504       if (HARD_REGNO_MODE_OK (final_regno, outermode)
3505           || ! HARD_REGNO_MODE_OK (REGNO (op), innermode))
3506         {
3507           rtx x = gen_rtx_REG_offset (op, outermode, final_regno, byte);
3508
3509           /* Propagate original regno.  We don't have any way to specify
3510              the offset inside original regno, so do so only for lowpart.
3511              The information is used only by alias analysis that can not
3512              grog partial register anyway.  */
3513
3514           if (subreg_lowpart_offset (outermode, innermode) == byte)
3515             ORIGINAL_REGNO (x) = ORIGINAL_REGNO (op);
3516           return x;
3517         }
3518     }
3519
3520   /* If we have a SUBREG of a register that we are replacing and we are
3521      replacing it with a MEM, make a new MEM and try replacing the
3522      SUBREG with it.  Don't do this if the MEM has a mode-dependent address
3523      or if we would be widening it.  */
3524
3525   if (GET_CODE (op) == MEM
3526       && ! mode_dependent_address_p (XEXP (op, 0))
3527       /* Allow splitting of volatile memory references in case we don't
3528          have instruction to move the whole thing.  */
3529       && (! MEM_VOLATILE_P (op)
3530           || ! have_insn_for (SET, innermode))
3531       && GET_MODE_SIZE (outermode) <= GET_MODE_SIZE (GET_MODE (op)))
3532     return adjust_address_nv (op, outermode, byte);
3533
3534   /* Handle complex values represented as CONCAT
3535      of real and imaginary part.  */
3536   if (GET_CODE (op) == CONCAT)
3537     {
3538       int is_realpart = byte < (unsigned int) GET_MODE_UNIT_SIZE (innermode);
3539       rtx part = is_realpart ? XEXP (op, 0) : XEXP (op, 1);
3540       unsigned int final_offset;
3541       rtx res;
3542
3543       final_offset = byte % (GET_MODE_UNIT_SIZE (innermode));
3544       res = simplify_subreg (outermode, part, GET_MODE (part), final_offset);
3545       if (res)
3546         return res;
3547       /* We can at least simplify it by referring directly to the
3548          relevant part.  */
3549       return gen_rtx_SUBREG (outermode, part, final_offset);
3550     }
3551
3552   /* Optimize SUBREG truncations of zero and sign extended values.  */
3553   if ((GET_CODE (op) == ZERO_EXTEND
3554        || GET_CODE (op) == SIGN_EXTEND)
3555       && GET_MODE_BITSIZE (outermode) < GET_MODE_BITSIZE (innermode))
3556     {
3557       unsigned int bitpos = subreg_lsb_1 (outermode, innermode, byte);
3558
3559       /* If we're requesting the lowpart of a zero or sign extension,
3560          there are three possibilities.  If the outermode is the same
3561          as the origmode, we can omit both the extension and the subreg.
3562          If the outermode is not larger than the origmode, we can apply
3563          the truncation without the extension.  Finally, if the outermode
3564          is larger than the origmode, but both are integer modes, we
3565          can just extend to the appropriate mode.  */
3566       if (bitpos == 0)
3567         {
3568           enum machine_mode origmode = GET_MODE (XEXP (op, 0));
3569           if (outermode == origmode)
3570             return XEXP (op, 0);
3571           if (GET_MODE_BITSIZE (outermode) <= GET_MODE_BITSIZE (origmode))
3572             return simplify_gen_subreg (outermode, XEXP (op, 0), origmode,
3573                                         subreg_lowpart_offset (outermode,
3574                                                                origmode));
3575           if (SCALAR_INT_MODE_P (outermode))
3576             return simplify_gen_unary (GET_CODE (op), outermode,
3577                                        XEXP (op, 0), origmode);
3578         }
3579
3580       /* A SUBREG resulting from a zero extension may fold to zero if
3581          it extracts higher bits that the ZERO_EXTEND's source bits.  */
3582       if (GET_CODE (op) == ZERO_EXTEND
3583           && bitpos >= GET_MODE_BITSIZE (GET_MODE (XEXP (op, 0))))
3584         return CONST0_RTX (outermode);
3585     }
3586
3587   return NULL_RTX;
3588 }
3589
3590 /* Make a SUBREG operation or equivalent if it folds.  */
3591
3592 rtx
3593 simplify_gen_subreg (enum machine_mode outermode, rtx op,
3594                      enum machine_mode innermode, unsigned int byte)
3595 {
3596   rtx new;
3597   /* Little bit of sanity checking.  */
3598   if (innermode == VOIDmode || outermode == VOIDmode
3599       || innermode == BLKmode || outermode == BLKmode)
3600     abort ();
3601
3602   if (GET_MODE (op) != innermode
3603       && GET_MODE (op) != VOIDmode)
3604     abort ();
3605
3606   if (byte % GET_MODE_SIZE (outermode)
3607       || byte >= GET_MODE_SIZE (innermode))
3608     abort ();
3609
3610   if (GET_CODE (op) == QUEUED)
3611     return NULL_RTX;
3612
3613   new = simplify_subreg (outermode, op, innermode, byte);
3614   if (new)
3615     return new;
3616
3617   if (GET_CODE (op) == SUBREG || GET_MODE (op) == VOIDmode)
3618     return NULL_RTX;
3619
3620   return gen_rtx_SUBREG (outermode, op, byte);
3621 }
3622 /* Simplify X, an rtx expression.
3623
3624    Return the simplified expression or NULL if no simplifications
3625    were possible.
3626
3627    This is the preferred entry point into the simplification routines;
3628    however, we still allow passes to call the more specific routines.
3629
3630    Right now GCC has three (yes, three) major bodies of RTL simplification
3631    code that need to be unified.
3632
3633         1. fold_rtx in cse.c.  This code uses various CSE specific
3634            information to aid in RTL simplification.
3635
3636         2. simplify_rtx in combine.c.  Similar to fold_rtx, except that
3637            it uses combine specific information to aid in RTL
3638            simplification.
3639
3640         3. The routines in this file.
3641
3642
3643    Long term we want to only have one body of simplification code; to
3644    get to that state I recommend the following steps:
3645
3646         1. Pour over fold_rtx & simplify_rtx and move any simplifications
3647            which are not pass dependent state into these routines.
3648
3649         2. As code is moved by #1, change fold_rtx & simplify_rtx to
3650            use this routine whenever possible.
3651
3652         3. Allow for pass dependent state to be provided to these
3653            routines and add simplifications based on the pass dependent
3654            state.  Remove code from cse.c & combine.c that becomes
3655            redundant/dead.
3656
3657     It will take time, but ultimately the compiler will be easier to
3658     maintain and improve.  It's totally silly that when we add a
3659     simplification that it needs to be added to 4 places (3 for RTL
3660     simplification and 1 for tree simplification.  */
3661
3662 rtx
3663 simplify_rtx (rtx x)
3664 {
3665   enum rtx_code code = GET_CODE (x);
3666   enum machine_mode mode = GET_MODE (x);
3667   rtx temp;
3668
3669   switch (GET_RTX_CLASS (code))
3670     {
3671     case RTX_UNARY:
3672       return simplify_unary_operation (code, mode,
3673                                        XEXP (x, 0), GET_MODE (XEXP (x, 0)));
3674     case RTX_COMM_ARITH:
3675       if (swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3676         return simplify_gen_binary (code, mode, XEXP (x, 1), XEXP (x, 0));
3677
3678       /* Fall through....  */
3679
3680     case RTX_BIN_ARITH:
3681       return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3682
3683     case RTX_TERNARY:
3684     case RTX_BITFIELD_OPS:
3685       return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
3686                                          XEXP (x, 0), XEXP (x, 1),
3687                                          XEXP (x, 2));
3688
3689     case RTX_COMPARE:
3690     case RTX_COMM_COMPARE:
3691       temp = simplify_relational_operation (code, mode,
3692                                             ((GET_MODE (XEXP (x, 0))
3693                                               != VOIDmode)
3694                                              ? GET_MODE (XEXP (x, 0))
3695                                              : GET_MODE (XEXP (x, 1))),
3696                                             XEXP (x, 0), XEXP (x, 1));
3697       return temp;
3698
3699     case RTX_EXTRA:
3700       if (code == SUBREG)
3701         return simplify_gen_subreg (mode, SUBREG_REG (x),
3702                                     GET_MODE (SUBREG_REG (x)),
3703                                     SUBREG_BYTE (x));
3704       if (code == CONSTANT_P_RTX)
3705         {
3706           if (CONSTANT_P (XEXP (x, 0)))
3707             return const1_rtx;
3708         }
3709       break;
3710
3711     case RTX_OBJ:
3712       if (code == LO_SUM)
3713         {
3714           /* Convert (lo_sum (high FOO) FOO) to FOO.  */
3715           if (GET_CODE (XEXP (x, 0)) == HIGH
3716               && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
3717           return XEXP (x, 1);
3718         }
3719       break;
3720
3721     default:
3722       break;
3723     }
3724   return NULL;
3725 }