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