OSDN Git Service

* combine.c (combine_simplify_rtx): If the modes are all VOIDmode,
[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 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include <setjmp.h>
26
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "flags.h"
32 #include "real.h"
33 #include "insn-config.h"
34 #include "recog.h"
35 #include "function.h"
36 #include "expr.h"
37 #include "toplev.h"
38 #include "output.h"
39 #include "ggc.h"
40 #include "obstack.h"
41 #include "hashtab.h"
42 #include "cselib.h"
43
44 /* Simplification and canonicalization of RTL.  */
45
46 /* Nonzero if X has the form (PLUS frame-pointer integer).  We check for
47    virtual regs here because the simplify_*_operation routines are called
48    by integrate.c, which is called before virtual register instantiation.
49
50    ?!? FIXED_BASE_PLUS_P and NONZERO_BASE_PLUS_P need to move into 
51    a header file so that their definitions can be shared with the
52    simplification routines in simplify-rtx.c.  Until then, do not
53    change these macros without also changing the copy in simplify-rtx.c.  */
54
55 #define FIXED_BASE_PLUS_P(X)                                    \
56   ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx    \
57    || ((X) == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])\
58    || (X) == virtual_stack_vars_rtx                             \
59    || (X) == virtual_incoming_args_rtx                          \
60    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
61        && (XEXP (X, 0) == frame_pointer_rtx                     \
62            || XEXP (X, 0) == hard_frame_pointer_rtx             \
63            || ((X) == arg_pointer_rtx                           \
64                && fixed_regs[ARG_POINTER_REGNUM])               \
65            || XEXP (X, 0) == virtual_stack_vars_rtx             \
66            || XEXP (X, 0) == virtual_incoming_args_rtx))        \
67    || GET_CODE (X) == ADDRESSOF)
68
69 /* Similar, but also allows reference to the stack pointer.
70
71    This used to include FIXED_BASE_PLUS_P, however, we can't assume that
72    arg_pointer_rtx by itself is nonzero, because on at least one machine,
73    the i960, the arg pointer is zero when it is unused.  */
74
75 #define NONZERO_BASE_PLUS_P(X)                                  \
76   ((X) == frame_pointer_rtx || (X) == hard_frame_pointer_rtx    \
77    || (X) == virtual_stack_vars_rtx                             \
78    || (X) == virtual_incoming_args_rtx                          \
79    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
80        && (XEXP (X, 0) == frame_pointer_rtx                     \
81            || XEXP (X, 0) == hard_frame_pointer_rtx             \
82            || ((X) == arg_pointer_rtx                           \
83                && fixed_regs[ARG_POINTER_REGNUM])               \
84            || XEXP (X, 0) == virtual_stack_vars_rtx             \
85            || XEXP (X, 0) == virtual_incoming_args_rtx))        \
86    || (X) == stack_pointer_rtx                                  \
87    || (X) == virtual_stack_dynamic_rtx                          \
88    || (X) == virtual_outgoing_args_rtx                          \
89    || (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
90        && (XEXP (X, 0) == stack_pointer_rtx                     \
91            || XEXP (X, 0) == virtual_stack_dynamic_rtx          \
92            || XEXP (X, 0) == virtual_outgoing_args_rtx))        \
93    || GET_CODE (X) == ADDRESSOF)
94
95 /* Much code operates on (low, high) pairs; the low value is an
96    unsigned wide int, the high value a signed wide int.  We
97    occasionally need to sign extend from low to high as if low were a
98    signed wide int.  */
99 #define HWI_SIGN_EXTEND(low) \
100  ((((HOST_WIDE_INT) low) < 0) ? ((HOST_WIDE_INT) -1) : ((HOST_WIDE_INT) 0))
101
102 static rtx simplify_plus_minus          PARAMS ((enum rtx_code,
103                                                  enum machine_mode, rtx, rtx));
104 static void check_fold_consts           PARAMS ((PTR));
105 static int entry_and_rtx_equal_p        PARAMS ((const void *, const void *));
106 static unsigned int get_value_hash      PARAMS ((const void *));
107 static struct elt_list *new_elt_list    PARAMS ((struct elt_list *,
108                                                  cselib_val *));
109 static struct elt_loc_list *new_elt_loc_list PARAMS ((struct elt_loc_list *,
110                                                       rtx));
111 static void unchain_one_value           PARAMS ((cselib_val *));
112 static void unchain_one_elt_list        PARAMS ((struct elt_list **));
113 static void unchain_one_elt_loc_list    PARAMS ((struct elt_loc_list **));
114 static void clear_table                 PARAMS ((int));
115 static int discard_useless_locs         PARAMS ((void **, void *));
116 static int discard_useless_values       PARAMS ((void **, void *));
117 static void remove_useless_values       PARAMS ((void));
118 static rtx wrap_constant                PARAMS ((enum machine_mode, rtx));
119 static unsigned int hash_rtx            PARAMS ((rtx, enum machine_mode, int));
120 static cselib_val *new_cselib_val       PARAMS ((unsigned int,
121                                                  enum machine_mode));
122 static void add_mem_for_addr            PARAMS ((cselib_val *, cselib_val *,
123                                                  rtx));
124 static cselib_val *cselib_lookup_mem    PARAMS ((rtx, int));
125 static rtx cselib_subst_to_values       PARAMS ((rtx));
126 static void cselib_invalidate_regno     PARAMS ((unsigned int,
127                                                  enum machine_mode));
128 static int cselib_mem_conflict_p        PARAMS ((rtx, rtx));
129 static int cselib_invalidate_mem_1      PARAMS ((void **, void *));
130 static void cselib_invalidate_mem       PARAMS ((rtx));
131 static void cselib_invalidate_rtx       PARAMS ((rtx, rtx, void *));
132 static void cselib_record_set           PARAMS ((rtx, cselib_val *,
133                                                  cselib_val *));
134 static void cselib_record_sets          PARAMS ((rtx));
135
136 /* There are three ways in which cselib can look up an rtx:
137    - for a REG, the reg_values table (which is indexed by regno) is used
138    - for a MEM, we recursively look up its address and then follow the
139      addr_list of that value
140    - for everything else, we compute a hash value and go through the hash
141      table.  Since different rtx's can still have the same hash value,
142      this involves walking the table entries for a given value and comparing
143      the locations of the entries with the rtx we are looking up.  */
144
145 /* A table that enables us to look up elts by their value.  */
146 static htab_t hash_table;
147
148 /* This is a global so we don't have to pass this through every function.
149    It is used in new_elt_loc_list to set SETTING_INSN.  */
150 static rtx cselib_current_insn;
151
152 /* Every new unknown value gets a unique number.  */
153 static unsigned int next_unknown_value;
154
155 /* The number of registers we had when the varrays were last resized.  */
156 static unsigned int cselib_nregs;
157
158 /* Count values without known locations.  Whenever this grows too big, we
159    remove these useless values from the table.  */
160 static int n_useless_values;
161
162 /* Number of useless values before we remove them from the hash table.  */
163 #define MAX_USELESS_VALUES 32
164
165 /* This table maps from register number to values.  It does not contain
166    pointers to cselib_val structures, but rather elt_lists.  The purpose is
167    to be able to refer to the same register in different modes.  */
168 static varray_type reg_values;
169 #define REG_VALUES(I) VARRAY_ELT_LIST (reg_values, (I))
170
171 /* Here the set of indices I with REG_VALUES(I) != 0 is saved.  This is used
172    in clear_table() for fast emptying.  */
173 static varray_type used_regs;
174
175 /* We pass this to cselib_invalidate_mem to invalidate all of
176    memory for a non-const call instruction.  */
177 static rtx callmem;
178
179 /* Memory for our structures is allocated from this obstack.  */
180 static struct obstack cselib_obstack;
181
182 /* Used to quickly free all memory.  */
183 static char *cselib_startobj;
184
185 /* Caches for unused structures.  */
186 static cselib_val *empty_vals;
187 static struct elt_list *empty_elt_lists;
188 static struct elt_loc_list *empty_elt_loc_lists;
189
190 /* Set by discard_useless_locs if it deleted the last location of any
191    value.  */
192 static int values_became_useless;
193 \f
194 /* Make a binary operation by properly ordering the operands and 
195    seeing if the expression folds.  */
196
197 rtx
198 simplify_gen_binary (code, mode, op0, op1)
199      enum rtx_code code;
200      enum machine_mode mode;
201      rtx op0, op1;
202 {
203   rtx tem;
204
205   /* Put complex operands first and constants second if commutative.  */
206   if (GET_RTX_CLASS (code) == 'c'
207       && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
208           || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
209               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
210           || (GET_CODE (op0) == SUBREG
211               && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
212               && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
213     tem = op0, op0 = op1, op1 = tem;
214
215   /* If this simplifies, do it.  */
216   tem = simplify_binary_operation (code, mode, op0, op1);
217
218   if (tem)
219     return tem;
220
221   /* Handle addition and subtraction of CONST_INT specially.  Otherwise,
222      just form the operation.  */
223
224   if (code == PLUS && GET_CODE (op1) == CONST_INT
225       && GET_MODE (op0) != VOIDmode)
226     return plus_constant (op0, INTVAL (op1));
227   else if (code == MINUS && GET_CODE (op1) == CONST_INT
228            && GET_MODE (op0) != VOIDmode)
229     return plus_constant (op0, - INTVAL (op1));
230   else
231     return gen_rtx_fmt_ee (code, mode, op0, op1);
232 }
233 \f
234 /* Try to simplify a unary operation CODE whose output mode is to be
235    MODE with input operand OP whose mode was originally OP_MODE.
236    Return zero if no simplification can be made.  */
237
238 rtx
239 simplify_unary_operation (code, mode, op, op_mode)
240      enum rtx_code code;
241      enum machine_mode mode;
242      rtx op;
243      enum machine_mode op_mode;
244 {
245   unsigned int width = GET_MODE_BITSIZE (mode);
246
247   /* The order of these tests is critical so that, for example, we don't
248      check the wrong mode (input vs. output) for a conversion operation,
249      such as FIX.  At some point, this should be simplified.  */
250
251 #if !defined(REAL_IS_NOT_DOUBLE) || defined(REAL_ARITHMETIC)
252
253   if (code == FLOAT && GET_MODE (op) == VOIDmode
254       && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
255     {
256       HOST_WIDE_INT hv, lv;
257       REAL_VALUE_TYPE d;
258
259       if (GET_CODE (op) == CONST_INT)
260         lv = INTVAL (op), hv = HWI_SIGN_EXTEND (lv);
261       else
262         lv = CONST_DOUBLE_LOW (op),  hv = CONST_DOUBLE_HIGH (op);
263
264 #ifdef REAL_ARITHMETIC
265       REAL_VALUE_FROM_INT (d, lv, hv, mode);
266 #else
267       if (hv < 0)
268         {
269           d = (double) (~ hv);
270           d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
271                 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
272           d += (double) (unsigned HOST_WIDE_INT) (~ lv);
273           d = (- d - 1.0);
274         }
275       else
276         {
277           d = (double) hv;
278           d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
279                 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
280           d += (double) (unsigned HOST_WIDE_INT) lv;
281         }
282 #endif  /* REAL_ARITHMETIC */
283       d = real_value_truncate (mode, d);
284       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
285     }
286   else if (code == UNSIGNED_FLOAT && GET_MODE (op) == VOIDmode
287            && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
288     {
289       HOST_WIDE_INT hv, lv;
290       REAL_VALUE_TYPE d;
291
292       if (GET_CODE (op) == CONST_INT)
293         lv = INTVAL (op), hv = HWI_SIGN_EXTEND (lv);
294       else
295         lv = CONST_DOUBLE_LOW (op),  hv = CONST_DOUBLE_HIGH (op);
296
297       if (op_mode == VOIDmode)
298         {
299           /* We don't know how to interpret negative-looking numbers in
300              this case, so don't try to fold those.  */
301           if (hv < 0)
302             return 0;
303         }
304       else if (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT * 2)
305         ;
306       else
307         hv = 0, lv &= GET_MODE_MASK (op_mode);
308
309 #ifdef REAL_ARITHMETIC
310       REAL_VALUE_FROM_UNSIGNED_INT (d, lv, hv, mode);
311 #else
312
313       d = (double) (unsigned HOST_WIDE_INT) hv;
314       d *= ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
315             * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
316       d += (double) (unsigned HOST_WIDE_INT) lv;
317 #endif  /* REAL_ARITHMETIC */
318       d = real_value_truncate (mode, d);
319       return CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
320     }
321 #endif
322
323   if (GET_CODE (op) == CONST_INT
324       && width <= HOST_BITS_PER_WIDE_INT && width > 0)
325     {
326       register HOST_WIDE_INT arg0 = INTVAL (op);
327       register HOST_WIDE_INT val;
328
329       switch (code)
330         {
331         case NOT:
332           val = ~ arg0;
333           break;
334
335         case NEG:
336           val = - arg0;
337           break;
338
339         case ABS:
340           val = (arg0 >= 0 ? arg0 : - arg0);
341           break;
342
343         case FFS:
344           /* Don't use ffs here.  Instead, get low order bit and then its
345              number.  If arg0 is zero, this will return 0, as desired.  */
346           arg0 &= GET_MODE_MASK (mode);
347           val = exact_log2 (arg0 & (- arg0)) + 1;
348           break;
349
350         case TRUNCATE:
351           val = arg0;
352           break;
353
354         case ZERO_EXTEND:
355           if (op_mode == VOIDmode)
356             op_mode = mode;
357           if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
358             {
359               /* If we were really extending the mode,
360                  we would have to distinguish between zero-extension
361                  and sign-extension.  */
362               if (width != GET_MODE_BITSIZE (op_mode))
363                 abort ();
364               val = arg0;
365             }
366           else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
367             val = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
368           else
369             return 0;
370           break;
371
372         case SIGN_EXTEND:
373           if (op_mode == VOIDmode)
374             op_mode = mode;
375           if (GET_MODE_BITSIZE (op_mode) == HOST_BITS_PER_WIDE_INT)
376             {
377               /* If we were really extending the mode,
378                  we would have to distinguish between zero-extension
379                  and sign-extension.  */
380               if (width != GET_MODE_BITSIZE (op_mode))
381                 abort ();
382               val = arg0;
383             }
384           else if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT)
385             {
386               val
387                 = arg0 & ~((HOST_WIDE_INT) (-1) << GET_MODE_BITSIZE (op_mode));
388               if (val
389                   & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (op_mode) - 1)))
390                 val -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
391             }
392           else
393             return 0;
394           break;
395
396         case SQRT:
397         case FLOAT_EXTEND:
398         case FLOAT_TRUNCATE:
399           return 0;
400
401         default:
402           abort ();
403         }
404
405       val = trunc_int_for_mode (val, mode);
406
407       return GEN_INT (val);
408     }
409
410   /* We can do some operations on integer CONST_DOUBLEs.  Also allow
411      for a DImode operation on a CONST_INT.  */
412   else if (GET_MODE (op) == VOIDmode && width <= HOST_BITS_PER_INT * 2
413            && (GET_CODE (op) == CONST_DOUBLE || GET_CODE (op) == CONST_INT))
414     {
415       unsigned HOST_WIDE_INT l1, lv;
416       HOST_WIDE_INT h1, hv;
417
418       if (GET_CODE (op) == CONST_DOUBLE)
419         l1 = CONST_DOUBLE_LOW (op), h1 = CONST_DOUBLE_HIGH (op);
420       else
421         l1 = INTVAL (op), h1 = HWI_SIGN_EXTEND (l1);
422
423       switch (code)
424         {
425         case NOT:
426           lv = ~ l1;
427           hv = ~ h1;
428           break;
429
430         case NEG:
431           neg_double (l1, h1, &lv, &hv);
432           break;
433
434         case ABS:
435           if (h1 < 0)
436             neg_double (l1, h1, &lv, &hv);
437           else
438             lv = l1, hv = h1;
439           break;
440
441         case FFS:
442           hv = 0;
443           if (l1 == 0)
444             lv = HOST_BITS_PER_WIDE_INT + exact_log2 (h1 & (-h1)) + 1;
445           else
446             lv = exact_log2 (l1 & (-l1)) + 1;
447           break;
448
449         case TRUNCATE:
450           /* This is just a change-of-mode, so do nothing.  */
451           lv = l1, hv = h1;
452           break;
453
454         case ZERO_EXTEND:
455           if (op_mode == VOIDmode
456               || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
457             return 0;
458
459           hv = 0;
460           lv = l1 & GET_MODE_MASK (op_mode);
461           break;
462
463         case SIGN_EXTEND:
464           if (op_mode == VOIDmode
465               || GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT)
466             return 0;
467           else
468             {
469               lv = l1 & GET_MODE_MASK (op_mode);
470               if (GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
471                   && (lv & ((HOST_WIDE_INT) 1
472                             << (GET_MODE_BITSIZE (op_mode) - 1))) != 0)
473                 lv -= (HOST_WIDE_INT) 1 << GET_MODE_BITSIZE (op_mode);
474
475               hv = HWI_SIGN_EXTEND (lv);
476             }
477           break;
478
479         case SQRT:
480           return 0;
481
482         default:
483           return 0;
484         }
485
486       return immed_double_const (lv, hv, mode);
487     }
488
489 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
490   else if (GET_CODE (op) == CONST_DOUBLE
491            && GET_MODE_CLASS (mode) == MODE_FLOAT)
492     {
493       REAL_VALUE_TYPE d;
494       jmp_buf handler;
495       rtx x;
496
497       if (setjmp (handler))
498         /* There used to be a warning here, but that is inadvisable.
499            People may want to cause traps, and the natural way
500            to do it should not get a warning.  */
501         return 0;
502
503       set_float_handler (handler);
504
505       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
506
507       switch (code)
508         {
509         case NEG:
510           d = REAL_VALUE_NEGATE (d);
511           break;
512
513         case ABS:
514           if (REAL_VALUE_NEGATIVE (d))
515             d = REAL_VALUE_NEGATE (d);
516           break;
517
518         case FLOAT_TRUNCATE:
519           d = real_value_truncate (mode, d);
520           break;
521
522         case FLOAT_EXTEND:
523           /* All this does is change the mode.  */
524           break;
525
526         case FIX:
527           d = REAL_VALUE_RNDZINT (d);
528           break;
529
530         case UNSIGNED_FIX:
531           d = REAL_VALUE_UNSIGNED_RNDZINT (d);
532           break;
533
534         case SQRT:
535           return 0;
536
537         default:
538           abort ();
539         }
540
541       x = CONST_DOUBLE_FROM_REAL_VALUE (d, mode);
542       set_float_handler (NULL_PTR);
543       return x;
544     }
545
546   else if (GET_CODE (op) == CONST_DOUBLE
547            && GET_MODE_CLASS (GET_MODE (op)) == MODE_FLOAT
548            && GET_MODE_CLASS (mode) == MODE_INT
549            && width <= HOST_BITS_PER_WIDE_INT && width > 0)
550     {
551       REAL_VALUE_TYPE d;
552       jmp_buf handler;
553       HOST_WIDE_INT val;
554
555       if (setjmp (handler))
556         return 0;
557
558       set_float_handler (handler);
559
560       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
561
562       switch (code)
563         {
564         case FIX:
565           val = REAL_VALUE_FIX (d);
566           break;
567
568         case UNSIGNED_FIX:
569           val = REAL_VALUE_UNSIGNED_FIX (d);
570           break;
571
572         default:
573           abort ();
574         }
575
576       set_float_handler (NULL_PTR);
577
578       val = trunc_int_for_mode (val, mode);
579
580       return GEN_INT (val);
581     }
582 #endif
583   /* This was formerly used only for non-IEEE float.
584      eggert@twinsun.com says it is safe for IEEE also.  */
585   else
586     {
587       enum rtx_code reversed;
588       /* There are some simplifications we can do even if the operands
589          aren't constant.  */
590       switch (code)
591         {
592         case NOT:
593           /* (not (not X)) == X.  */
594           if (GET_CODE (op) == NOT)
595             return XEXP (op, 0);
596
597           /* (not (eq X Y)) == (ne X Y), etc.  */
598           if (mode == BImode && GET_RTX_CLASS (GET_CODE (op)) == '<'
599               && ((reversed = reversed_comparison_code (op, NULL_RTX))
600                   != UNKNOWN))
601             return gen_rtx_fmt_ee (reversed,
602                                    op_mode, XEXP (op, 0), XEXP (op, 1));
603           break;
604
605         case NEG:
606           /* (neg (neg X)) == X.  */
607           if (GET_CODE (op) == NEG)
608             return XEXP (op, 0);
609           break;
610
611         case SIGN_EXTEND:
612           /* (sign_extend (truncate (minus (label_ref L1) (label_ref L2))))
613              becomes just the MINUS if its mode is MODE.  This allows
614              folding switch statements on machines using casesi (such as
615              the Vax).  */
616           if (GET_CODE (op) == TRUNCATE
617               && GET_MODE (XEXP (op, 0)) == mode
618               && GET_CODE (XEXP (op, 0)) == MINUS
619               && GET_CODE (XEXP (XEXP (op, 0), 0)) == LABEL_REF
620               && GET_CODE (XEXP (XEXP (op, 0), 1)) == LABEL_REF)
621             return XEXP (op, 0);
622
623 #ifdef POINTERS_EXTEND_UNSIGNED
624           if (! POINTERS_EXTEND_UNSIGNED
625               && mode == Pmode && GET_MODE (op) == ptr_mode
626               && CONSTANT_P (op))
627             return convert_memory_address (Pmode, op);
628 #endif
629           break;
630
631 #ifdef POINTERS_EXTEND_UNSIGNED
632         case ZERO_EXTEND:
633           if (POINTERS_EXTEND_UNSIGNED
634               && mode == Pmode && GET_MODE (op) == ptr_mode
635               && CONSTANT_P (op))
636             return convert_memory_address (Pmode, op);
637           break;
638 #endif
639           
640         default:
641           break;
642         }
643
644       return 0;
645     }
646 }
647 \f
648 /* Simplify a binary operation CODE with result mode MODE, operating on OP0
649    and OP1.  Return 0 if no simplification is possible.
650
651    Don't use this for relational operations such as EQ or LT.
652    Use simplify_relational_operation instead.  */
653
654 rtx
655 simplify_binary_operation (code, mode, op0, op1)
656      enum rtx_code code;
657      enum machine_mode mode;
658      rtx op0, op1;
659 {
660   register HOST_WIDE_INT arg0, arg1, arg0s, arg1s;
661   HOST_WIDE_INT val;
662   unsigned int width = GET_MODE_BITSIZE (mode);
663   rtx tem;
664
665   /* Relational operations don't work here.  We must know the mode
666      of the operands in order to do the comparison correctly.
667      Assuming a full word can give incorrect results.
668      Consider comparing 128 with -128 in QImode.  */
669
670   if (GET_RTX_CLASS (code) == '<')
671     abort ();
672
673 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
674   if (GET_MODE_CLASS (mode) == MODE_FLOAT
675       && GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE
676       && mode == GET_MODE (op0) && mode == GET_MODE (op1))
677     {
678       REAL_VALUE_TYPE f0, f1, value;
679       jmp_buf handler;
680
681       if (setjmp (handler))
682         return 0;
683
684       set_float_handler (handler);
685
686       REAL_VALUE_FROM_CONST_DOUBLE (f0, op0);
687       REAL_VALUE_FROM_CONST_DOUBLE (f1, op1);
688       f0 = real_value_truncate (mode, f0);
689       f1 = real_value_truncate (mode, f1);
690
691 #ifdef REAL_ARITHMETIC
692 #ifndef REAL_INFINITY
693       if (code == DIV && REAL_VALUES_EQUAL (f1, dconst0))
694         return 0;
695 #endif
696       REAL_ARITHMETIC (value, rtx_to_tree_code (code), f0, f1);
697 #else
698       switch (code)
699         {
700         case PLUS:
701           value = f0 + f1;
702           break;
703         case MINUS:
704           value = f0 - f1;
705           break;
706         case MULT:
707           value = f0 * f1;
708           break;
709         case DIV:
710 #ifndef REAL_INFINITY
711           if (f1 == 0)
712             return 0;
713 #endif
714           value = f0 / f1;
715           break;
716         case SMIN:
717           value = MIN (f0, f1);
718           break;
719         case SMAX:
720           value = MAX (f0, f1);
721           break;
722         default:
723           abort ();
724         }
725 #endif
726
727       value = real_value_truncate (mode, value);
728       set_float_handler (NULL_PTR);
729       return CONST_DOUBLE_FROM_REAL_VALUE (value, mode);
730     }
731 #endif  /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
732
733   /* We can fold some multi-word operations.  */
734   if (GET_MODE_CLASS (mode) == MODE_INT
735       && width == HOST_BITS_PER_WIDE_INT * 2
736       && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT)
737       && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT))
738     {
739       unsigned HOST_WIDE_INT l1, l2, lv;
740       HOST_WIDE_INT h1, h2, hv;
741
742       if (GET_CODE (op0) == CONST_DOUBLE)
743         l1 = CONST_DOUBLE_LOW (op0), h1 = CONST_DOUBLE_HIGH (op0);
744       else
745         l1 = INTVAL (op0), h1 = HWI_SIGN_EXTEND (l1);
746
747       if (GET_CODE (op1) == CONST_DOUBLE)
748         l2 = CONST_DOUBLE_LOW (op1), h2 = CONST_DOUBLE_HIGH (op1);
749       else
750         l2 = INTVAL (op1), h2 = HWI_SIGN_EXTEND (l2);
751
752       switch (code)
753         {
754         case MINUS:
755           /* A - B == A + (-B).  */
756           neg_double (l2, h2, &lv, &hv);
757           l2 = lv, h2 = hv;
758
759           /* .. fall through ...  */
760
761         case PLUS:
762           add_double (l1, h1, l2, h2, &lv, &hv);
763           break;
764
765         case MULT:
766           mul_double (l1, h1, l2, h2, &lv, &hv);
767           break;
768
769         case DIV:  case MOD:   case UDIV:  case UMOD:
770           /* We'd need to include tree.h to do this and it doesn't seem worth
771              it.  */
772           return 0;
773
774         case AND:
775           lv = l1 & l2, hv = h1 & h2;
776           break;
777
778         case IOR:
779           lv = l1 | l2, hv = h1 | h2;
780           break;
781
782         case XOR:
783           lv = l1 ^ l2, hv = h1 ^ h2;
784           break;
785
786         case SMIN:
787           if (h1 < h2
788               || (h1 == h2
789                   && ((unsigned HOST_WIDE_INT) l1
790                       < (unsigned HOST_WIDE_INT) l2)))
791             lv = l1, hv = h1;
792           else
793             lv = l2, hv = h2;
794           break;
795
796         case SMAX:
797           if (h1 > h2
798               || (h1 == h2
799                   && ((unsigned HOST_WIDE_INT) l1
800                       > (unsigned HOST_WIDE_INT) l2)))
801             lv = l1, hv = h1;
802           else
803             lv = l2, hv = h2;
804           break;
805
806         case UMIN:
807           if ((unsigned HOST_WIDE_INT) h1 < (unsigned HOST_WIDE_INT) h2
808               || (h1 == h2
809                   && ((unsigned HOST_WIDE_INT) l1
810                       < (unsigned HOST_WIDE_INT) l2)))
811             lv = l1, hv = h1;
812           else
813             lv = l2, hv = h2;
814           break;
815
816         case UMAX:
817           if ((unsigned HOST_WIDE_INT) h1 > (unsigned HOST_WIDE_INT) h2
818               || (h1 == h2
819                   && ((unsigned HOST_WIDE_INT) l1
820                       > (unsigned HOST_WIDE_INT) l2)))
821             lv = l1, hv = h1;
822           else
823             lv = l2, hv = h2;
824           break;
825
826         case LSHIFTRT:   case ASHIFTRT:
827         case ASHIFT:
828         case ROTATE:     case ROTATERT:
829 #ifdef SHIFT_COUNT_TRUNCATED
830           if (SHIFT_COUNT_TRUNCATED)
831             l2 &= (GET_MODE_BITSIZE (mode) - 1), h2 = 0;
832 #endif
833
834           if (h2 != 0 || l2 >= GET_MODE_BITSIZE (mode))
835             return 0;
836
837           if (code == LSHIFTRT || code == ASHIFTRT)
838             rshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv,
839                            code == ASHIFTRT);
840           else if (code == ASHIFT)
841             lshift_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv, 1);
842           else if (code == ROTATE)
843             lrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
844           else /* code == ROTATERT */
845             rrotate_double (l1, h1, l2, GET_MODE_BITSIZE (mode), &lv, &hv);
846           break;
847
848         default:
849           return 0;
850         }
851
852       return immed_double_const (lv, hv, mode);
853     }
854
855   if (GET_CODE (op0) != CONST_INT || GET_CODE (op1) != CONST_INT
856       || width > HOST_BITS_PER_WIDE_INT || width == 0)
857     {
858       /* Even if we can't compute a constant result,
859          there are some cases worth simplifying.  */
860
861       switch (code)
862         {
863         case PLUS:
864           /* In IEEE floating point, x+0 is not the same as x.  Similarly
865              for the other optimizations below.  */
866           if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
867               && FLOAT_MODE_P (mode) && ! flag_fast_math)
868             break;
869
870           if (op1 == CONST0_RTX (mode))
871             return op0;
872
873           /* ((-a) + b) -> (b - a) and similarly for (a + (-b)) */
874           if (GET_CODE (op0) == NEG)
875             return simplify_gen_binary (MINUS, mode, op1, XEXP (op0, 0));
876           else if (GET_CODE (op1) == NEG)
877             return simplify_gen_binary (MINUS, mode, op0, XEXP (op1, 0));
878
879           /* Handle both-operands-constant cases.  We can only add
880              CONST_INTs to constants since the sum of relocatable symbols
881              can't be handled by most assemblers.  Don't add CONST_INT
882              to CONST_INT since overflow won't be computed properly if wider
883              than HOST_BITS_PER_WIDE_INT.  */
884
885           if (CONSTANT_P (op0) && GET_MODE (op0) != VOIDmode
886               && GET_CODE (op1) == CONST_INT)
887             return plus_constant (op0, INTVAL (op1));
888           else if (CONSTANT_P (op1) && GET_MODE (op1) != VOIDmode
889                    && GET_CODE (op0) == CONST_INT)
890             return plus_constant (op1, INTVAL (op0));
891
892           /* See if this is something like X * C - X or vice versa or
893              if the multiplication is written as a shift.  If so, we can
894              distribute and make a new multiply, shift, or maybe just
895              have X (if C is 2 in the example above).  But don't make
896              real multiply if we didn't have one before.  */
897
898           if (! FLOAT_MODE_P (mode))
899             {
900               HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
901               rtx lhs = op0, rhs = op1;
902               int had_mult = 0;
903
904               if (GET_CODE (lhs) == NEG)
905                 coeff0 = -1, lhs = XEXP (lhs, 0);
906               else if (GET_CODE (lhs) == MULT
907                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
908                 {
909                   coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
910                   had_mult = 1;
911                 }
912               else if (GET_CODE (lhs) == ASHIFT
913                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT
914                        && INTVAL (XEXP (lhs, 1)) >= 0
915                        && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
916                 {
917                   coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
918                   lhs = XEXP (lhs, 0);
919                 }
920
921               if (GET_CODE (rhs) == NEG)
922                 coeff1 = -1, rhs = XEXP (rhs, 0);
923               else if (GET_CODE (rhs) == MULT
924                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
925                 {
926                   coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
927                   had_mult = 1;
928                 }
929               else if (GET_CODE (rhs) == ASHIFT
930                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT
931                        && INTVAL (XEXP (rhs, 1)) >= 0
932                        && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
933                 {
934                   coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
935                   rhs = XEXP (rhs, 0);
936                 }
937
938               if (rtx_equal_p (lhs, rhs))
939                 {
940                   tem = simplify_gen_binary (MULT, mode, lhs,
941                                         GEN_INT (coeff0 + coeff1));
942                   return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
943                 }
944             }
945
946           /* If one of the operands is a PLUS or a MINUS, see if we can
947              simplify this by the associative law. 
948              Don't use the associative law for floating point.
949              The inaccuracy makes it nonassociative,
950              and subtle programs can break if operations are associated.  */
951
952           if (INTEGRAL_MODE_P (mode)
953               && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
954                   || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS)
955               && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
956             return tem;
957           break;
958
959         case COMPARE:
960 #ifdef HAVE_cc0
961           /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
962              using cc0, in which case we want to leave it as a COMPARE
963              so we can distinguish it from a register-register-copy.
964
965              In IEEE floating point, x-0 is not the same as x.  */
966
967           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
968                || ! FLOAT_MODE_P (mode) || flag_fast_math)
969               && op1 == CONST0_RTX (mode))
970             return op0;
971 #endif
972
973           /* Convert (compare (gt (flags) 0) (lt (flags) 0)) to (flags).  */
974           if (((GET_CODE (op0) == GT && GET_CODE (op1) == LT)
975                || (GET_CODE (op0) == GTU && GET_CODE (op1) == LTU))
976               && XEXP (op0, 1) == const0_rtx && XEXP (op1, 1) == const0_rtx)
977             {
978               rtx xop00 = XEXP (op0, 0);
979               rtx xop10 = XEXP (op1, 0);
980
981 #ifdef HAVE_cc0
982               if (GET_CODE (xop00) == CC0 && GET_CODE (xop10) == CC0)
983 #else
984               if (GET_CODE (xop00) == REG && GET_CODE (xop10) == REG
985                   && GET_MODE (xop00) == GET_MODE (xop10)
986                   && REGNO (xop00) == REGNO (xop10)
987                   && GET_MODE_CLASS (GET_MODE (xop00)) == MODE_CC
988                   && GET_MODE_CLASS (GET_MODE (xop10)) == MODE_CC)
989 #endif
990                 return xop00;
991             }
992
993           break;              
994         case MINUS:
995           /* None of these optimizations can be done for IEEE
996              floating point.  */
997           if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
998               && FLOAT_MODE_P (mode) && ! flag_fast_math)
999             break;
1000
1001           /* We can't assume x-x is 0 even with non-IEEE floating point,
1002              but since it is zero except in very strange circumstances, we
1003              will treat it as zero with -ffast-math.  */
1004           if (rtx_equal_p (op0, op1)
1005               && ! side_effects_p (op0)
1006               && (! FLOAT_MODE_P (mode) || flag_fast_math))
1007             return CONST0_RTX (mode);
1008
1009           /* Change subtraction from zero into negation.  */
1010           if (op0 == CONST0_RTX (mode))
1011             return gen_rtx_NEG (mode, op1);
1012
1013           /* (-1 - a) is ~a.  */
1014           if (op0 == constm1_rtx)
1015             return gen_rtx_NOT (mode, op1);
1016
1017           /* Subtracting 0 has no effect.  */
1018           if (op1 == CONST0_RTX (mode))
1019             return op0;
1020
1021           /* See if this is something like X * C - X or vice versa or
1022              if the multiplication is written as a shift.  If so, we can
1023              distribute and make a new multiply, shift, or maybe just
1024              have X (if C is 2 in the example above).  But don't make
1025              real multiply if we didn't have one before.  */
1026
1027           if (! FLOAT_MODE_P (mode))
1028             {
1029               HOST_WIDE_INT coeff0 = 1, coeff1 = 1;
1030               rtx lhs = op0, rhs = op1;
1031               int had_mult = 0;
1032
1033               if (GET_CODE (lhs) == NEG)
1034                 coeff0 = -1, lhs = XEXP (lhs, 0);
1035               else if (GET_CODE (lhs) == MULT
1036                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT)
1037                 {
1038                   coeff0 = INTVAL (XEXP (lhs, 1)), lhs = XEXP (lhs, 0);
1039                   had_mult = 1;
1040                 }
1041               else if (GET_CODE (lhs) == ASHIFT
1042                        && GET_CODE (XEXP (lhs, 1)) == CONST_INT
1043                        && INTVAL (XEXP (lhs, 1)) >= 0
1044                        && INTVAL (XEXP (lhs, 1)) < HOST_BITS_PER_WIDE_INT)
1045                 {
1046                   coeff0 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (lhs, 1));
1047                   lhs = XEXP (lhs, 0);
1048                 }
1049
1050               if (GET_CODE (rhs) == NEG)
1051                 coeff1 = - 1, rhs = XEXP (rhs, 0);
1052               else if (GET_CODE (rhs) == MULT
1053                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT)
1054                 {
1055                   coeff1 = INTVAL (XEXP (rhs, 1)), rhs = XEXP (rhs, 0);
1056                   had_mult = 1;
1057                 }
1058               else if (GET_CODE (rhs) == ASHIFT
1059                        && GET_CODE (XEXP (rhs, 1)) == CONST_INT
1060                        && INTVAL (XEXP (rhs, 1)) >= 0
1061                        && INTVAL (XEXP (rhs, 1)) < HOST_BITS_PER_WIDE_INT)
1062                 {
1063                   coeff1 = ((HOST_WIDE_INT) 1) << INTVAL (XEXP (rhs, 1));
1064                   rhs = XEXP (rhs, 0);
1065                 }
1066
1067               if (rtx_equal_p (lhs, rhs))
1068                 {
1069                   tem = simplify_gen_binary (MULT, mode, lhs,
1070                                              GEN_INT (coeff0 - coeff1));
1071                   return (GET_CODE (tem) == MULT && ! had_mult) ? 0 : tem;
1072                 }
1073             }
1074
1075           /* (a - (-b)) -> (a + b).  */
1076           if (GET_CODE (op1) == NEG)
1077             return simplify_gen_binary (PLUS, mode, op0, XEXP (op1, 0));
1078
1079           /* If one of the operands is a PLUS or a MINUS, see if we can
1080              simplify this by the associative law. 
1081              Don't use the associative law for floating point.
1082              The inaccuracy makes it nonassociative,
1083              and subtle programs can break if operations are associated.  */
1084
1085           if (INTEGRAL_MODE_P (mode)
1086               && (GET_CODE (op0) == PLUS || GET_CODE (op0) == MINUS
1087                   || GET_CODE (op1) == PLUS || GET_CODE (op1) == MINUS)
1088               && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
1089             return tem;
1090
1091           /* Don't let a relocatable value get a negative coeff.  */
1092           if (GET_CODE (op1) == CONST_INT && GET_MODE (op0) != VOIDmode)
1093             return plus_constant (op0, - INTVAL (op1));
1094
1095           /* (x - (x & y)) -> (x & ~y) */
1096           if (GET_CODE (op1) == AND)
1097             {
1098              if (rtx_equal_p (op0, XEXP (op1, 0)))
1099                return simplify_gen_binary (AND, mode, op0,
1100                                            gen_rtx_NOT (mode, XEXP (op1, 1)));
1101              if (rtx_equal_p (op0, XEXP (op1, 1)))
1102                return simplify_gen_binary (AND, mode, op0,
1103                                            gen_rtx_NOT (mode, XEXP (op1, 0)));
1104            }
1105           break;
1106
1107         case MULT:
1108           if (op1 == constm1_rtx)
1109             {
1110               tem = simplify_unary_operation (NEG, mode, op0, mode);
1111
1112               return tem ? tem : gen_rtx_NEG (mode, op0);
1113             }
1114
1115           /* In IEEE floating point, x*0 is not always 0.  */
1116           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1117                || ! FLOAT_MODE_P (mode) || flag_fast_math)
1118               && op1 == CONST0_RTX (mode)
1119               && ! side_effects_p (op0))
1120             return op1;
1121
1122           /* In IEEE floating point, x*1 is not equivalent to x for nans.
1123              However, ANSI says we can drop signals,
1124              so we can do this anyway.  */
1125           if (op1 == CONST1_RTX (mode))
1126             return op0;
1127
1128           /* Convert multiply by constant power of two into shift unless
1129              we are still generating RTL.  This test is a kludge.  */
1130           if (GET_CODE (op1) == CONST_INT
1131               && (val = exact_log2 (INTVAL (op1))) >= 0
1132               /* If the mode is larger than the host word size, and the
1133                  uppermost bit is set, then this isn't a power of two due
1134                  to implicit sign extension.  */
1135               && (width <= HOST_BITS_PER_WIDE_INT
1136                   || val != HOST_BITS_PER_WIDE_INT - 1)
1137               && ! rtx_equal_function_value_matters)
1138             return gen_rtx_ASHIFT (mode, op0, GEN_INT (val));
1139
1140           if (GET_CODE (op1) == CONST_DOUBLE
1141               && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT)
1142             {
1143               REAL_VALUE_TYPE d;
1144               jmp_buf handler;
1145               int op1is2, op1ism1;
1146
1147               if (setjmp (handler))
1148                 return 0;
1149
1150               set_float_handler (handler);
1151               REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
1152               op1is2 = REAL_VALUES_EQUAL (d, dconst2);
1153               op1ism1 = REAL_VALUES_EQUAL (d, dconstm1);
1154               set_float_handler (NULL_PTR);
1155
1156               /* x*2 is x+x and x*(-1) is -x */
1157               if (op1is2 && GET_MODE (op0) == mode)
1158                 return gen_rtx_PLUS (mode, op0, copy_rtx (op0));
1159
1160               else if (op1ism1 && GET_MODE (op0) == mode)
1161                 return gen_rtx_NEG (mode, op0);
1162             }
1163           break;
1164
1165         case IOR:
1166           if (op1 == const0_rtx)
1167             return op0;
1168           if (GET_CODE (op1) == CONST_INT
1169               && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1170             return op1;
1171           if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1172             return op0;
1173           /* A | (~A) -> -1 */
1174           if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
1175                || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
1176               && ! side_effects_p (op0)
1177               && GET_MODE_CLASS (mode) != MODE_CC)
1178             return constm1_rtx;
1179           break;
1180
1181         case XOR:
1182           if (op1 == const0_rtx)
1183             return op0;
1184           if (GET_CODE (op1) == CONST_INT
1185               && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1186             return gen_rtx_NOT (mode, op0);
1187           if (op0 == op1 && ! side_effects_p (op0)
1188               && GET_MODE_CLASS (mode) != MODE_CC)
1189             return const0_rtx;
1190           break;
1191
1192         case AND:
1193           if (op1 == const0_rtx && ! side_effects_p (op0))
1194             return const0_rtx;
1195           if (GET_CODE (op1) == CONST_INT
1196               && (INTVAL (op1) & GET_MODE_MASK (mode)) == GET_MODE_MASK (mode))
1197             return op0;
1198           if (op0 == op1 && ! side_effects_p (op0)
1199               && GET_MODE_CLASS (mode) != MODE_CC)
1200             return op0;
1201           /* A & (~A) -> 0 */
1202           if (((GET_CODE (op0) == NOT && rtx_equal_p (XEXP (op0, 0), op1))
1203                || (GET_CODE (op1) == NOT && rtx_equal_p (XEXP (op1, 0), op0)))
1204               && ! side_effects_p (op0)
1205               && GET_MODE_CLASS (mode) != MODE_CC)
1206             return const0_rtx;
1207           break;
1208
1209         case UDIV:
1210           /* Convert divide by power of two into shift (divide by 1 handled
1211              below).  */
1212           if (GET_CODE (op1) == CONST_INT
1213               && (arg1 = exact_log2 (INTVAL (op1))) > 0)
1214             return gen_rtx_LSHIFTRT (mode, op0, GEN_INT (arg1));
1215
1216           /* ... fall through ...  */
1217
1218         case DIV:
1219           if (op1 == CONST1_RTX (mode))
1220             return op0;
1221
1222           /* In IEEE floating point, 0/x is not always 0.  */
1223           if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1224                || ! FLOAT_MODE_P (mode) || flag_fast_math)
1225               && op0 == CONST0_RTX (mode)
1226               && ! side_effects_p (op1))
1227             return op0;
1228
1229 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1230           /* Change division by a constant into multiplication.  Only do
1231              this with -ffast-math until an expert says it is safe in
1232              general.  */
1233           else if (GET_CODE (op1) == CONST_DOUBLE
1234                    && GET_MODE_CLASS (GET_MODE (op1)) == MODE_FLOAT
1235                    && op1 != CONST0_RTX (mode)
1236                    && flag_fast_math)
1237             {
1238               REAL_VALUE_TYPE d;
1239               REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
1240
1241               if (! REAL_VALUES_EQUAL (d, dconst0))
1242                 {
1243 #if defined (REAL_ARITHMETIC)
1244                   REAL_ARITHMETIC (d, rtx_to_tree_code (DIV), dconst1, d);
1245                   return gen_rtx_MULT (mode, op0, 
1246                                        CONST_DOUBLE_FROM_REAL_VALUE (d, mode));
1247 #else
1248                   return
1249                     gen_rtx_MULT (mode, op0, 
1250                                   CONST_DOUBLE_FROM_REAL_VALUE (1./d, mode));
1251 #endif
1252                 }
1253             }
1254 #endif
1255           break;
1256
1257         case UMOD:
1258           /* Handle modulus by power of two (mod with 1 handled below).  */
1259           if (GET_CODE (op1) == CONST_INT
1260               && exact_log2 (INTVAL (op1)) > 0)
1261             return gen_rtx_AND (mode, op0, GEN_INT (INTVAL (op1) - 1));
1262
1263           /* ... fall through ...  */
1264
1265         case MOD:
1266           if ((op0 == const0_rtx || op1 == const1_rtx)
1267               && ! side_effects_p (op0) && ! side_effects_p (op1))
1268             return const0_rtx;
1269           break;
1270
1271         case ROTATERT:
1272         case ROTATE:
1273           /* Rotating ~0 always results in ~0.  */
1274           if (GET_CODE (op0) == CONST_INT && width <= HOST_BITS_PER_WIDE_INT
1275               && (unsigned HOST_WIDE_INT) INTVAL (op0) == GET_MODE_MASK (mode)
1276               && ! side_effects_p (op1))
1277             return op0;
1278
1279           /* ... fall through ...  */
1280
1281         case ASHIFT:
1282         case ASHIFTRT:
1283         case LSHIFTRT:
1284           if (op1 == const0_rtx)
1285             return op0;
1286           if (op0 == const0_rtx && ! side_effects_p (op1))
1287             return op0;
1288           break;
1289
1290         case SMIN:
1291           if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT 
1292               && INTVAL (op1) == (HOST_WIDE_INT) 1 << (width -1)
1293               && ! side_effects_p (op0))
1294             return op1;
1295           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1296             return op0;
1297           break;
1298            
1299         case SMAX:
1300           if (width <= HOST_BITS_PER_WIDE_INT && GET_CODE (op1) == CONST_INT
1301               && ((unsigned HOST_WIDE_INT) INTVAL (op1)
1302                   == (unsigned HOST_WIDE_INT) GET_MODE_MASK (mode) >> 1)
1303               && ! side_effects_p (op0))
1304             return op1;
1305           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1306             return op0;
1307           break;
1308
1309         case UMIN:
1310           if (op1 == const0_rtx && ! side_effects_p (op0))
1311             return op1;
1312           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1313             return op0;
1314           break;
1315             
1316         case UMAX:
1317           if (op1 == constm1_rtx && ! side_effects_p (op0))
1318             return op1;
1319           else if (rtx_equal_p (op0, op1) && ! side_effects_p (op0))
1320             return op0;
1321           break;
1322
1323         default:
1324           abort ();
1325         }
1326       
1327       return 0;
1328     }
1329
1330   /* Get the integer argument values in two forms:
1331      zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S.  */
1332
1333   arg0 = INTVAL (op0);
1334   arg1 = INTVAL (op1);
1335
1336   if (width < HOST_BITS_PER_WIDE_INT)
1337     {
1338       arg0 &= ((HOST_WIDE_INT) 1 << width) - 1;
1339       arg1 &= ((HOST_WIDE_INT) 1 << width) - 1;
1340
1341       arg0s = arg0;
1342       if (arg0s & ((HOST_WIDE_INT) 1 << (width - 1)))
1343         arg0s |= ((HOST_WIDE_INT) (-1) << width);
1344
1345       arg1s = arg1;
1346       if (arg1s & ((HOST_WIDE_INT) 1 << (width - 1)))
1347         arg1s |= ((HOST_WIDE_INT) (-1) << width);
1348     }
1349   else
1350     {
1351       arg0s = arg0;
1352       arg1s = arg1;
1353     }
1354
1355   /* Compute the value of the arithmetic.  */
1356
1357   switch (code)
1358     {
1359     case PLUS:
1360       val = arg0s + arg1s;
1361       break;
1362
1363     case MINUS:
1364       val = arg0s - arg1s;
1365       break;
1366
1367     case MULT:
1368       val = arg0s * arg1s;
1369       break;
1370
1371     case DIV:
1372       if (arg1s == 0)
1373         return 0;
1374       val = arg0s / arg1s;
1375       break;
1376
1377     case MOD:
1378       if (arg1s == 0)
1379         return 0;
1380       val = arg0s % arg1s;
1381       break;
1382
1383     case UDIV:
1384       if (arg1 == 0)
1385         return 0;
1386       val = (unsigned HOST_WIDE_INT) arg0 / arg1;
1387       break;
1388
1389     case UMOD:
1390       if (arg1 == 0)
1391         return 0;
1392       val = (unsigned HOST_WIDE_INT) arg0 % arg1;
1393       break;
1394
1395     case AND:
1396       val = arg0 & arg1;
1397       break;
1398
1399     case IOR:
1400       val = arg0 | arg1;
1401       break;
1402
1403     case XOR:
1404       val = arg0 ^ arg1;
1405       break;
1406
1407     case LSHIFTRT:
1408       /* If shift count is undefined, don't fold it; let the machine do
1409          what it wants.  But truncate it if the machine will do that.  */
1410       if (arg1 < 0)
1411         return 0;
1412
1413 #ifdef SHIFT_COUNT_TRUNCATED
1414       if (SHIFT_COUNT_TRUNCATED)
1415         arg1 %= width;
1416 #endif
1417
1418       val = ((unsigned HOST_WIDE_INT) arg0) >> arg1;
1419       break;
1420
1421     case ASHIFT:
1422       if (arg1 < 0)
1423         return 0;
1424
1425 #ifdef SHIFT_COUNT_TRUNCATED
1426       if (SHIFT_COUNT_TRUNCATED)
1427         arg1 %= width;
1428 #endif
1429
1430       val = ((unsigned HOST_WIDE_INT) arg0) << arg1;
1431       break;
1432
1433     case ASHIFTRT:
1434       if (arg1 < 0)
1435         return 0;
1436
1437 #ifdef SHIFT_COUNT_TRUNCATED
1438       if (SHIFT_COUNT_TRUNCATED)
1439         arg1 %= width;
1440 #endif
1441
1442       val = arg0s >> arg1;
1443
1444       /* Bootstrap compiler may not have sign extended the right shift.
1445          Manually extend the sign to insure bootstrap cc matches gcc.  */
1446       if (arg0s < 0 && arg1 > 0)
1447         val |= ((HOST_WIDE_INT) -1) << (HOST_BITS_PER_WIDE_INT - arg1);
1448
1449       break;
1450
1451     case ROTATERT:
1452       if (arg1 < 0)
1453         return 0;
1454
1455       arg1 %= width;
1456       val = ((((unsigned HOST_WIDE_INT) arg0) << (width - arg1))
1457              | (((unsigned HOST_WIDE_INT) arg0) >> arg1));
1458       break;
1459
1460     case ROTATE:
1461       if (arg1 < 0)
1462         return 0;
1463
1464       arg1 %= width;
1465       val = ((((unsigned HOST_WIDE_INT) arg0) << arg1)
1466              | (((unsigned HOST_WIDE_INT) arg0) >> (width - arg1)));
1467       break;
1468
1469     case COMPARE:
1470       /* Do nothing here.  */
1471       return 0;
1472
1473     case SMIN:
1474       val = arg0s <= arg1s ? arg0s : arg1s;
1475       break;
1476
1477     case UMIN:
1478       val = ((unsigned HOST_WIDE_INT) arg0
1479              <= (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1480       break;
1481
1482     case SMAX:
1483       val = arg0s > arg1s ? arg0s : arg1s;
1484       break;
1485
1486     case UMAX:
1487       val = ((unsigned HOST_WIDE_INT) arg0
1488              > (unsigned HOST_WIDE_INT) arg1 ? arg0 : arg1);
1489       break;
1490
1491     default:
1492       abort ();
1493     }
1494
1495   val = trunc_int_for_mode (val, mode);
1496
1497   return GEN_INT (val);
1498 }
1499 \f
1500 /* Simplify a PLUS or MINUS, at least one of whose operands may be another
1501    PLUS or MINUS.
1502
1503    Rather than test for specific case, we do this by a brute-force method
1504    and do all possible simplifications until no more changes occur.  Then
1505    we rebuild the operation.  */
1506
1507 static rtx
1508 simplify_plus_minus (code, mode, op0, op1)
1509      enum rtx_code code;
1510      enum machine_mode mode;
1511      rtx op0, op1;
1512 {
1513   rtx ops[8];
1514   int negs[8];
1515   rtx result, tem;
1516   int n_ops = 2, input_ops = 2, input_consts = 0, n_consts = 0;
1517   int first = 1, negate = 0, changed;
1518   int i, j;
1519
1520   memset ((char *) ops, 0, sizeof ops);
1521   
1522   /* Set up the two operands and then expand them until nothing has been
1523      changed.  If we run out of room in our array, give up; this should
1524      almost never happen.  */
1525
1526   ops[0] = op0, ops[1] = op1, negs[0] = 0, negs[1] = (code == MINUS);
1527
1528   changed = 1;
1529   while (changed)
1530     {
1531       changed = 0;
1532
1533       for (i = 0; i < n_ops; i++)
1534         switch (GET_CODE (ops[i]))
1535           {
1536           case PLUS:
1537           case MINUS:
1538             if (n_ops == 7)
1539               return 0;
1540
1541             ops[n_ops] = XEXP (ops[i], 1);
1542             negs[n_ops++] = GET_CODE (ops[i]) == MINUS ? !negs[i] : negs[i];
1543             ops[i] = XEXP (ops[i], 0);
1544             input_ops++;
1545             changed = 1;
1546             break;
1547
1548           case NEG:
1549             ops[i] = XEXP (ops[i], 0);
1550             negs[i] = ! negs[i];
1551             changed = 1;
1552             break;
1553
1554           case CONST:
1555             ops[i] = XEXP (ops[i], 0);
1556             input_consts++;
1557             changed = 1;
1558             break;
1559
1560           case NOT:
1561             /* ~a -> (-a - 1) */
1562             if (n_ops != 7)
1563               {
1564                 ops[n_ops] = constm1_rtx;
1565                 negs[n_ops++] = negs[i];
1566                 ops[i] = XEXP (ops[i], 0);
1567                 negs[i] = ! negs[i];
1568                 changed = 1;
1569               }
1570             break;
1571
1572           case CONST_INT:
1573             if (negs[i])
1574               ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0, changed = 1;
1575             break;
1576
1577           default:
1578             break;
1579           }
1580     }
1581
1582   /* If we only have two operands, we can't do anything.  */
1583   if (n_ops <= 2)
1584     return 0;
1585
1586   /* Now simplify each pair of operands until nothing changes.  The first
1587      time through just simplify constants against each other.  */
1588
1589   changed = 1;
1590   while (changed)
1591     {
1592       changed = first;
1593
1594       for (i = 0; i < n_ops - 1; i++)
1595         for (j = i + 1; j < n_ops; j++)
1596           if (ops[i] != 0 && ops[j] != 0
1597               && (! first || (CONSTANT_P (ops[i]) && CONSTANT_P (ops[j]))))
1598             {
1599               rtx lhs = ops[i], rhs = ops[j];
1600               enum rtx_code ncode = PLUS;
1601
1602               if (negs[i] && ! negs[j])
1603                 lhs = ops[j], rhs = ops[i], ncode = MINUS;
1604               else if (! negs[i] && negs[j])
1605                 ncode = MINUS;
1606
1607               tem = simplify_binary_operation (ncode, mode, lhs, rhs);
1608               if (tem)
1609                 {
1610                   ops[i] = tem, ops[j] = 0;
1611                   negs[i] = negs[i] && negs[j];
1612                   if (GET_CODE (tem) == NEG)
1613                     ops[i] = XEXP (tem, 0), negs[i] = ! negs[i];
1614
1615                   if (GET_CODE (ops[i]) == CONST_INT && negs[i])
1616                     ops[i] = GEN_INT (- INTVAL (ops[i])), negs[i] = 0;
1617                   changed = 1;
1618                 }
1619             }
1620
1621       first = 0;
1622     }
1623
1624   /* Pack all the operands to the lower-numbered entries and give up if
1625      we didn't reduce the number of operands we had.  Make sure we
1626      count a CONST as two operands.  If we have the same number of
1627      operands, but have made more CONSTs than we had, this is also
1628      an improvement, so accept it.  */
1629
1630   for (i = 0, j = 0; j < n_ops; j++)
1631     if (ops[j] != 0)
1632       {
1633         ops[i] = ops[j], negs[i++] = negs[j];
1634         if (GET_CODE (ops[j]) == CONST)
1635           n_consts++;
1636       }
1637
1638   if (i + n_consts > input_ops
1639       || (i + n_consts == input_ops && n_consts <= input_consts))
1640     return 0;
1641
1642   n_ops = i;
1643
1644   /* If we have a CONST_INT, put it last.  */
1645   for (i = 0; i < n_ops - 1; i++)
1646     if (GET_CODE (ops[i]) == CONST_INT)
1647       {
1648         tem = ops[n_ops - 1], ops[n_ops - 1] = ops[i] , ops[i] = tem;
1649         j = negs[n_ops - 1], negs[n_ops - 1] = negs[i], negs[i] = j;
1650       }
1651
1652   /* Put a non-negated operand first.  If there aren't any, make all
1653      operands positive and negate the whole thing later.  */
1654   for (i = 0; i < n_ops && negs[i]; i++)
1655     ;
1656
1657   if (i == n_ops)
1658     {
1659       for (i = 0; i < n_ops; i++)
1660         negs[i] = 0;
1661       negate = 1;
1662     }
1663   else if (i != 0)
1664     {
1665       tem = ops[0], ops[0] = ops[i], ops[i] = tem;
1666       j = negs[0], negs[0] = negs[i], negs[i] = j;
1667     }
1668
1669   /* Now make the result by performing the requested operations.  */
1670   result = ops[0];
1671   for (i = 1; i < n_ops; i++)
1672     result = simplify_gen_binary (negs[i] ? MINUS : PLUS, mode, result, ops[i]);
1673
1674   return negate ? gen_rtx_NEG (mode, result) : result;
1675 }
1676
1677 struct cfc_args
1678 {
1679   rtx op0, op1;                 /* Input */
1680   int equal, op0lt, op1lt;      /* Output */
1681   int unordered;
1682 };
1683
1684 static void
1685 check_fold_consts (data)
1686   PTR data;
1687 {
1688   struct cfc_args *args = (struct cfc_args *) data;
1689   REAL_VALUE_TYPE d0, d1;
1690
1691   /* We may possibly raise an exception while reading the value.  */
1692   args->unordered = 1;
1693   REAL_VALUE_FROM_CONST_DOUBLE (d0, args->op0);
1694   REAL_VALUE_FROM_CONST_DOUBLE (d1, args->op1);
1695
1696   /* Comparisons of Inf versus Inf are ordered.  */
1697   if (REAL_VALUE_ISNAN (d0)
1698       || REAL_VALUE_ISNAN (d1))
1699     return;
1700   args->equal = REAL_VALUES_EQUAL (d0, d1);
1701   args->op0lt = REAL_VALUES_LESS (d0, d1);
1702   args->op1lt = REAL_VALUES_LESS (d1, d0);
1703   args->unordered = 0;
1704 }
1705
1706 /* Like simplify_binary_operation except used for relational operators.
1707    MODE is the mode of the operands, not that of the result.  If MODE
1708    is VOIDmode, both operands must also be VOIDmode and we compare the
1709    operands in "infinite precision".
1710
1711    If no simplification is possible, this function returns zero.  Otherwise,
1712    it returns either const_true_rtx or const0_rtx.  */
1713
1714 rtx
1715 simplify_relational_operation (code, mode, op0, op1)
1716      enum rtx_code code;
1717      enum machine_mode mode;
1718      rtx op0, op1;
1719 {
1720   int equal, op0lt, op0ltu, op1lt, op1ltu;
1721   rtx tem;
1722
1723   if (mode == VOIDmode
1724       && (GET_MODE (op0) != VOIDmode
1725           || GET_MODE (op1) != VOIDmode))
1726     abort ();
1727
1728   /* If op0 is a compare, extract the comparison arguments from it.  */
1729   if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
1730     op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
1731
1732   /* We can't simplify MODE_CC values since we don't know what the
1733      actual comparison is.  */
1734   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC
1735 #ifdef HAVE_cc0
1736       || op0 == cc0_rtx
1737 #endif
1738       )
1739     return 0;
1740
1741   /* Make sure the constant is second.  */
1742   if ((CONSTANT_P (op0) && ! CONSTANT_P (op1))
1743       || (GET_CODE (op0) == CONST_INT && GET_CODE (op1) != CONST_INT))
1744     {
1745       tem = op0, op0 = op1, op1 = tem;
1746       code = swap_condition (code);
1747     }
1748
1749   /* For integer comparisons of A and B maybe we can simplify A - B and can
1750      then simplify a comparison of that with zero.  If A and B are both either
1751      a register or a CONST_INT, this can't help; testing for these cases will
1752      prevent infinite recursion here and speed things up.
1753
1754      If CODE is an unsigned comparison, then we can never do this optimization,
1755      because it gives an incorrect result if the subtraction wraps around zero.
1756      ANSI C defines unsigned operations such that they never overflow, and
1757      thus such cases can not be ignored.  */
1758
1759   if (INTEGRAL_MODE_P (mode) && op1 != const0_rtx
1760       && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == CONST_INT)
1761             && (GET_CODE (op1) == REG || GET_CODE (op1) == CONST_INT))
1762       && 0 != (tem = simplify_binary_operation (MINUS, mode, op0, op1))
1763       && code != GTU && code != GEU && code != LTU && code != LEU)
1764     return simplify_relational_operation (signed_condition (code),
1765                                           mode, tem, const0_rtx);
1766
1767   if (flag_fast_math && code == ORDERED)
1768     return const_true_rtx;
1769
1770   if (flag_fast_math && code == UNORDERED)
1771     return const0_rtx;
1772
1773   /* For non-IEEE floating-point, if the two operands are equal, we know the
1774      result.  */
1775   if (rtx_equal_p (op0, op1)
1776       && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
1777           || ! FLOAT_MODE_P (GET_MODE (op0)) || flag_fast_math))
1778     equal = 1, op0lt = 0, op0ltu = 0, op1lt = 0, op1ltu = 0;
1779
1780   /* If the operands are floating-point constants, see if we can fold
1781      the result.  */
1782 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1783   else if (GET_CODE (op0) == CONST_DOUBLE && GET_CODE (op1) == CONST_DOUBLE
1784            && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
1785     {
1786       struct cfc_args args;
1787
1788       /* Setup input for check_fold_consts() */
1789       args.op0 = op0;
1790       args.op1 = op1;
1791       
1792       
1793       if (!do_float_handler(check_fold_consts, (PTR) &args))
1794         args.unordered = 1;
1795
1796       if (args.unordered)
1797         switch (code)
1798           {
1799           case UNEQ:
1800           case UNLT:
1801           case UNGT:
1802           case UNLE:
1803           case UNGE:
1804           case NE:
1805           case UNORDERED:
1806             return const_true_rtx;
1807           case EQ:
1808           case LT:
1809           case GT:
1810           case LE:
1811           case GE:
1812           case LTGT:
1813           case ORDERED:
1814             return const0_rtx;
1815           default:
1816             return 0;
1817           }
1818
1819       /* Receive output from check_fold_consts() */
1820       equal = args.equal;
1821       op0lt = op0ltu = args.op0lt;
1822       op1lt = op1ltu = args.op1lt;
1823     }
1824 #endif  /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1825
1826   /* Otherwise, see if the operands are both integers.  */
1827   else if ((GET_MODE_CLASS (mode) == MODE_INT || mode == VOIDmode)
1828            && (GET_CODE (op0) == CONST_DOUBLE || GET_CODE (op0) == CONST_INT)
1829            && (GET_CODE (op1) == CONST_DOUBLE || GET_CODE (op1) == CONST_INT))
1830     {
1831       int width = GET_MODE_BITSIZE (mode);
1832       HOST_WIDE_INT l0s, h0s, l1s, h1s;
1833       unsigned HOST_WIDE_INT l0u, h0u, l1u, h1u;
1834
1835       /* Get the two words comprising each integer constant.  */
1836       if (GET_CODE (op0) == CONST_DOUBLE)
1837         {
1838           l0u = l0s = CONST_DOUBLE_LOW (op0);
1839           h0u = h0s = CONST_DOUBLE_HIGH (op0);
1840         }
1841       else
1842         {
1843           l0u = l0s = INTVAL (op0);
1844           h0u = h0s = HWI_SIGN_EXTEND (l0s);
1845         }
1846           
1847       if (GET_CODE (op1) == CONST_DOUBLE)
1848         {
1849           l1u = l1s = CONST_DOUBLE_LOW (op1);
1850           h1u = h1s = CONST_DOUBLE_HIGH (op1);
1851         }
1852       else
1853         {
1854           l1u = l1s = INTVAL (op1);
1855           h1u = h1s = HWI_SIGN_EXTEND (l1s);
1856         }
1857
1858       /* If WIDTH is nonzero and smaller than HOST_BITS_PER_WIDE_INT,
1859          we have to sign or zero-extend the values.  */
1860       if (width != 0 && width < HOST_BITS_PER_WIDE_INT)
1861         {
1862           l0u &= ((HOST_WIDE_INT) 1 << width) - 1;
1863           l1u &= ((HOST_WIDE_INT) 1 << width) - 1;
1864
1865           if (l0s & ((HOST_WIDE_INT) 1 << (width - 1)))
1866             l0s |= ((HOST_WIDE_INT) (-1) << width);
1867
1868           if (l1s & ((HOST_WIDE_INT) 1 << (width - 1)))
1869             l1s |= ((HOST_WIDE_INT) (-1) << width);
1870         }
1871       if (width != 0 && width <= HOST_BITS_PER_WIDE_INT)
1872         h0u = h1u = 0, h0s = HWI_SIGN_EXTEND (l0s), h1s = HWI_SIGN_EXTEND (l1s);
1873
1874       equal = (h0u == h1u && l0u == l1u);
1875       op0lt = (h0s < h1s || (h0s == h1s && l0u < l1u));
1876       op1lt = (h1s < h0s || (h1s == h0s && l1u < l0u));
1877       op0ltu = (h0u < h1u || (h0u == h1u && l0u < l1u));
1878       op1ltu = (h1u < h0u || (h1u == h0u && l1u < l0u));
1879     }
1880
1881   /* Otherwise, there are some code-specific tests we can make.  */
1882   else
1883     {
1884       switch (code)
1885         {
1886         case EQ:
1887           /* References to the frame plus a constant or labels cannot
1888              be zero, but a SYMBOL_REF can due to #pragma weak.  */
1889           if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx)
1890                || GET_CODE (op0) == LABEL_REF)
1891 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1892               /* On some machines, the ap reg can be 0 sometimes.  */
1893               && op0 != arg_pointer_rtx
1894 #endif
1895                 )
1896             return const0_rtx;
1897           break;
1898
1899         case NE:
1900           if (((NONZERO_BASE_PLUS_P (op0) && op1 == const0_rtx)
1901                || GET_CODE (op0) == LABEL_REF)
1902 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1903               && op0 != arg_pointer_rtx
1904 #endif
1905               )
1906             return const_true_rtx;
1907           break;
1908
1909         case GEU:
1910           /* Unsigned values are never negative.  */
1911           if (op1 == const0_rtx)
1912             return const_true_rtx;
1913           break;
1914
1915         case LTU:
1916           if (op1 == const0_rtx)
1917             return const0_rtx;
1918           break;
1919
1920         case LEU:
1921           /* Unsigned values are never greater than the largest
1922              unsigned value.  */
1923           if (GET_CODE (op1) == CONST_INT
1924               && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
1925             && INTEGRAL_MODE_P (mode))
1926           return const_true_rtx;
1927           break;
1928
1929         case GTU:
1930           if (GET_CODE (op1) == CONST_INT
1931               && (unsigned HOST_WIDE_INT) INTVAL (op1) == GET_MODE_MASK (mode)
1932               && INTEGRAL_MODE_P (mode))
1933             return const0_rtx;
1934           break;
1935           
1936         default:
1937           break;
1938         }
1939
1940       return 0;
1941     }
1942
1943   /* If we reach here, EQUAL, OP0LT, OP0LTU, OP1LT, and OP1LTU are set
1944      as appropriate.  */
1945   switch (code)
1946     {
1947     case EQ:
1948     case UNEQ:
1949       return equal ? const_true_rtx : const0_rtx;
1950     case NE:
1951     case LTGT:
1952       return ! equal ? const_true_rtx : const0_rtx;
1953     case LT:
1954     case UNLT:
1955       return op0lt ? const_true_rtx : const0_rtx;
1956     case GT:
1957     case UNGT:
1958       return op1lt ? const_true_rtx : const0_rtx;
1959     case LTU:
1960       return op0ltu ? const_true_rtx : const0_rtx;
1961     case GTU:
1962       return op1ltu ? const_true_rtx : const0_rtx;
1963     case LE:
1964     case UNLE:
1965       return equal || op0lt ? const_true_rtx : const0_rtx;
1966     case GE:
1967     case UNGE:
1968       return equal || op1lt ? const_true_rtx : const0_rtx;
1969     case LEU:
1970       return equal || op0ltu ? const_true_rtx : const0_rtx;
1971     case GEU:
1972       return equal || op1ltu ? const_true_rtx : const0_rtx;
1973     case ORDERED:
1974       return const_true_rtx;
1975     case UNORDERED:
1976       return const0_rtx;
1977     default:
1978       abort ();
1979     }
1980 }
1981 \f
1982 /* Simplify CODE, an operation with result mode MODE and three operands,
1983    OP0, OP1, and OP2.  OP0_MODE was the mode of OP0 before it became
1984    a constant.  Return 0 if no simplifications is possible.  */
1985
1986 rtx
1987 simplify_ternary_operation (code, mode, op0_mode, op0, op1, op2)
1988      enum rtx_code code;
1989      enum machine_mode mode, op0_mode;
1990      rtx op0, op1, op2;
1991 {
1992   unsigned int width = GET_MODE_BITSIZE (mode);
1993
1994   /* VOIDmode means "infinite" precision.  */
1995   if (width == 0)
1996     width = HOST_BITS_PER_WIDE_INT;
1997
1998   switch (code)
1999     {
2000     case SIGN_EXTRACT:
2001     case ZERO_EXTRACT:
2002       if (GET_CODE (op0) == CONST_INT
2003           && GET_CODE (op1) == CONST_INT
2004           && GET_CODE (op2) == CONST_INT
2005           && ((unsigned) INTVAL (op1) + (unsigned) INTVAL (op2) <= width)
2006           && width <= (unsigned) HOST_BITS_PER_WIDE_INT)
2007         {
2008           /* Extracting a bit-field from a constant */
2009           HOST_WIDE_INT val = INTVAL (op0);
2010
2011           if (BITS_BIG_ENDIAN)
2012             val >>= (GET_MODE_BITSIZE (op0_mode)
2013                      - INTVAL (op2) - INTVAL (op1));
2014           else
2015             val >>= INTVAL (op2);
2016
2017           if (HOST_BITS_PER_WIDE_INT != INTVAL (op1))
2018             {
2019               /* First zero-extend.  */
2020               val &= ((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1;
2021               /* If desired, propagate sign bit.  */
2022               if (code == SIGN_EXTRACT
2023                   && (val & ((HOST_WIDE_INT) 1 << (INTVAL (op1) - 1))))
2024                 val |= ~ (((HOST_WIDE_INT) 1 << INTVAL (op1)) - 1);
2025             }
2026
2027           /* Clear the bits that don't belong in our mode,
2028              unless they and our sign bit are all one.
2029              So we get either a reasonable negative value or a reasonable
2030              unsigned value for this mode.  */
2031           if (width < HOST_BITS_PER_WIDE_INT
2032               && ((val & ((HOST_WIDE_INT) (-1) << (width - 1)))
2033                   != ((HOST_WIDE_INT) (-1) << (width - 1))))
2034             val &= ((HOST_WIDE_INT) 1 << width) - 1;
2035
2036           return GEN_INT (val);
2037         }
2038       break;
2039
2040     case IF_THEN_ELSE:
2041       if (GET_CODE (op0) == CONST_INT)
2042         return op0 != const0_rtx ? op1 : op2;
2043
2044       /* Convert a == b ? b : a to "a".  */
2045       if (GET_CODE (op0) == NE && ! side_effects_p (op0)
2046           && (! FLOAT_MODE_P (mode) || flag_fast_math)
2047           && rtx_equal_p (XEXP (op0, 0), op1)
2048           && rtx_equal_p (XEXP (op0, 1), op2))
2049         return op1;
2050       else if (GET_CODE (op0) == EQ && ! side_effects_p (op0)
2051           && (! FLOAT_MODE_P (mode) || flag_fast_math)
2052           && rtx_equal_p (XEXP (op0, 1), op1)
2053           && rtx_equal_p (XEXP (op0, 0), op2))
2054         return op2;
2055       else if (GET_RTX_CLASS (GET_CODE (op0)) == '<' && ! side_effects_p (op0))
2056         {
2057           enum machine_mode cmp_mode = (GET_MODE (XEXP (op0, 0)) == VOIDmode
2058                                         ? GET_MODE (XEXP (op0, 1))
2059                                         : GET_MODE (XEXP (op0, 0)));
2060           if (cmp_mode == VOIDmode)
2061             cmp_mode = op0_mode;
2062           rtx temp
2063              = simplify_relational_operation (GET_CODE (op0), cmp_mode,
2064                                               XEXP (op0, 0), XEXP (op0, 1));
2065
2066           /* See if any simplifications were possible.  */
2067           if (temp == const0_rtx)
2068             return op2;
2069           else if (temp == const1_rtx)
2070             return op1;
2071           else if (temp)
2072             op0 = temp;
2073
2074           /* Look for happy constants in op1 and op2.  */
2075           if (GET_CODE (op1) == CONST_INT && GET_CODE (op2) == CONST_INT)
2076             {
2077               HOST_WIDE_INT t = INTVAL (op1);
2078               HOST_WIDE_INT f = INTVAL (op2);
2079               
2080               if (t == STORE_FLAG_VALUE && f == 0)
2081                 code = GET_CODE (op0);
2082               else if (t == 0 && f == STORE_FLAG_VALUE)
2083                 {
2084                   enum rtx_code tmp;
2085                   tmp = reversed_comparison_code (op0, NULL_RTX);
2086                   if (tmp == UNKNOWN)
2087                     break;
2088                   code = tmp;
2089                 }
2090               else
2091                 break;
2092
2093               return gen_rtx_fmt_ee (code, mode, XEXP (op0, 0), XEXP (op0, 1));
2094             }
2095         }
2096       break;
2097
2098     default:
2099       abort ();
2100     }
2101
2102   return 0;
2103 }
2104
2105 /* Simplify X, an rtx expression.
2106
2107    Return the simplified expression or NULL if no simplifications
2108    were possible.
2109
2110    This is the preferred entry point into the simplification routines;
2111    however, we still allow passes to call the more specific routines.
2112
2113    Right now GCC has three (yes, three) major bodies of RTL simplficiation
2114    code that need to be unified.
2115
2116         1. fold_rtx in cse.c.  This code uses various CSE specific
2117            information to aid in RTL simplification.
2118
2119         2. simplify_rtx in combine.c.  Similar to fold_rtx, except that
2120            it uses combine specific information to aid in RTL
2121            simplification.
2122
2123         3. The routines in this file.
2124
2125
2126    Long term we want to only have one body of simplification code; to
2127    get to that state I recommend the following steps:
2128
2129         1. Pour over fold_rtx & simplify_rtx and move any simplifications
2130            which are not pass dependent state into these routines.
2131
2132         2. As code is moved by #1, change fold_rtx & simplify_rtx to
2133            use this routine whenever possible.
2134
2135         3. Allow for pass dependent state to be provided to these
2136            routines and add simplifications based on the pass dependent
2137            state.  Remove code from cse.c & combine.c that becomes
2138            redundant/dead.
2139
2140     It will take time, but ultimately the compiler will be easier to
2141     maintain and improve.  It's totally silly that when we add a
2142     simplification that it needs to be added to 4 places (3 for RTL
2143     simplification and 1 for tree simplification.  */
2144            
2145 rtx
2146 simplify_rtx (x)
2147      rtx x;
2148 {
2149   enum rtx_code code;
2150   enum machine_mode mode;
2151
2152   mode = GET_MODE (x);
2153   code = GET_CODE (x);
2154
2155   switch (GET_RTX_CLASS (code))
2156     {
2157     case '1':
2158       return simplify_unary_operation (code, mode,
2159                                        XEXP (x, 0), GET_MODE (XEXP (x, 0)));
2160     case '2':
2161     case 'c':
2162       return simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
2163
2164     case '3':
2165     case 'b':
2166       return simplify_ternary_operation (code, mode, GET_MODE (XEXP (x, 0)),
2167                                          XEXP (x, 0), XEXP (x, 1), XEXP (x, 2));
2168
2169     case '<':
2170       return simplify_relational_operation (code,
2171                                             (GET_MODE (XEXP (x, 0)) != VOIDmode
2172                                              ? GET_MODE (XEXP (x, 0))
2173                                              : GET_MODE (XEXP (x, 1))),
2174                                             XEXP (x, 0), XEXP (x, 1));
2175     default:
2176       return NULL;
2177     }
2178 }
2179 \f
2180
2181 /* Allocate a struct elt_list and fill in its two elements with the
2182    arguments.  */
2183
2184 static struct elt_list *
2185 new_elt_list (next, elt)
2186      struct elt_list *next;
2187      cselib_val *elt;
2188 {
2189   struct elt_list *el = empty_elt_lists;
2190
2191   if (el)
2192     empty_elt_lists = el->next;
2193   else
2194     el = (struct elt_list *) obstack_alloc (&cselib_obstack,
2195                                             sizeof (struct elt_list));
2196   el->next = next;
2197   el->elt = elt;
2198   return el;
2199 }
2200
2201 /* Allocate a struct elt_loc_list and fill in its two elements with the
2202    arguments.  */
2203
2204 static struct elt_loc_list *
2205 new_elt_loc_list (next, loc)
2206      struct elt_loc_list *next;
2207      rtx loc;
2208 {
2209   struct elt_loc_list *el = empty_elt_loc_lists;
2210
2211   if (el)
2212     empty_elt_loc_lists = el->next;
2213   else
2214     el = (struct elt_loc_list *) obstack_alloc (&cselib_obstack,
2215                                                 sizeof (struct elt_loc_list));
2216   el->next = next;
2217   el->loc = loc;
2218   el->setting_insn = cselib_current_insn;
2219   return el;
2220 }
2221
2222 /* The elt_list at *PL is no longer needed.  Unchain it and free its
2223    storage.  */
2224
2225 static void
2226 unchain_one_elt_list (pl)
2227      struct elt_list **pl;
2228 {
2229   struct elt_list *l = *pl;
2230
2231   *pl = l->next;
2232   l->next = empty_elt_lists;
2233   empty_elt_lists = l;
2234 }
2235
2236 /* Likewise for elt_loc_lists.  */
2237
2238 static void
2239 unchain_one_elt_loc_list (pl)
2240      struct elt_loc_list **pl;
2241 {
2242   struct elt_loc_list *l = *pl;
2243
2244   *pl = l->next;
2245   l->next = empty_elt_loc_lists;
2246   empty_elt_loc_lists = l;
2247 }
2248
2249 /* Likewise for cselib_vals.  This also frees the addr_list associated with
2250    V.  */
2251
2252 static void
2253 unchain_one_value (v)
2254      cselib_val *v;
2255 {
2256   while (v->addr_list)
2257     unchain_one_elt_list (&v->addr_list);
2258
2259   v->u.next_free = empty_vals;
2260   empty_vals = v;
2261 }
2262
2263 /* Remove all entries from the hash table.  Also used during
2264    initialization.  If CLEAR_ALL isn't set, then only clear the entries
2265    which are known to have been used.  */
2266
2267 static void
2268 clear_table (clear_all)
2269      int clear_all;
2270 {
2271   unsigned int i;
2272
2273   if (clear_all)
2274     for (i = 0; i < cselib_nregs; i++)
2275       REG_VALUES (i) = 0;
2276   else
2277     for (i = 0; i < VARRAY_ACTIVE_SIZE (used_regs); i++)
2278       REG_VALUES (VARRAY_UINT (used_regs, i)) = 0;
2279
2280   VARRAY_POP_ALL (used_regs);
2281
2282   htab_empty (hash_table);
2283   obstack_free (&cselib_obstack, cselib_startobj);
2284
2285   empty_vals = 0;
2286   empty_elt_lists = 0;
2287   empty_elt_loc_lists = 0;
2288   n_useless_values = 0;
2289
2290   next_unknown_value = 0;
2291 }
2292
2293 /* The equality test for our hash table.  The first argument ENTRY is a table
2294    element (i.e. a cselib_val), while the second arg X is an rtx.  We know
2295    that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
2296    CONST of an appropriate mode.  */
2297
2298 static int
2299 entry_and_rtx_equal_p (entry, x_arg)
2300      const void *entry, *x_arg;
2301 {
2302   struct elt_loc_list *l;
2303   const cselib_val *v = (const cselib_val *) entry;
2304   rtx x = (rtx) x_arg;
2305   enum machine_mode mode = GET_MODE (x);
2306
2307   if (GET_CODE (x) == CONST_INT
2308       || (mode == VOIDmode && GET_CODE (x) == CONST_DOUBLE))
2309     abort ();
2310   if (mode != GET_MODE (v->u.val_rtx))
2311     return 0;
2312
2313   /* Unwrap X if necessary.  */
2314   if (GET_CODE (x) == CONST
2315       && (GET_CODE (XEXP (x, 0)) == CONST_INT
2316           || GET_CODE (XEXP (x, 0)) == CONST_DOUBLE))
2317     x = XEXP (x, 0);
2318   
2319   /* We don't guarantee that distinct rtx's have different hash values,
2320      so we need to do a comparison.  */
2321   for (l = v->locs; l; l = l->next)
2322     if (rtx_equal_for_cselib_p (l->loc, x))
2323       return 1;
2324
2325   return 0;
2326 }
2327
2328 /* The hash function for our hash table.  The value is always computed with
2329    hash_rtx when adding an element; this function just extracts the hash
2330    value from a cselib_val structure.  */
2331
2332 static unsigned int
2333 get_value_hash (entry)
2334      const void *entry;
2335 {
2336   const cselib_val *v = (const cselib_val *) entry;
2337   return v->value;
2338 }
2339
2340 /* Return true if X contains a VALUE rtx.  If ONLY_USELESS is set, we
2341    only return true for values which point to a cselib_val whose value
2342    element has been set to zero, which implies the cselib_val will be
2343    removed.  */
2344
2345 int
2346 references_value_p (x, only_useless)
2347      rtx x;
2348      int only_useless;
2349 {
2350   enum rtx_code code = GET_CODE (x);
2351   const char *fmt = GET_RTX_FORMAT (code);
2352   int i, j;
2353
2354   if (GET_CODE (x) == VALUE
2355       && (! only_useless || CSELIB_VAL_PTR (x)->locs == 0))
2356     return 1;
2357
2358   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2359     {
2360       if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
2361         return 1;
2362       else if (fmt[i] == 'E')
2363         for (j = 0; j < XVECLEN (x, i); j++)
2364           if (references_value_p (XVECEXP (x, i, j), only_useless))
2365             return 1;
2366     }
2367
2368   return 0;
2369 }
2370
2371 /* For all locations found in X, delete locations that reference useless
2372    values (i.e. values without any location).  Called through
2373    htab_traverse.  */
2374
2375 static int
2376 discard_useless_locs (x, info)
2377      void **x;
2378      void *info ATTRIBUTE_UNUSED;
2379 {
2380   cselib_val *v = (cselib_val *)*x;
2381   struct elt_loc_list **p = &v->locs;
2382   int had_locs = v->locs != 0;
2383
2384   while (*p)
2385     {
2386       if (references_value_p ((*p)->loc, 1))
2387         unchain_one_elt_loc_list (p);
2388       else
2389         p = &(*p)->next;
2390     }
2391
2392   if (had_locs && v->locs == 0)
2393     {
2394       n_useless_values++;
2395       values_became_useless = 1;
2396     }
2397   return 1;
2398 }
2399
2400 /* If X is a value with no locations, remove it from the hashtable.  */
2401
2402 static int
2403 discard_useless_values (x, info)
2404      void **x;
2405      void *info ATTRIBUTE_UNUSED;
2406 {
2407   cselib_val *v = (cselib_val *)*x;
2408
2409   if (v->locs == 0)
2410     {
2411       htab_clear_slot (hash_table, x);
2412       unchain_one_value (v);
2413       n_useless_values--;
2414     }
2415
2416   return 1;
2417 }
2418
2419 /* Clean out useless values (i.e. those which no longer have locations
2420    associated with them) from the hash table.  */
2421
2422 static void
2423 remove_useless_values ()
2424 {
2425   /* First pass: eliminate locations that reference the value.  That in
2426      turn can make more values useless.  */
2427   do
2428     {
2429       values_became_useless = 0;
2430       htab_traverse (hash_table, discard_useless_locs, 0);
2431     }
2432   while (values_became_useless);
2433
2434   /* Second pass: actually remove the values.  */
2435   htab_traverse (hash_table, discard_useless_values, 0);
2436
2437   if (n_useless_values != 0)
2438     abort ();
2439 }
2440
2441 /* Return nonzero if we can prove that X and Y contain the same value, taking
2442    our gathered information into account.  */
2443
2444 int
2445 rtx_equal_for_cselib_p (x, y)
2446      rtx x, y;
2447 {
2448   enum rtx_code code;
2449   const char *fmt;
2450   int i;
2451   
2452   if (GET_CODE (x) == REG || GET_CODE (x) == MEM)
2453     {
2454       cselib_val *e = cselib_lookup (x, GET_MODE (x), 0);
2455
2456       if (e)
2457         x = e->u.val_rtx;
2458     }
2459
2460   if (GET_CODE (y) == REG || GET_CODE (y) == MEM)
2461     {
2462       cselib_val *e = cselib_lookup (y, GET_MODE (y), 0);
2463
2464       if (e)
2465         y = e->u.val_rtx;
2466     }
2467
2468   if (x == y)
2469     return 1;
2470
2471   if (GET_CODE (x) == VALUE && GET_CODE (y) == VALUE)
2472     return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
2473
2474   if (GET_CODE (x) == VALUE)
2475     {
2476       cselib_val *e = CSELIB_VAL_PTR (x);
2477       struct elt_loc_list *l;
2478
2479       for (l = e->locs; l; l = l->next)
2480         {
2481           rtx t = l->loc;
2482
2483           /* Avoid infinite recursion.  */
2484           if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
2485             continue;
2486           else if (rtx_equal_for_cselib_p (t, y))
2487             return 1;
2488         }
2489       
2490       return 0;
2491     }
2492
2493   if (GET_CODE (y) == VALUE)
2494     {
2495       cselib_val *e = CSELIB_VAL_PTR (y);
2496       struct elt_loc_list *l;
2497
2498       for (l = e->locs; l; l = l->next)
2499         {
2500           rtx t = l->loc;
2501
2502           if (GET_CODE (t) == REG || GET_CODE (t) == MEM)
2503             continue;
2504           else if (rtx_equal_for_cselib_p (x, t))
2505             return 1;
2506         }
2507       
2508       return 0;
2509     }
2510
2511   if (GET_CODE (x) != GET_CODE (y) || GET_MODE (x) != GET_MODE (y))
2512     return 0;
2513
2514   /* This won't be handled correctly by the code below.  */
2515   if (GET_CODE (x) == LABEL_REF)
2516     return XEXP (x, 0) == XEXP (y, 0);
2517   
2518   code = GET_CODE (x);
2519   fmt = GET_RTX_FORMAT (code);
2520
2521   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2522     {
2523       int j;
2524
2525       switch (fmt[i])
2526         {
2527         case 'w':
2528           if (XWINT (x, i) != XWINT (y, i))
2529             return 0;
2530           break;
2531
2532         case 'n':
2533         case 'i':
2534           if (XINT (x, i) != XINT (y, i))
2535             return 0;
2536           break;
2537
2538         case 'V':
2539         case 'E':
2540           /* Two vectors must have the same length.  */
2541           if (XVECLEN (x, i) != XVECLEN (y, i))
2542             return 0;
2543
2544           /* And the corresponding elements must match.  */
2545           for (j = 0; j < XVECLEN (x, i); j++)
2546             if (! rtx_equal_for_cselib_p (XVECEXP (x, i, j),
2547                                           XVECEXP (y, i, j)))
2548               return 0;
2549           break;
2550
2551         case 'e':
2552           if (! rtx_equal_for_cselib_p (XEXP (x, i), XEXP (y, i)))
2553             return 0;
2554           break;
2555
2556         case 'S':
2557         case 's':
2558           if (strcmp (XSTR (x, i), XSTR (y, i)))
2559             return 0;
2560           break;
2561
2562         case 'u':
2563           /* These are just backpointers, so they don't matter.  */
2564           break;
2565
2566         case '0':
2567         case 't':
2568           break;
2569
2570           /* It is believed that rtx's at this level will never
2571              contain anything but integers and other rtx's,
2572              except for within LABEL_REFs and SYMBOL_REFs.  */
2573         default:
2574           abort ();
2575         }
2576     }
2577   return 1;
2578 }
2579
2580 /* We need to pass down the mode of constants through the hash table
2581    functions.  For that purpose, wrap them in a CONST of the appropriate
2582    mode.  */
2583 static rtx
2584 wrap_constant (mode, x)
2585      enum machine_mode mode;
2586      rtx x;
2587 {
2588   if (GET_CODE (x) != CONST_INT
2589       && (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode))
2590     return x;
2591   if (mode == VOIDmode)
2592     abort ();
2593   return gen_rtx_CONST (mode, x);
2594 }
2595
2596 /* Hash an rtx.  Return 0 if we couldn't hash the rtx.
2597    For registers and memory locations, we look up their cselib_val structure
2598    and return its VALUE element.
2599    Possible reasons for return 0 are: the object is volatile, or we couldn't
2600    find a register or memory location in the table and CREATE is zero.  If
2601    CREATE is nonzero, table elts are created for regs and mem.
2602    MODE is used in hashing for CONST_INTs only;
2603    otherwise the mode of X is used.  */
2604
2605 static unsigned int
2606 hash_rtx (x, mode, create)
2607      rtx x;
2608      enum machine_mode mode;
2609      int create;
2610 {
2611   cselib_val *e;
2612   int i, j;
2613   enum rtx_code code;
2614   const char *fmt;
2615   unsigned int hash = 0;
2616
2617   /* repeat is used to turn tail-recursion into iteration.  */
2618  repeat:
2619   code = GET_CODE (x);
2620   hash += (unsigned) code + (unsigned) GET_MODE (x);
2621
2622   switch (code)
2623     {
2624     case MEM:
2625     case REG:
2626       e = cselib_lookup (x, GET_MODE (x), create);
2627       if (! e)
2628         return 0;
2629
2630       hash += e->value;
2631       return hash;
2632
2633     case CONST_INT:
2634       hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + INTVAL (x);
2635       return hash ? hash : CONST_INT;
2636
2637     case CONST_DOUBLE:
2638       /* This is like the general case, except that it only counts
2639          the integers representing the constant.  */
2640       hash += (unsigned) code + (unsigned) GET_MODE (x);
2641       if (GET_MODE (x) != VOIDmode)
2642         for (i = 2; i < GET_RTX_LENGTH (CONST_DOUBLE); i++)
2643           hash += XWINT (x, i);
2644       else
2645         hash += ((unsigned) CONST_DOUBLE_LOW (x)
2646                  + (unsigned) CONST_DOUBLE_HIGH (x));
2647       return hash ? hash : CONST_DOUBLE;
2648
2649       /* Assume there is only one rtx object for any given label.  */
2650     case LABEL_REF:
2651       hash
2652         += ((unsigned) LABEL_REF << 7) + (unsigned long) XEXP (x, 0);
2653       return hash ? hash : LABEL_REF;
2654
2655     case SYMBOL_REF:
2656       hash
2657         += ((unsigned) SYMBOL_REF << 7) + (unsigned long) XSTR (x, 0);
2658       return hash ? hash : SYMBOL_REF;
2659
2660     case PRE_DEC:
2661     case PRE_INC:
2662     case POST_DEC:
2663     case POST_INC:
2664     case POST_MODIFY:
2665     case PRE_MODIFY:
2666     case PC:
2667     case CC0:
2668     case CALL:
2669     case UNSPEC_VOLATILE:
2670       return 0;
2671
2672     case ASM_OPERANDS:
2673       if (MEM_VOLATILE_P (x))
2674         return 0;
2675
2676       break;
2677       
2678     default:
2679       break;
2680     }
2681
2682   i = GET_RTX_LENGTH (code) - 1;
2683   fmt = GET_RTX_FORMAT (code);
2684   for (; i >= 0; i--)
2685     {
2686       if (fmt[i] == 'e')
2687         {
2688           rtx tem = XEXP (x, i);
2689           unsigned int tem_hash;
2690
2691           /* If we are about to do the last recursive call
2692              needed at this level, change it into iteration.
2693              This function  is called enough to be worth it.  */
2694           if (i == 0)
2695             {
2696               x = tem;
2697               goto repeat;
2698             }
2699
2700           tem_hash = hash_rtx (tem, 0, create);
2701           if (tem_hash == 0)
2702             return 0;
2703
2704           hash += tem_hash;
2705         }
2706       else if (fmt[i] == 'E')
2707         for (j = 0; j < XVECLEN (x, i); j++)
2708           {
2709             unsigned int tem_hash = hash_rtx (XVECEXP (x, i, j), 0, create);
2710
2711             if (tem_hash == 0)
2712               return 0;
2713
2714             hash += tem_hash;
2715           }
2716       else if (fmt[i] == 's')
2717         {
2718           const unsigned char *p = (const unsigned char *) XSTR (x, i);
2719
2720           if (p)
2721             while (*p)
2722               hash += *p++;
2723         }
2724       else if (fmt[i] == 'i')
2725         hash += XINT (x, i);
2726       else if (fmt[i] == '0' || fmt[i] == 't')
2727         /* unused */;
2728       else
2729         abort ();
2730     }
2731
2732   return hash ? hash : 1 + GET_CODE (x);
2733 }
2734
2735 /* Create a new value structure for VALUE and initialize it.  The mode of the
2736    value is MODE.  */
2737
2738 static cselib_val *
2739 new_cselib_val (value, mode)
2740      unsigned int value;
2741      enum machine_mode mode;
2742 {
2743   cselib_val *e = empty_vals;
2744
2745   if (e)
2746     empty_vals = e->u.next_free;
2747   else
2748     e = (cselib_val *) obstack_alloc (&cselib_obstack, sizeof (cselib_val));
2749
2750   if (value == 0)
2751     abort ();
2752
2753   e->value = value;
2754   e->u.val_rtx = gen_rtx_VALUE (mode);
2755   CSELIB_VAL_PTR (e->u.val_rtx) = e;
2756   e->addr_list = 0;
2757   e->locs = 0;
2758   return e;
2759 }
2760
2761 /* ADDR_ELT is a value that is used as address.  MEM_ELT is the value that
2762    contains the data at this address.  X is a MEM that represents the
2763    value.  Update the two value structures to represent this situation.  */
2764
2765 static void
2766 add_mem_for_addr (addr_elt, mem_elt, x)
2767      cselib_val *addr_elt, *mem_elt;
2768      rtx x;
2769 {
2770   rtx new;
2771   struct elt_loc_list *l;
2772
2773   /* Avoid duplicates.  */
2774   for (l = mem_elt->locs; l; l = l->next)
2775     if (GET_CODE (l->loc) == MEM
2776         && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
2777       return;
2778
2779   new = gen_rtx_MEM (GET_MODE (x), addr_elt->u.val_rtx);
2780   MEM_COPY_ATTRIBUTES (new, x);
2781
2782   addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
2783   mem_elt->locs = new_elt_loc_list (mem_elt->locs, new);
2784 }
2785
2786 /* Subroutine of cselib_lookup.  Return a value for X, which is a MEM rtx.
2787    If CREATE, make a new one if we haven't seen it before.  */
2788
2789 static cselib_val *
2790 cselib_lookup_mem (x, create)
2791      rtx x;
2792      int create;
2793 {
2794   enum machine_mode mode = GET_MODE (x);
2795   void **slot;
2796   cselib_val *addr;
2797   cselib_val *mem_elt;
2798   struct elt_list *l;
2799
2800   if (MEM_VOLATILE_P (x) || mode == BLKmode
2801       || (FLOAT_MODE_P (mode) && flag_float_store))
2802     return 0;
2803
2804   /* Look up the value for the address.  */
2805   addr = cselib_lookup (XEXP (x, 0), mode, create);
2806   if (! addr)
2807     return 0;
2808
2809   /* Find a value that describes a value of our mode at that address.  */
2810   for (l = addr->addr_list; l; l = l->next)
2811     if (GET_MODE (l->elt->u.val_rtx) == mode)
2812       return l->elt;
2813
2814   if (! create)
2815     return 0;
2816
2817   mem_elt = new_cselib_val (++next_unknown_value, mode);
2818   add_mem_for_addr (addr, mem_elt, x);
2819   slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x),
2820                                    mem_elt->value, INSERT);
2821   *slot = mem_elt;
2822   return mem_elt;
2823 }
2824
2825 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
2826    with VALUE expressions.  This way, it becomes independent of changes
2827    to registers and memory.
2828    X isn't actually modified; if modifications are needed, new rtl is
2829    allocated.  However, the return value can share rtl with X.  */
2830
2831 static rtx
2832 cselib_subst_to_values (x)
2833      rtx x;
2834 {
2835   enum rtx_code code = GET_CODE (x);
2836   const char *fmt = GET_RTX_FORMAT (code);
2837   cselib_val *e;
2838   struct elt_list *l;
2839   rtx copy = x;
2840   int i;
2841
2842   switch (code)
2843     {
2844     case REG:
2845       for (l = REG_VALUES (REGNO (x)); l; l = l->next)
2846         if (GET_MODE (l->elt->u.val_rtx) == GET_MODE (x))
2847           return l->elt->u.val_rtx;
2848
2849       abort ();
2850
2851     case MEM:
2852       e = cselib_lookup_mem (x, 0);
2853       if (! e)
2854         abort ();
2855       return e->u.val_rtx;
2856
2857       /* CONST_DOUBLEs must be special-cased here so that we won't try to
2858          look up the CONST_DOUBLE_MEM inside.  */
2859     case CONST_DOUBLE:
2860     case CONST_INT:
2861       return x;
2862
2863     default:
2864       break;
2865     }
2866
2867   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2868     {
2869       if (fmt[i] == 'e')
2870         {
2871           rtx t = cselib_subst_to_values (XEXP (x, i));
2872
2873           if (t != XEXP (x, i) && x == copy)
2874             copy = shallow_copy_rtx (x);
2875
2876           XEXP (copy, i) = t;
2877         }
2878       else if (fmt[i] == 'E')
2879         {
2880           int j, k;
2881
2882           for (j = 0; j < XVECLEN (x, i); j++)
2883             {
2884               rtx t = cselib_subst_to_values (XVECEXP (x, i, j));
2885
2886               if (t != XVECEXP (x, i, j) && XVEC (x, i) == XVEC (copy, i))
2887                 {
2888                   if (x == copy)
2889                     copy = shallow_copy_rtx (x);
2890
2891                   XVEC (copy, i) = rtvec_alloc (XVECLEN (x, i));
2892                   for (k = 0; k < j; k++)
2893                     XVECEXP (copy, i, k) = XVECEXP (x, i, k);
2894                 }
2895
2896               XVECEXP (copy, i, j) = t;
2897             }
2898         }
2899     }
2900
2901   return copy;
2902 }
2903
2904 /* Look up the rtl expression X in our tables and return the value it has.
2905    If CREATE is zero, we return NULL if we don't know the value.  Otherwise,
2906    we create a new one if possible, using mode MODE if X doesn't have a mode
2907    (i.e. because it's a constant).  */
2908
2909 cselib_val *
2910 cselib_lookup (x, mode, create)
2911      rtx x;
2912      enum machine_mode mode;
2913      int create;
2914 {
2915   void **slot;
2916   cselib_val *e;
2917   unsigned int hashval;
2918
2919   if (GET_MODE (x) != VOIDmode)
2920     mode = GET_MODE (x);
2921
2922   if (GET_CODE (x) == VALUE)
2923     return CSELIB_VAL_PTR (x);
2924
2925   if (GET_CODE (x) == REG)
2926     {
2927       struct elt_list *l;
2928       unsigned int i = REGNO (x);
2929
2930       for (l = REG_VALUES (i); l; l = l->next)
2931         if (mode == GET_MODE (l->elt->u.val_rtx))
2932           return l->elt;
2933
2934       if (! create)
2935         return 0;
2936
2937       e = new_cselib_val (++next_unknown_value, GET_MODE (x));
2938       e->locs = new_elt_loc_list (e->locs, x);
2939       if (REG_VALUES (i) == 0)
2940         VARRAY_PUSH_UINT (used_regs, i);
2941       REG_VALUES (i) = new_elt_list (REG_VALUES (i), e);
2942       slot = htab_find_slot_with_hash (hash_table, x, e->value, INSERT);
2943       *slot = e;
2944       return e;
2945     }
2946
2947   if (GET_CODE (x) == MEM)
2948     return cselib_lookup_mem (x, create);
2949
2950   hashval = hash_rtx (x, mode, create);
2951   /* Can't even create if hashing is not possible.  */
2952   if (! hashval)
2953     return 0;
2954
2955   slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x),
2956                                    hashval, create ? INSERT : NO_INSERT);
2957   if (slot == 0)
2958     return 0;
2959
2960   e = (cselib_val *) *slot;
2961   if (e)
2962     return e;
2963
2964   e = new_cselib_val (hashval, mode);
2965
2966   /* We have to fill the slot before calling cselib_subst_to_values:
2967      the hash table is inconsistent until we do so, and
2968      cselib_subst_to_values will need to do lookups.  */
2969   *slot = (void *) e;
2970   e->locs = new_elt_loc_list (e->locs, cselib_subst_to_values (x));
2971   return e;
2972 }
2973
2974 /* Invalidate any entries in reg_values that overlap REGNO.  This is called
2975    if REGNO is changing.  MODE is the mode of the assignment to REGNO, which
2976    is used to determine how many hard registers are being changed.  If MODE
2977    is VOIDmode, then only REGNO is being changed; this is used when
2978    invalidating call clobbered registers across a call.  */
2979
2980 static void
2981 cselib_invalidate_regno (regno, mode)
2982      unsigned int regno;
2983      enum machine_mode mode;
2984 {
2985   unsigned int endregno;
2986   unsigned int i;
2987
2988   /* If we see pseudos after reload, something is _wrong_.  */
2989   if (reload_completed && regno >= FIRST_PSEUDO_REGISTER
2990       && reg_renumber[regno] >= 0)
2991     abort ();
2992
2993   /* Determine the range of registers that must be invalidated.  For
2994      pseudos, only REGNO is affected.  For hard regs, we must take MODE
2995      into account, and we must also invalidate lower register numbers
2996      if they contain values that overlap REGNO.  */
2997   endregno = regno + 1;
2998   if (regno < FIRST_PSEUDO_REGISTER && mode != VOIDmode) 
2999     endregno = regno + HARD_REGNO_NREGS (regno, mode);
3000
3001   for (i = 0; i < endregno; i++)
3002     {
3003       struct elt_list **l = &REG_VALUES (i);
3004
3005       /* Go through all known values for this reg; if it overlaps the range
3006          we're invalidating, remove the value.  */
3007       while (*l)
3008         {
3009           cselib_val *v = (*l)->elt;
3010           struct elt_loc_list **p;
3011           unsigned int this_last = i;
3012
3013           if (i < FIRST_PSEUDO_REGISTER)
3014             this_last += HARD_REGNO_NREGS (i, GET_MODE (v->u.val_rtx)) - 1;
3015
3016           if (this_last < regno)
3017             {
3018               l = &(*l)->next;
3019               continue;
3020             }
3021
3022           /* We have an overlap.  */
3023           unchain_one_elt_list (l);
3024
3025           /* Now, we clear the mapping from value to reg.  It must exist, so
3026              this code will crash intentionally if it doesn't.  */
3027           for (p = &v->locs; ; p = &(*p)->next)
3028             {
3029               rtx x = (*p)->loc;
3030
3031               if (GET_CODE (x) == REG && REGNO (x) == i)
3032                 {
3033                   unchain_one_elt_loc_list (p);
3034                   break;
3035                 }
3036             }
3037           if (v->locs == 0)
3038             n_useless_values++;
3039         }
3040     }
3041 }
3042
3043 /* The memory at address MEM_BASE is being changed.
3044    Return whether this change will invalidate VAL.  */
3045
3046 static int
3047 cselib_mem_conflict_p (mem_base, val)
3048      rtx mem_base;
3049      rtx val;
3050 {
3051   enum rtx_code code;
3052   const char *fmt;
3053   int i, j;
3054
3055   code = GET_CODE (val);
3056   switch (code)
3057     {
3058       /* Get rid of a few simple cases quickly. */
3059     case REG:
3060     case PC:
3061     case CC0:
3062     case SCRATCH:
3063     case CONST:
3064     case CONST_INT:
3065     case CONST_DOUBLE:
3066     case SYMBOL_REF:
3067     case LABEL_REF:
3068       return 0;
3069
3070     case MEM:
3071       if (GET_MODE (mem_base) == BLKmode
3072           || GET_MODE (val) == BLKmode
3073           || anti_dependence (val, mem_base))
3074         return 1;
3075
3076       /* The address may contain nested MEMs.  */
3077       break;
3078
3079     default:
3080       break;
3081     }
3082
3083   fmt = GET_RTX_FORMAT (code);
3084   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3085     {
3086       if (fmt[i] == 'e')
3087         {
3088           if (cselib_mem_conflict_p (mem_base, XEXP (val, i)))
3089             return 1;
3090         }
3091       else if (fmt[i] == 'E')
3092         for (j = 0; j < XVECLEN (val, i); j++)
3093           if (cselib_mem_conflict_p (mem_base, XVECEXP (val, i, j)))
3094             return 1;
3095     }
3096
3097   return 0;
3098 }
3099
3100 /* For the value found in SLOT, walk its locations to determine if any overlap
3101    INFO (which is a MEM rtx).  */
3102
3103 static int
3104 cselib_invalidate_mem_1 (slot, info)
3105      void **slot;
3106      void *info;
3107 {
3108   cselib_val *v = (cselib_val *) *slot;
3109   rtx mem_rtx = (rtx) info;
3110   struct elt_loc_list **p = &v->locs;
3111   int had_locs = v->locs != 0;
3112
3113   while (*p)
3114     {
3115       rtx x = (*p)->loc;
3116       cselib_val *addr;
3117       struct elt_list **mem_chain;
3118
3119       /* MEMs may occur in locations only at the top level; below
3120          that every MEM or REG is substituted by its VALUE.  */
3121       if (GET_CODE (x) != MEM
3122           || ! cselib_mem_conflict_p (mem_rtx, x))
3123         {
3124           p = &(*p)->next;
3125           continue;
3126         }
3127
3128       /* This one overlaps.  */
3129       /* We must have a mapping from this MEM's address to the
3130          value (E).  Remove that, too.  */
3131       addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0);
3132       mem_chain = &addr->addr_list;
3133       for (;;)
3134         {
3135           if ((*mem_chain)->elt == v)
3136             {
3137               unchain_one_elt_list (mem_chain);
3138               break;
3139             }
3140
3141           mem_chain = &(*mem_chain)->next;
3142         }
3143
3144       unchain_one_elt_loc_list (p);
3145     }
3146
3147   if (had_locs && v->locs == 0)
3148     n_useless_values++;
3149
3150   return 1;
3151 }
3152
3153 /* Invalidate any locations in the table which are changed because of a
3154    store to MEM_RTX.  If this is called because of a non-const call
3155    instruction, MEM_RTX is (mem:BLK const0_rtx).  */
3156
3157 static void
3158 cselib_invalidate_mem (mem_rtx)
3159      rtx mem_rtx;
3160 {
3161   htab_traverse (hash_table, cselib_invalidate_mem_1, mem_rtx);
3162 }
3163
3164 /* Invalidate DEST, which is being assigned to or clobbered.  The second and
3165    the third parameter exist so that this function can be passed to
3166    note_stores; they are ignored.  */
3167
3168 static void
3169 cselib_invalidate_rtx (dest, ignore, data)
3170      rtx dest;
3171      rtx ignore ATTRIBUTE_UNUSED;
3172      void *data ATTRIBUTE_UNUSED;
3173 {
3174   while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SIGN_EXTRACT
3175          || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG)
3176     dest = XEXP (dest, 0);
3177
3178   if (GET_CODE (dest) == REG)
3179     cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
3180   else if (GET_CODE (dest) == MEM)
3181     cselib_invalidate_mem (dest);
3182
3183   /* Some machines don't define AUTO_INC_DEC, but they still use push
3184      instructions.  We need to catch that case here in order to
3185      invalidate the stack pointer correctly.  Note that invalidating
3186      the stack pointer is different from invalidating DEST.  */
3187   if (push_operand (dest, GET_MODE (dest)))
3188     cselib_invalidate_rtx (stack_pointer_rtx, NULL_RTX, NULL);
3189 }
3190
3191 /* Record the result of a SET instruction.  DEST is being set; the source
3192    contains the value described by SRC_ELT.  If DEST is a MEM, DEST_ADDR_ELT
3193    describes its address.  */
3194
3195 static void
3196 cselib_record_set (dest, src_elt, dest_addr_elt)
3197      rtx dest;
3198      cselib_val *src_elt, *dest_addr_elt;
3199 {
3200   int dreg = GET_CODE (dest) == REG ? (int) REGNO (dest) : -1;
3201
3202   if (src_elt == 0 || side_effects_p (dest))
3203     return;
3204
3205   if (dreg >= 0)
3206     {
3207       if (REG_VALUES (dreg) == 0)
3208         VARRAY_PUSH_UINT (used_regs, dreg);
3209
3210       REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
3211       if (src_elt->locs == 0)
3212         n_useless_values--;
3213       src_elt->locs = new_elt_loc_list (src_elt->locs, dest);
3214     }
3215   else if (GET_CODE (dest) == MEM && dest_addr_elt != 0)
3216     {
3217       if (src_elt->locs == 0)
3218         n_useless_values--;
3219       add_mem_for_addr (dest_addr_elt, src_elt, dest);
3220     }
3221 }
3222
3223 /* Describe a single set that is part of an insn.  */
3224 struct set
3225 {
3226   rtx src;
3227   rtx dest;
3228   cselib_val *src_elt;
3229   cselib_val *dest_addr_elt;
3230 };
3231
3232 /* There is no good way to determine how many elements there can be
3233    in a PARALLEL.  Since it's fairly cheap, use a really large number.  */
3234 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
3235
3236 /* Record the effects of any sets in INSN.  */
3237 static void
3238 cselib_record_sets (insn)
3239      rtx insn;
3240 {
3241   int n_sets = 0;
3242   int i;
3243   struct set sets[MAX_SETS];
3244   rtx body = PATTERN (insn);
3245
3246   body = PATTERN (insn);
3247   /* Find all sets.  */
3248   if (GET_CODE (body) == SET)
3249     {
3250       sets[0].src = SET_SRC (body);
3251       sets[0].dest = SET_DEST (body);
3252       n_sets = 1;
3253     }
3254   else if (GET_CODE (body) == PARALLEL)
3255     {
3256       /* Look through the PARALLEL and record the values being
3257          set, if possible.  Also handle any CLOBBERs.  */
3258       for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
3259         {
3260           rtx x = XVECEXP (body, 0, i);
3261
3262           if (GET_CODE (x) == SET)
3263             {
3264               sets[n_sets].src = SET_SRC (x);
3265               sets[n_sets].dest = SET_DEST (x);
3266               n_sets++;
3267             }
3268         }
3269     }
3270
3271   /* Look up the values that are read.  Do this before invalidating the
3272      locations that are written.  */
3273   for (i = 0; i < n_sets; i++)
3274     {
3275       rtx dest = sets[i].dest;
3276
3277       /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
3278          the low part after invalidating any knowledge about larger modes.  */
3279       if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
3280         sets[i].dest = dest = XEXP (dest, 0);
3281
3282       /* We don't know how to record anything but REG or MEM.  */
3283       if (GET_CODE (dest) == REG || GET_CODE (dest) == MEM)
3284         {
3285           sets[i].src_elt = cselib_lookup (sets[i].src, GET_MODE (dest), 1);
3286           if (GET_CODE (dest) == MEM)
3287             sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0), Pmode, 1);
3288           else
3289             sets[i].dest_addr_elt = 0;
3290         }
3291     }
3292
3293   /* Invalidate all locations written by this insn.  Note that the elts we
3294      looked up in the previous loop aren't affected, just some of their
3295      locations may go away.  */
3296   note_stores (body, cselib_invalidate_rtx, NULL);
3297
3298   /* Now enter the equivalences in our tables.  */
3299   for (i = 0; i < n_sets; i++)
3300     {
3301       rtx dest = sets[i].dest;
3302       if (GET_CODE (dest) == REG || GET_CODE (dest) == MEM)
3303         cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
3304     }
3305 }
3306
3307 /* Record the effects of INSN.  */
3308
3309 void
3310 cselib_process_insn (insn)
3311      rtx insn;
3312 {
3313   int i;
3314   rtx x;
3315
3316   cselib_current_insn = insn;
3317
3318   /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp.  */
3319   if (GET_CODE (insn) == CODE_LABEL
3320       || (GET_CODE (insn) == NOTE
3321           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
3322       || (GET_CODE (insn) == INSN
3323           && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
3324           && MEM_VOLATILE_P (PATTERN (insn))))
3325     {
3326       clear_table (0);
3327       return;
3328     }
3329
3330   if (! INSN_P (insn))
3331     {
3332       cselib_current_insn = 0;
3333       return;
3334     }
3335
3336   /* If this is a call instruction, forget anything stored in a
3337      call clobbered register, or, if this is not a const call, in
3338      memory.  */
3339   if (GET_CODE (insn) == CALL_INSN)
3340     {
3341       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3342         if (call_used_regs[i])
3343           cselib_invalidate_regno (i, VOIDmode);
3344
3345       if (! CONST_CALL_P (insn))
3346         cselib_invalidate_mem (callmem);
3347     }
3348
3349   cselib_record_sets (insn);
3350
3351 #ifdef AUTO_INC_DEC
3352   /* Clobber any registers which appear in REG_INC notes.  We
3353      could keep track of the changes to their values, but it is
3354      unlikely to help.  */
3355   for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
3356     if (REG_NOTE_KIND (x) == REG_INC)
3357       cselib_invalidate_rtx (XEXP (x, 0), NULL_RTX, NULL);
3358 #endif
3359
3360   /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
3361      after we have processed the insn.  */
3362   if (GET_CODE (insn) == CALL_INSN)
3363     for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
3364       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
3365         cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0), NULL_RTX, NULL);
3366
3367   cselib_current_insn = 0;
3368
3369   if (n_useless_values > MAX_USELESS_VALUES)
3370     remove_useless_values ();
3371 }
3372
3373 /* Make sure our varrays are big enough.  Not called from any cselib routines;
3374    it must be called by the user if it allocated new registers.  */
3375
3376 void
3377 cselib_update_varray_sizes ()
3378 {
3379   unsigned int nregs = max_reg_num ();
3380
3381   if (nregs == cselib_nregs)
3382     return;
3383
3384   cselib_nregs = nregs;
3385   VARRAY_GROW (reg_values, nregs);
3386   VARRAY_GROW (used_regs, nregs);
3387 }
3388
3389 /* Initialize cselib for one pass.  The caller must also call
3390    init_alias_analysis.  */
3391
3392 void
3393 cselib_init ()
3394 {
3395   /* These are only created once.  */
3396   if (! callmem)
3397     {
3398       gcc_obstack_init (&cselib_obstack);
3399       cselib_startobj = obstack_alloc (&cselib_obstack, 0);
3400
3401       callmem = gen_rtx_MEM (BLKmode, const0_rtx);
3402       ggc_add_rtx_root (&callmem, 1);
3403     }
3404
3405   cselib_nregs = max_reg_num ();
3406   VARRAY_ELT_LIST_INIT (reg_values, cselib_nregs, "reg_values");
3407   VARRAY_UINT_INIT (used_regs, cselib_nregs, "used_regs");
3408   hash_table = htab_create (31, get_value_hash, entry_and_rtx_equal_p, NULL);
3409   clear_table (1);
3410 }
3411
3412 /* Called when the current user is done with cselib.  */
3413
3414 void
3415 cselib_finish ()
3416 {
3417   clear_table (0);
3418   VARRAY_FREE (reg_values);
3419   VARRAY_FREE (used_regs);
3420   htab_delete (hash_table);
3421 }