OSDN Git Service

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