OSDN Git Service

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