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