OSDN Git Service

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