genattrtab.c (attr_rtx_1): Hash SYMBOL_REFs.
[platform/upstream/gcc.git] / gcc / genattrtab.c
1 /* Generate code from machine description to compute values of attributes.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5    Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 /* This program handles insn attributes and the DEFINE_DELAY and
24    DEFINE_INSN_RESERVATION definitions.
25
26    It produces a series of functions named `get_attr_...', one for each insn
27    attribute.  Each of these is given the rtx for an insn and returns a member
28    of the enum for the attribute.
29
30    These subroutines have the form of a `switch' on the INSN_CODE (via
31    `recog_memoized').  Each case either returns a constant attribute value
32    or a value that depends on tests on other attributes, the form of
33    operands, or some random C expression (encoded with a SYMBOL_REF
34    expression).
35
36    If the attribute `alternative', or a random C expression is present,
37    `constrain_operands' is called.  If either of these cases of a reference to
38    an operand is found, `extract_insn' is called.
39
40    The special attribute `length' is also recognized.  For this operand,
41    expressions involving the address of an operand or the current insn,
42    (address (pc)), are valid.  In this case, an initial pass is made to
43    set all lengths that do not depend on address.  Those that do are set to
44    the maximum length.  Then each insn that depends on an address is checked
45    and possibly has its length changed.  The process repeats until no further
46    changed are made.  The resulting lengths are saved for use by
47    `get_attr_length'.
48
49    A special form of DEFINE_ATTR, where the expression for default value is a
50    CONST expression, indicates an attribute that is constant for a given run
51    of the compiler.  The subroutine generated for these attributes has no
52    parameters as it does not depend on any particular insn.  Constant
53    attributes are typically used to specify which variety of processor is
54    used.
55
56    Internal attributes are defined to handle DEFINE_DELAY and
57    DEFINE_INSN_RESERVATION.  Special routines are output for these cases.
58
59    This program works by keeping a list of possible values for each attribute.
60    These include the basic attribute choices, default values for attribute, and
61    all derived quantities.
62
63    As the description file is read, the definition for each insn is saved in a
64    `struct insn_def'.   When the file reading is complete, a `struct insn_ent'
65    is created for each insn and chained to the corresponding attribute value,
66    either that specified, or the default.
67
68    An optimization phase is then run.  This simplifies expressions for each
69    insn.  EQ_ATTR tests are resolved, whenever possible, to a test that
70    indicates when the attribute has the specified value for the insn.  This
71    avoids recursive calls during compilation.
72
73    The strategy used when processing DEFINE_DELAY definitions is to create
74    arbitrarily complex expressions and have the optimization simplify them.
75
76    Once optimization is complete, any required routines and definitions
77    will be written.
78
79    An optimization that is not yet implemented is to hoist the constant
80    expressions entirely out of the routines and definitions that are written.
81    A way to do this is to iterate over all possible combinations of values
82    for constant attributes and generate a set of functions for that given
83    combination.  An initialization function would be written that evaluates
84    the attributes and installs the corresponding set of routines and
85    definitions (each would be accessed through a pointer).
86
87    We use the flags in an RTX as follows:
88    `unchanging' (ATTR_IND_SIMPLIFIED_P): This rtx is fully simplified
89       independent of the insn code.
90    `in_struct' (ATTR_CURR_SIMPLIFIED_P): This rtx is fully simplified
91       for the insn code currently being processed (see optimize_attrs).
92    `return_val' (ATTR_PERMANENT_P): This rtx is permanent and unique
93       (see attr_rtx).  */
94
95 #define ATTR_IND_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), unchanging))
96 #define ATTR_CURR_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), in_struct))
97 #define ATTR_PERMANENT_P(RTX) (RTX_FLAG((RTX), return_val))
98
99 #if 0
100 #define strcmp_check(S1, S2) ((S1) == (S2)              \
101                               ? 0                       \
102                               : (gcc_assert (strcmp ((S1), (S2))), 1))
103 #else
104 #define strcmp_check(S1, S2) ((S1) != (S2))
105 #endif
106
107 #include "bconfig.h"
108 #include "system.h"
109 #include "coretypes.h"
110 #include "tm.h"
111 #include "rtl.h"
112 #include "obstack.h"
113 #include "errors.h"
114 #include "read-md.h"
115 #include "gensupport.h"
116 #include "vecprim.h"
117 #include "fnmatch.h"
118
119 /* Flags for make_internal_attr's `special' parameter.  */
120 #define ATTR_NONE               0
121 #define ATTR_SPECIAL            (1 << 0)
122
123 static struct obstack obstack1, obstack2;
124 static struct obstack *hash_obstack = &obstack1;
125 static struct obstack *temp_obstack = &obstack2;
126
127 /* enough space to reserve for printing out ints */
128 #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
129
130 /* Define structures used to record attributes and values.  */
131
132 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
133    encountered, we store all the relevant information into a
134    `struct insn_def'.  This is done to allow attribute definitions to occur
135    anywhere in the file.  */
136
137 struct insn_def
138 {
139   struct insn_def *next;        /* Next insn in chain.  */
140   rtx def;                      /* The DEFINE_...  */
141   int insn_code;                /* Instruction number.  */
142   int insn_index;               /* Expression number in file, for errors.  */
143   int lineno;                   /* Line number.  */
144   int num_alternatives;         /* Number of alternatives.  */
145   int vec_idx;                  /* Index of attribute vector in `def'.  */
146 };
147
148 /* Once everything has been read in, we store in each attribute value a list
149    of insn codes that have that value.  Here is the structure used for the
150    list.  */
151
152 struct insn_ent
153 {
154   struct insn_ent *next;        /* Next in chain.  */
155   struct insn_def *def;         /* Instruction definition.  */
156 };
157
158 /* Each value of an attribute (either constant or computed) is assigned a
159    structure which is used as the listhead of the insns that have that
160    value.  */
161
162 struct attr_value
163 {
164   rtx value;                    /* Value of attribute.  */
165   struct attr_value *next;      /* Next attribute value in chain.  */
166   struct insn_ent *first_insn;  /* First insn with this value.  */
167   int num_insns;                /* Number of insns with this value.  */
168   int has_asm_insn;             /* True if this value used for `asm' insns */
169 };
170
171 /* Structure for each attribute.  */
172
173 struct attr_desc
174 {
175   char *name;                   /* Name of attribute.  */
176   const char *enum_name;        /* Enum name for DEFINE_ENUM_NAME.  */
177   struct attr_desc *next;       /* Next attribute.  */
178   struct attr_value *first_value; /* First value of this attribute.  */
179   struct attr_value *default_val; /* Default value for this attribute.  */
180   int lineno : 24;              /* Line number.  */
181   unsigned is_numeric   : 1;    /* Values of this attribute are numeric.  */
182   unsigned is_const     : 1;    /* Attribute value constant for each run.  */
183   unsigned is_special   : 1;    /* Don't call `write_attr_set'.  */
184 };
185
186 /* Structure for each DEFINE_DELAY.  */
187
188 struct delay_desc
189 {
190   rtx def;                      /* DEFINE_DELAY expression.  */
191   struct delay_desc *next;      /* Next DEFINE_DELAY.  */
192   int num;                      /* Number of DEFINE_DELAY, starting at 1.  */
193   int lineno;                   /* Line number.  */
194 };
195
196 struct attr_value_list
197 {
198   struct attr_value *av;
199   struct insn_ent *ie;
200   struct attr_desc *attr;
201   struct attr_value_list *next;
202 };
203
204 /* Listheads of above structures.  */
205
206 /* This one is indexed by the first character of the attribute name.  */
207 #define MAX_ATTRS_INDEX 256
208 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
209 static struct insn_def *defs;
210 static struct delay_desc *delays;
211 struct attr_value_list **insn_code_values;
212
213 /* Other variables.  */
214
215 static int insn_code_number;
216 static int insn_index_number;
217 static int got_define_asm_attributes;
218 static int must_extract;
219 static int must_constrain;
220 static int address_used;
221 static int length_used;
222 static int num_delays;
223 static int have_annul_true, have_annul_false;
224 static int num_insn_ents;
225
226 /* Stores, for each insn code, the number of constraint alternatives.  */
227
228 static int *insn_n_alternatives;
229
230 /* Stores, for each insn code, a bitmap that has bits on for each possible
231    alternative.  */
232
233 static int *insn_alternatives;
234
235 /* Used to simplify expressions.  */
236
237 static rtx true_rtx, false_rtx;
238
239 /* Used to reduce calls to `strcmp' */
240
241 static const char *alternative_name;
242 static const char *length_str;
243 static const char *delay_type_str;
244 static const char *delay_1_0_str;
245 static const char *num_delay_slots_str;
246
247 /* Simplify an expression.  Only call the routine if there is something to
248    simplify.  */
249 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX)     \
250   (ATTR_IND_SIMPLIFIED_P (EXP) || ATTR_CURR_SIMPLIFIED_P (EXP) ? (EXP)  \
251    : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
252
253 #define DEF_ATTR_STRING(S) (attr_string ((S), strlen (S)))
254
255 /* Forward declarations of functions used before their definitions, only.  */
256 static char *attr_string           (const char *, int);
257 static char *attr_printf           (unsigned int, const char *, ...)
258   ATTRIBUTE_PRINTF_2;
259 static rtx make_numeric_value      (int);
260 static struct attr_desc *find_attr (const char **, int);
261 static rtx mk_attr_alt             (int);
262 static char *next_comma_elt        (const char **);
263 static rtx insert_right_side       (enum rtx_code, rtx, rtx, int, int);
264 static rtx copy_boolean            (rtx);
265 static int compares_alternatives_p (rtx);
266 static void make_internal_attr     (const char *, rtx, int);
267 static void insert_insn_ent        (struct attr_value *, struct insn_ent *);
268 static void walk_attr_value        (rtx);
269 static int max_attr_value          (rtx, int*);
270 static int min_attr_value          (rtx, int*);
271 static int or_attr_value           (rtx, int*);
272 static rtx simplify_test_exp       (rtx, int, int);
273 static rtx simplify_test_exp_in_temp (rtx, int, int);
274 static rtx copy_rtx_unchanging     (rtx);
275 static bool attr_alt_subset_p      (rtx, rtx);
276 static bool attr_alt_subset_of_compl_p (rtx, rtx);
277 static void clear_struct_flag      (rtx);
278 static void write_attr_valueq      (struct attr_desc *, const char *);
279 static struct attr_value *find_most_used  (struct attr_desc *);
280 static void write_attr_set         (struct attr_desc *, int, rtx,
281                                     const char *, const char *, rtx,
282                                     int, int, unsigned int);
283 static void write_attr_case        (struct attr_desc *, struct attr_value *,
284                                     int, const char *, const char *, int, rtx);
285 static void write_attr_value       (struct attr_desc *, rtx);
286 static void write_upcase           (const char *);
287 static void write_indent           (int);
288 static rtx identity_fn             (rtx);
289 static rtx zero_fn                 (rtx);
290 static rtx one_fn                  (rtx);
291 static rtx max_fn                  (rtx);
292 static rtx min_fn                  (rtx);
293
294 #define oballoc(T) XOBNEW (hash_obstack, T)
295 #define oballocvec(T, N) XOBNEWVEC (hash_obstack, T, (N))
296
297 /* Hash table for sharing RTL and strings.  */
298
299 /* Each hash table slot is a bucket containing a chain of these structures.
300    Strings are given negative hash codes; RTL expressions are given positive
301    hash codes.  */
302
303 struct attr_hash
304 {
305   struct attr_hash *next;       /* Next structure in the bucket.  */
306   int hashcode;                 /* Hash code of this rtx or string.  */
307   union
308     {
309       char *str;                /* The string (negative hash codes) */
310       rtx rtl;                  /* or the RTL recorded here.  */
311     } u;
312 };
313
314 /* Now here is the hash table.  When recording an RTL, it is added to
315    the slot whose index is the hash code mod the table size.  Note
316    that the hash table is used for several kinds of RTL (see attr_rtx)
317    and for strings.  While all these live in the same table, they are
318    completely independent, and the hash code is computed differently
319    for each.  */
320
321 #define RTL_HASH_SIZE 4093
322 static struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
323
324 /* Here is how primitive or already-shared RTL's hash
325    codes are made.  */
326 #define RTL_HASH(RTL) ((intptr_t) (RTL) & 0777777)
327
328 /* Add an entry to the hash table for RTL with hash code HASHCODE.  */
329
330 static void
331 attr_hash_add_rtx (int hashcode, rtx rtl)
332 {
333   struct attr_hash *h;
334
335   h = XOBNEW (hash_obstack, struct attr_hash);
336   h->hashcode = hashcode;
337   h->u.rtl = rtl;
338   h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
339   attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
340 }
341
342 /* Add an entry to the hash table for STRING with hash code HASHCODE.  */
343
344 static void
345 attr_hash_add_string (int hashcode, char *str)
346 {
347   struct attr_hash *h;
348
349   h = XOBNEW (hash_obstack, struct attr_hash);
350   h->hashcode = -hashcode;
351   h->u.str = str;
352   h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
353   attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
354 }
355
356 /* Generate an RTL expression, but avoid duplicates.
357    Set the ATTR_PERMANENT_P flag for these permanent objects.
358
359    In some cases we cannot uniquify; then we return an ordinary
360    impermanent rtx with ATTR_PERMANENT_P clear.
361
362    Args are as follows:
363
364    rtx attr_rtx (code, [element1, ..., elementn])  */
365
366 static rtx
367 attr_rtx_1 (enum rtx_code code, va_list p)
368 {
369   rtx rt_val = NULL_RTX;/* RTX to return to caller...           */
370   int hashcode;
371   struct attr_hash *h;
372   struct obstack *old_obstack = rtl_obstack;
373
374   /* For each of several cases, search the hash table for an existing entry.
375      Use that entry if one is found; otherwise create a new RTL and add it
376      to the table.  */
377
378   if (GET_RTX_CLASS (code) == RTX_UNARY)
379     {
380       rtx arg0 = va_arg (p, rtx);
381
382       /* A permanent object cannot point to impermanent ones.  */
383       if (! ATTR_PERMANENT_P (arg0))
384         {
385           rt_val = rtx_alloc (code);
386           XEXP (rt_val, 0) = arg0;
387           return rt_val;
388         }
389
390       hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
391       for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
392         if (h->hashcode == hashcode
393             && GET_CODE (h->u.rtl) == code
394             && XEXP (h->u.rtl, 0) == arg0)
395           return h->u.rtl;
396
397       if (h == 0)
398         {
399           rtl_obstack = hash_obstack;
400           rt_val = rtx_alloc (code);
401           XEXP (rt_val, 0) = arg0;
402         }
403     }
404   else if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
405            || GET_RTX_CLASS (code) == RTX_COMM_ARITH
406            || GET_RTX_CLASS (code) == RTX_COMPARE
407            || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
408     {
409       rtx arg0 = va_arg (p, rtx);
410       rtx arg1 = va_arg (p, rtx);
411
412       /* A permanent object cannot point to impermanent ones.  */
413       if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
414         {
415           rt_val = rtx_alloc (code);
416           XEXP (rt_val, 0) = arg0;
417           XEXP (rt_val, 1) = arg1;
418           return rt_val;
419         }
420
421       hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
422       for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
423         if (h->hashcode == hashcode
424             && GET_CODE (h->u.rtl) == code
425             && XEXP (h->u.rtl, 0) == arg0
426             && XEXP (h->u.rtl, 1) == arg1)
427           return h->u.rtl;
428
429       if (h == 0)
430         {
431           rtl_obstack = hash_obstack;
432           rt_val = rtx_alloc (code);
433           XEXP (rt_val, 0) = arg0;
434           XEXP (rt_val, 1) = arg1;
435         }
436     }
437   else if (code == SYMBOL_REF
438            || (GET_RTX_LENGTH (code) == 1
439                && GET_RTX_FORMAT (code)[0] == 's'))
440     {
441       char *arg0 = va_arg (p, char *);
442
443       arg0 = DEF_ATTR_STRING (arg0);
444
445       hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
446       for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
447         if (h->hashcode == hashcode
448             && GET_CODE (h->u.rtl) == code
449             && XSTR (h->u.rtl, 0) == arg0)
450           return h->u.rtl;
451
452       if (h == 0)
453         {
454           rtl_obstack = hash_obstack;
455           rt_val = rtx_alloc (code);
456           XSTR (rt_val, 0) = arg0;
457           if (code == SYMBOL_REF)
458             {
459               X0EXP (rt_val, 1) = NULL_RTX;
460               X0EXP (rt_val, 2) = NULL_RTX;
461             }
462         }
463     }
464   else if (GET_RTX_LENGTH (code) == 2
465            && GET_RTX_FORMAT (code)[0] == 's'
466            && GET_RTX_FORMAT (code)[1] == 's')
467     {
468       char *arg0 = va_arg (p, char *);
469       char *arg1 = va_arg (p, char *);
470
471       hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
472       for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
473         if (h->hashcode == hashcode
474             && GET_CODE (h->u.rtl) == code
475             && XSTR (h->u.rtl, 0) == arg0
476             && XSTR (h->u.rtl, 1) == arg1)
477           return h->u.rtl;
478
479       if (h == 0)
480         {
481           rtl_obstack = hash_obstack;
482           rt_val = rtx_alloc (code);
483           XSTR (rt_val, 0) = arg0;
484           XSTR (rt_val, 1) = arg1;
485         }
486     }
487   else if (code == CONST_INT)
488     {
489       HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
490       if (arg0 == 0)
491         return false_rtx;
492       else if (arg0 == 1)
493         return true_rtx;
494       else
495         goto nohash;
496     }
497   else
498     {
499       int i;            /* Array indices...                     */
500       const char *fmt;  /* Current rtx's format...              */
501     nohash:
502       rt_val = rtx_alloc (code);        /* Allocate the storage space.  */
503
504       fmt = GET_RTX_FORMAT (code);      /* Find the right format...  */
505       for (i = 0; i < GET_RTX_LENGTH (code); i++)
506         {
507           switch (*fmt++)
508             {
509             case '0':           /* Unused field.  */
510               break;
511
512             case 'i':           /* An integer?  */
513               XINT (rt_val, i) = va_arg (p, int);
514               break;
515
516             case 'w':           /* A wide integer? */
517               XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
518               break;
519
520             case 's':           /* A string?  */
521               XSTR (rt_val, i) = va_arg (p, char *);
522               break;
523
524             case 'e':           /* An expression?  */
525             case 'u':           /* An insn?  Same except when printing.  */
526               XEXP (rt_val, i) = va_arg (p, rtx);
527               break;
528
529             case 'E':           /* An RTX vector?  */
530               XVEC (rt_val, i) = va_arg (p, rtvec);
531               break;
532
533             default:
534               gcc_unreachable ();
535             }
536         }
537       return rt_val;
538     }
539
540   rtl_obstack = old_obstack;
541   attr_hash_add_rtx (hashcode, rt_val);
542   ATTR_PERMANENT_P (rt_val) = 1;
543   return rt_val;
544 }
545
546 static rtx
547 attr_rtx (enum rtx_code code, ...)
548 {
549   rtx result;
550   va_list p;
551
552   va_start (p, code);
553   result = attr_rtx_1 (code, p);
554   va_end (p);
555   return result;
556 }
557
558 /* Create a new string printed with the printf line arguments into a space
559    of at most LEN bytes:
560
561    rtx attr_printf (len, format, [arg1, ..., argn])  */
562
563 static char *
564 attr_printf (unsigned int len, const char *fmt, ...)
565 {
566   char str[256];
567   va_list p;
568
569   va_start (p, fmt);
570
571   gcc_assert (len < sizeof str); /* Leave room for \0.  */
572
573   vsprintf (str, fmt, p);
574   va_end (p);
575
576   return DEF_ATTR_STRING (str);
577 }
578
579 static rtx
580 attr_eq (const char *name, const char *value)
581 {
582   return attr_rtx (EQ_ATTR, DEF_ATTR_STRING (name), DEF_ATTR_STRING (value));
583 }
584
585 static const char *
586 attr_numeral (int n)
587 {
588   return XSTR (make_numeric_value (n), 0);
589 }
590
591 /* Return a permanent (possibly shared) copy of a string STR (not assumed
592    to be null terminated) with LEN bytes.  */
593
594 static char *
595 attr_string (const char *str, int len)
596 {
597   struct attr_hash *h;
598   int hashcode;
599   int i;
600   char *new_str;
601
602   /* Compute the hash code.  */
603   hashcode = (len + 1) * 613 + (unsigned) str[0];
604   for (i = 1; i < len; i += 2)
605     hashcode = ((hashcode * 613) + (unsigned) str[i]);
606   if (hashcode < 0)
607     hashcode = -hashcode;
608
609   /* Search the table for the string.  */
610   for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
611     if (h->hashcode == -hashcode && h->u.str[0] == str[0]
612         && !strncmp (h->u.str, str, len))
613       return h->u.str;                  /* <-- return if found.  */
614
615   /* Not found; create a permanent copy and add it to the hash table.  */
616   new_str = XOBNEWVAR (hash_obstack, char, len + 1);
617   memcpy (new_str, str, len);
618   new_str[len] = '\0';
619   attr_hash_add_string (hashcode, new_str);
620   copy_md_ptr_loc (new_str, str);
621
622   return new_str;                       /* Return the new string.  */
623 }
624
625 /* Check two rtx's for equality of contents,
626    taking advantage of the fact that if both are hashed
627    then they can't be equal unless they are the same object.  */
628
629 static int
630 attr_equal_p (rtx x, rtx y)
631 {
632   return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
633                      && rtx_equal_p (x, y)));
634 }
635
636 /* Copy an attribute value expression,
637    descending to all depths, but not copying any
638    permanent hashed subexpressions.  */
639
640 static rtx
641 attr_copy_rtx (rtx orig)
642 {
643   rtx copy;
644   int i, j;
645   RTX_CODE code;
646   const char *format_ptr;
647
648   /* No need to copy a permanent object.  */
649   if (ATTR_PERMANENT_P (orig))
650     return orig;
651
652   code = GET_CODE (orig);
653
654   switch (code)
655     {
656     case REG:
657     case CONST_INT:
658     case CONST_DOUBLE:
659     case CONST_VECTOR:
660     case SYMBOL_REF:
661     case CODE_LABEL:
662     case PC:
663     case CC0:
664       return orig;
665
666     default:
667       break;
668     }
669
670   copy = rtx_alloc (code);
671   PUT_MODE (copy, GET_MODE (orig));
672   ATTR_IND_SIMPLIFIED_P (copy) = ATTR_IND_SIMPLIFIED_P (orig);
673   ATTR_CURR_SIMPLIFIED_P (copy) = ATTR_CURR_SIMPLIFIED_P (orig);
674   ATTR_PERMANENT_P (copy) = ATTR_PERMANENT_P (orig);
675
676   format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
677
678   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
679     {
680       switch (*format_ptr++)
681         {
682         case 'e':
683           XEXP (copy, i) = XEXP (orig, i);
684           if (XEXP (orig, i) != NULL)
685             XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
686           break;
687
688         case 'E':
689         case 'V':
690           XVEC (copy, i) = XVEC (orig, i);
691           if (XVEC (orig, i) != NULL)
692             {
693               XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
694               for (j = 0; j < XVECLEN (copy, i); j++)
695                 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
696             }
697           break;
698
699         case 'n':
700         case 'i':
701           XINT (copy, i) = XINT (orig, i);
702           break;
703
704         case 'w':
705           XWINT (copy, i) = XWINT (orig, i);
706           break;
707
708         case 's':
709         case 'S':
710           XSTR (copy, i) = XSTR (orig, i);
711           break;
712
713         default:
714           gcc_unreachable ();
715         }
716     }
717   return copy;
718 }
719
720 /* Given a test expression for an attribute, ensure it is validly formed.
721    IS_CONST indicates whether the expression is constant for each compiler
722    run (a constant expression may not test any particular insn).
723
724    Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
725    and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")).  Do the latter
726    test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
727
728    Update the string address in EQ_ATTR expression to be the same used
729    in the attribute (or `alternative_name') to speed up subsequent
730    `find_attr' calls and eliminate most `strcmp' calls.
731
732    Return the new expression, if any.  */
733
734 static rtx
735 check_attr_test (rtx exp, int is_const, int lineno)
736 {
737   struct attr_desc *attr;
738   struct attr_value *av;
739   const char *name_ptr, *p;
740   rtx orexp, newexp;
741
742   switch (GET_CODE (exp))
743     {
744     case EQ_ATTR:
745       /* Handle negation test.  */
746       if (XSTR (exp, 1)[0] == '!')
747         return check_attr_test (attr_rtx (NOT,
748                                           attr_eq (XSTR (exp, 0),
749                                                    &XSTR (exp, 1)[1])),
750                                 is_const, lineno);
751
752       else if (n_comma_elts (XSTR (exp, 1)) == 1)
753         {
754           attr = find_attr (&XSTR (exp, 0), 0);
755           if (attr == NULL)
756             {
757               if (! strcmp (XSTR (exp, 0), "alternative"))
758                 return mk_attr_alt (1 << atoi (XSTR (exp, 1)));
759               else
760                 fatal ("unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
761             }
762
763           if (is_const && ! attr->is_const)
764             fatal ("constant expression uses insn attribute `%s' in EQ_ATTR",
765                    XSTR (exp, 0));
766
767           /* Copy this just to make it permanent,
768              so expressions using it can be permanent too.  */
769           exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
770
771           /* It shouldn't be possible to simplify the value given to a
772              constant attribute, so don't expand this until it's time to
773              write the test expression.  */
774           if (attr->is_const)
775             ATTR_IND_SIMPLIFIED_P (exp) = 1;
776
777           if (attr->is_numeric)
778             {
779               for (p = XSTR (exp, 1); *p; p++)
780                 if (! ISDIGIT (*p))
781                   fatal ("attribute `%s' takes only numeric values",
782                          XSTR (exp, 0));
783             }
784           else
785             {
786               for (av = attr->first_value; av; av = av->next)
787                 if (GET_CODE (av->value) == CONST_STRING
788                     && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
789                   break;
790
791               if (av == NULL)
792                 fatal ("unknown value `%s' for `%s' attribute",
793                        XSTR (exp, 1), XSTR (exp, 0));
794             }
795         }
796       else
797         {
798           if (! strcmp (XSTR (exp, 0), "alternative"))
799             {
800               int set = 0;
801
802               name_ptr = XSTR (exp, 1);
803               while ((p = next_comma_elt (&name_ptr)) != NULL)
804                 set |= 1 << atoi (p);
805
806               return mk_attr_alt (set);
807             }
808           else
809             {
810               /* Make an IOR tree of the possible values.  */
811               orexp = false_rtx;
812               name_ptr = XSTR (exp, 1);
813               while ((p = next_comma_elt (&name_ptr)) != NULL)
814                 {
815                   newexp = attr_eq (XSTR (exp, 0), p);
816                   orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
817                 }
818
819               return check_attr_test (orexp, is_const, lineno);
820             }
821         }
822       break;
823
824     case ATTR_FLAG:
825       break;
826
827     case CONST_INT:
828       /* Either TRUE or FALSE.  */
829       if (XWINT (exp, 0))
830         return true_rtx;
831       else
832         return false_rtx;
833
834     case IOR:
835     case AND:
836       XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
837       XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
838       break;
839
840     case NOT:
841       XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
842       break;
843
844     case MATCH_OPERAND:
845       if (is_const)
846         fatal ("RTL operator \"%s\" not valid in constant attribute test",
847                GET_RTX_NAME (GET_CODE (exp)));
848       /* These cases can't be simplified.  */
849       ATTR_IND_SIMPLIFIED_P (exp) = 1;
850       break;
851
852     case LE:  case LT:  case GT:  case GE:
853     case LEU: case LTU: case GTU: case GEU:
854     case NE:  case EQ:
855       if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
856           && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
857         exp = attr_rtx (GET_CODE (exp),
858                         attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
859                         attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
860       /* These cases can't be simplified.  */
861       ATTR_IND_SIMPLIFIED_P (exp) = 1;
862       break;
863
864     case SYMBOL_REF:
865       if (is_const)
866         {
867           /* These cases are valid for constant attributes, but can't be
868              simplified.  */
869           exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
870           ATTR_IND_SIMPLIFIED_P (exp) = 1;
871           break;
872         }
873     default:
874       fatal ("RTL operator \"%s\" not valid in attribute test",
875              GET_RTX_NAME (GET_CODE (exp)));
876     }
877
878   return exp;
879 }
880
881 /* Given an expression, ensure that it is validly formed and that all named
882    attribute values are valid for the given attribute.  Issue a fatal error
883    if not.  If no attribute is specified, assume a numeric attribute.
884
885    Return a perhaps modified replacement expression for the value.  */
886
887 static rtx
888 check_attr_value (rtx exp, struct attr_desc *attr)
889 {
890   struct attr_value *av;
891   const char *p;
892   int i;
893
894   switch (GET_CODE (exp))
895     {
896     case CONST_INT:
897       if (attr && ! attr->is_numeric)
898         {
899           error_with_line (attr->lineno,
900                            "CONST_INT not valid for non-numeric attribute %s",
901                            attr->name);
902           break;
903         }
904
905       if (INTVAL (exp) < 0)
906         {
907           error_with_line (attr->lineno,
908                            "negative numeric value specified for attribute %s",
909                            attr->name);
910           break;
911         }
912       break;
913
914     case CONST_STRING:
915       if (! strcmp (XSTR (exp, 0), "*"))
916         break;
917
918       if (attr == 0 || attr->is_numeric)
919         {
920           p = XSTR (exp, 0);
921           for (; *p; p++)
922             if (! ISDIGIT (*p))
923               {
924                 error_with_line (attr ? attr->lineno : 0,
925                                  "non-numeric value for numeric attribute %s",
926                                  attr ? attr->name : "internal");
927                 break;
928               }
929           break;
930         }
931
932       for (av = attr->first_value; av; av = av->next)
933         if (GET_CODE (av->value) == CONST_STRING
934             && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
935           break;
936
937       if (av == NULL)
938         error_with_line (attr->lineno,
939                          "unknown value `%s' for `%s' attribute",
940                          XSTR (exp, 0), attr ? attr->name : "internal");
941       break;
942
943     case IF_THEN_ELSE:
944       XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
945                                        attr ? attr->is_const : 0,
946                                        attr ? attr->lineno : 0);
947       XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
948       XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
949       break;
950
951     case PLUS:
952     case MINUS:
953     case MULT:
954     case DIV:
955     case MOD:
956       if (attr && !attr->is_numeric)
957         {
958           error_with_line (attr->lineno,
959                            "invalid operation `%s' for non-numeric"
960                            " attribute value", GET_RTX_NAME (GET_CODE (exp)));
961           break;
962         }
963       /* Fall through.  */
964
965     case IOR:
966     case AND:
967       XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
968       XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
969       break;
970
971     case FFS:
972     case CLZ:
973     case CTZ:
974     case POPCOUNT:
975     case PARITY:
976     case BSWAP:
977       XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
978       break;
979
980     case COND:
981       if (XVECLEN (exp, 0) % 2 != 0)
982         {
983           error_with_line (attr->lineno,
984                            "first operand of COND must have even length");
985           break;
986         }
987
988       for (i = 0; i < XVECLEN (exp, 0); i += 2)
989         {
990           XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
991                                                  attr ? attr->is_const : 0,
992                                                  attr ? attr->lineno : 0);
993           XVECEXP (exp, 0, i + 1)
994             = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
995         }
996
997       XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
998       break;
999
1000     case ATTR:
1001       {
1002         struct attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
1003         if (attr2 == NULL)
1004           error_with_line (attr ? attr->lineno : 0,
1005                            "unknown attribute `%s' in ATTR",
1006                            XSTR (exp, 0));
1007         else if (attr && attr->is_const && ! attr2->is_const)
1008           error_with_line (attr->lineno,
1009                            "non-constant attribute `%s' referenced from `%s'",
1010                            XSTR (exp, 0), attr->name);
1011         else if (attr
1012                  && attr->is_numeric != attr2->is_numeric)
1013           error_with_line (attr->lineno,
1014                            "numeric attribute mismatch calling `%s' from `%s'",
1015                            XSTR (exp, 0), attr->name);
1016       }
1017       break;
1018
1019     case SYMBOL_REF:
1020       /* A constant SYMBOL_REF is valid as a constant attribute test and
1021          is expanded later by make_canonical into a COND.  In a non-constant
1022          attribute test, it is left be.  */
1023       return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1024
1025     default:
1026       error_with_line (attr ? attr->lineno : 0,
1027                        "invalid operation `%s' for attribute value",
1028                        GET_RTX_NAME (GET_CODE (exp)));
1029       break;
1030     }
1031
1032   return exp;
1033 }
1034
1035 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1036    It becomes a COND with each test being (eq_attr "alternative" "n") */
1037
1038 static rtx
1039 convert_set_attr_alternative (rtx exp, struct insn_def *id)
1040 {
1041   int num_alt = id->num_alternatives;
1042   rtx condexp;
1043   int i;
1044
1045   if (XVECLEN (exp, 1) != num_alt)
1046     {
1047       error_with_line (id->lineno,
1048                        "bad number of entries in SET_ATTR_ALTERNATIVE");
1049       return NULL_RTX;
1050     }
1051
1052   /* Make a COND with all tests but the last.  Select the last value via the
1053      default.  */
1054   condexp = rtx_alloc (COND);
1055   XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1056
1057   for (i = 0; i < num_alt - 1; i++)
1058     {
1059       const char *p;
1060       p = attr_numeral (i);
1061
1062       XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1063       XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1064     }
1065
1066   XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1067
1068   return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1069 }
1070
1071 /* Given a SET_ATTR, convert to the appropriate SET.  If a comma-separated
1072    list of values is given, convert to SET_ATTR_ALTERNATIVE first.  */
1073
1074 static rtx
1075 convert_set_attr (rtx exp, struct insn_def *id)
1076 {
1077   rtx newexp;
1078   const char *name_ptr;
1079   char *p;
1080   int n;
1081
1082   /* See how many alternative specified.  */
1083   n = n_comma_elts (XSTR (exp, 1));
1084   if (n == 1)
1085     return attr_rtx (SET,
1086                      attr_rtx (ATTR, XSTR (exp, 0)),
1087                      attr_rtx (CONST_STRING, XSTR (exp, 1)));
1088
1089   newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1090   XSTR (newexp, 0) = XSTR (exp, 0);
1091   XVEC (newexp, 1) = rtvec_alloc (n);
1092
1093   /* Process each comma-separated name.  */
1094   name_ptr = XSTR (exp, 1);
1095   n = 0;
1096   while ((p = next_comma_elt (&name_ptr)) != NULL)
1097     XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1098
1099   return convert_set_attr_alternative (newexp, id);
1100 }
1101
1102 /* Scan all definitions, checking for validity.  Also, convert any SET_ATTR
1103    and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1104    expressions.  */
1105
1106 static void
1107 check_defs (void)
1108 {
1109   struct insn_def *id;
1110   struct attr_desc *attr;
1111   int i;
1112   rtx value;
1113
1114   for (id = defs; id; id = id->next)
1115     {
1116       if (XVEC (id->def, id->vec_idx) == NULL)
1117         continue;
1118
1119       for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1120         {
1121           value = XVECEXP (id->def, id->vec_idx, i);
1122           switch (GET_CODE (value))
1123             {
1124             case SET:
1125               if (GET_CODE (XEXP (value, 0)) != ATTR)
1126                 {
1127                   error_with_line (id->lineno, "bad attribute set");
1128                   value = NULL_RTX;
1129                 }
1130               break;
1131
1132             case SET_ATTR_ALTERNATIVE:
1133               value = convert_set_attr_alternative (value, id);
1134               break;
1135
1136             case SET_ATTR:
1137               value = convert_set_attr (value, id);
1138               break;
1139
1140             default:
1141               error_with_line (id->lineno, "invalid attribute code %s",
1142                                GET_RTX_NAME (GET_CODE (value)));
1143               value = NULL_RTX;
1144             }
1145           if (value == NULL_RTX)
1146             continue;
1147
1148           if ((attr = find_attr (&XSTR (XEXP (value, 0), 0), 0)) == NULL)
1149             {
1150               error_with_line (id->lineno, "unknown attribute %s",
1151                                XSTR (XEXP (value, 0), 0));
1152               continue;
1153             }
1154
1155           XVECEXP (id->def, id->vec_idx, i) = value;
1156           XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1157         }
1158     }
1159 }
1160
1161 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1162    expressions by converting them into a COND.  This removes cases from this
1163    program.  Also, replace an attribute value of "*" with the default attribute
1164    value.  */
1165
1166 static rtx
1167 make_canonical (struct attr_desc *attr, rtx exp)
1168 {
1169   int i;
1170   rtx newexp;
1171
1172   switch (GET_CODE (exp))
1173     {
1174     case CONST_INT:
1175       exp = make_numeric_value (INTVAL (exp));
1176       break;
1177
1178     case CONST_STRING:
1179       if (! strcmp (XSTR (exp, 0), "*"))
1180         {
1181           if (attr == 0 || attr->default_val == 0)
1182             fatal ("(attr_value \"*\") used in invalid context");
1183           exp = attr->default_val->value;
1184         }
1185       else
1186         XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1187
1188       break;
1189
1190     case SYMBOL_REF:
1191       if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
1192         break;
1193       /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1194          This makes the COND something that won't be considered an arbitrary
1195          expression by walk_attr_value.  */
1196       ATTR_IND_SIMPLIFIED_P (exp) = 1;
1197       exp = check_attr_value (exp, attr);
1198       break;
1199
1200     case IF_THEN_ELSE:
1201       newexp = rtx_alloc (COND);
1202       XVEC (newexp, 0) = rtvec_alloc (2);
1203       XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1204       XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1205
1206       XEXP (newexp, 1) = XEXP (exp, 2);
1207
1208       exp = newexp;
1209       /* Fall through to COND case since this is now a COND.  */
1210
1211     case COND:
1212       {
1213         int allsame = 1;
1214         rtx defval;
1215
1216         /* First, check for degenerate COND.  */
1217         if (XVECLEN (exp, 0) == 0)
1218           return make_canonical (attr, XEXP (exp, 1));
1219         defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1220
1221         for (i = 0; i < XVECLEN (exp, 0); i += 2)
1222           {
1223             XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1224             XVECEXP (exp, 0, i + 1)
1225               = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1226             if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1227               allsame = 0;
1228           }
1229         if (allsame)
1230           return defval;
1231       }
1232       break;
1233
1234     default:
1235       break;
1236     }
1237
1238   return exp;
1239 }
1240
1241 static rtx
1242 copy_boolean (rtx exp)
1243 {
1244   if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1245     return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1246                      copy_boolean (XEXP (exp, 1)));
1247   if (GET_CODE (exp) == MATCH_OPERAND)
1248     {
1249       XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1250       XSTR (exp, 2) = DEF_ATTR_STRING (XSTR (exp, 2));
1251     }
1252   else if (GET_CODE (exp) == EQ_ATTR)
1253     {
1254       XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1255       XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1256     }
1257
1258   return exp;
1259 }
1260
1261 /* Given a value and an attribute description, return a `struct attr_value *'
1262    that represents that value.  This is either an existing structure, if the
1263    value has been previously encountered, or a newly-created structure.
1264
1265    `insn_code' is the code of an insn whose attribute has the specified
1266    value (-2 if not processing an insn).  We ensure that all insns for
1267    a given value have the same number of alternatives if the value checks
1268    alternatives.  */
1269
1270 static struct attr_value *
1271 get_attr_value (rtx value, struct attr_desc *attr, int insn_code)
1272 {
1273   struct attr_value *av;
1274   int num_alt = 0;
1275
1276   value = make_canonical (attr, value);
1277   if (compares_alternatives_p (value))
1278     {
1279       if (insn_code < 0 || insn_alternatives == NULL)
1280         fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1281       else
1282         num_alt = insn_alternatives[insn_code];
1283     }
1284
1285   for (av = attr->first_value; av; av = av->next)
1286     if (rtx_equal_p (value, av->value)
1287         && (num_alt == 0 || av->first_insn == NULL
1288             || insn_alternatives[av->first_insn->def->insn_code]))
1289       return av;
1290
1291   av = oballoc (struct attr_value);
1292   av->value = value;
1293   av->next = attr->first_value;
1294   attr->first_value = av;
1295   av->first_insn = NULL;
1296   av->num_insns = 0;
1297   av->has_asm_insn = 0;
1298
1299   return av;
1300 }
1301
1302 /* After all DEFINE_DELAYs have been read in, create internal attributes
1303    to generate the required routines.
1304
1305    First, we compute the number of delay slots for each insn (as a COND of
1306    each of the test expressions in DEFINE_DELAYs).  Then, if more than one
1307    delay type is specified, we compute a similar function giving the
1308    DEFINE_DELAY ordinal for each insn.
1309
1310    Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1311    tells whether a given insn can be in that delay slot.
1312
1313    Normal attribute filling and optimization expands these to contain the
1314    information needed to handle delay slots.  */
1315
1316 static void
1317 expand_delays (void)
1318 {
1319   struct delay_desc *delay;
1320   rtx condexp;
1321   rtx newexp;
1322   int i;
1323   char *p;
1324
1325   /* First, generate data for `num_delay_slots' function.  */
1326
1327   condexp = rtx_alloc (COND);
1328   XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1329   XEXP (condexp, 1) = make_numeric_value (0);
1330
1331   for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1332     {
1333       XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1334       XVECEXP (condexp, 0, i + 1)
1335         = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1336     }
1337
1338   make_internal_attr (num_delay_slots_str, condexp, ATTR_NONE);
1339
1340   /* If more than one delay type, do the same for computing the delay type.  */
1341   if (num_delays > 1)
1342     {
1343       condexp = rtx_alloc (COND);
1344       XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1345       XEXP (condexp, 1) = make_numeric_value (0);
1346
1347       for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1348         {
1349           XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1350           XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1351         }
1352
1353       make_internal_attr (delay_type_str, condexp, ATTR_SPECIAL);
1354     }
1355
1356   /* For each delay possibility and delay slot, compute an eligibility
1357      attribute for non-annulled insns and for each type of annulled (annul
1358      if true and annul if false).  */
1359   for (delay = delays; delay; delay = delay->next)
1360     {
1361       for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1362         {
1363           condexp = XVECEXP (delay->def, 1, i);
1364           if (condexp == 0)
1365             condexp = false_rtx;
1366           newexp = attr_rtx (IF_THEN_ELSE, condexp,
1367                              make_numeric_value (1), make_numeric_value (0));
1368
1369           p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
1370                            "*delay_%d_%d", delay->num, i / 3);
1371           make_internal_attr (p, newexp, ATTR_SPECIAL);
1372
1373           if (have_annul_true)
1374             {
1375               condexp = XVECEXP (delay->def, 1, i + 1);
1376               if (condexp == 0) condexp = false_rtx;
1377               newexp = attr_rtx (IF_THEN_ELSE, condexp,
1378                                  make_numeric_value (1),
1379                                  make_numeric_value (0));
1380               p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1381                                "*annul_true_%d_%d", delay->num, i / 3);
1382               make_internal_attr (p, newexp, ATTR_SPECIAL);
1383             }
1384
1385           if (have_annul_false)
1386             {
1387               condexp = XVECEXP (delay->def, 1, i + 2);
1388               if (condexp == 0) condexp = false_rtx;
1389               newexp = attr_rtx (IF_THEN_ELSE, condexp,
1390                                  make_numeric_value (1),
1391                                  make_numeric_value (0));
1392               p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1393                                "*annul_false_%d_%d", delay->num, i / 3);
1394               make_internal_attr (p, newexp, ATTR_SPECIAL);
1395             }
1396         }
1397     }
1398 }
1399
1400 /* Once all attributes and insns have been read and checked, we construct for
1401    each attribute value a list of all the insns that have that value for
1402    the attribute.  */
1403
1404 static void
1405 fill_attr (struct attr_desc *attr)
1406 {
1407   struct attr_value *av;
1408   struct insn_ent *ie;
1409   struct insn_def *id;
1410   int i;
1411   rtx value;
1412
1413   /* Don't fill constant attributes.  The value is independent of
1414      any particular insn.  */
1415   if (attr->is_const)
1416     return;
1417
1418   for (id = defs; id; id = id->next)
1419     {
1420       /* If no value is specified for this insn for this attribute, use the
1421          default.  */
1422       value = NULL;
1423       if (XVEC (id->def, id->vec_idx))
1424         for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1425           if (! strcmp_check (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
1426                               attr->name))
1427             value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
1428
1429       if (value == NULL)
1430         av = attr->default_val;
1431       else
1432         av = get_attr_value (value, attr, id->insn_code);
1433
1434       ie = oballoc (struct insn_ent);
1435       ie->def = id;
1436       insert_insn_ent (av, ie);
1437     }
1438 }
1439
1440 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
1441    test that checks relative positions of insns (uses MATCH_DUP or PC).
1442    If so, replace it with what is obtained by passing the expression to
1443    ADDRESS_FN.  If not but it is a COND or IF_THEN_ELSE, call this routine
1444    recursively on each value (including the default value).  Otherwise,
1445    return the value returned by NO_ADDRESS_FN applied to EXP.  */
1446
1447 static rtx
1448 substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
1449                     rtx (*address_fn) (rtx))
1450 {
1451   int i;
1452   rtx newexp;
1453
1454   if (GET_CODE (exp) == COND)
1455     {
1456       /* See if any tests use addresses.  */
1457       address_used = 0;
1458       for (i = 0; i < XVECLEN (exp, 0); i += 2)
1459         walk_attr_value (XVECEXP (exp, 0, i));
1460
1461       if (address_used)
1462         return (*address_fn) (exp);
1463
1464       /* Make a new copy of this COND, replacing each element.  */
1465       newexp = rtx_alloc (COND);
1466       XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1467       for (i = 0; i < XVECLEN (exp, 0); i += 2)
1468         {
1469           XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
1470           XVECEXP (newexp, 0, i + 1)
1471             = substitute_address (XVECEXP (exp, 0, i + 1),
1472                                   no_address_fn, address_fn);
1473         }
1474
1475       XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
1476                                              no_address_fn, address_fn);
1477
1478       return newexp;
1479     }
1480
1481   else if (GET_CODE (exp) == IF_THEN_ELSE)
1482     {
1483       address_used = 0;
1484       walk_attr_value (XEXP (exp, 0));
1485       if (address_used)
1486         return (*address_fn) (exp);
1487
1488       return attr_rtx (IF_THEN_ELSE,
1489                        substitute_address (XEXP (exp, 0),
1490                                            no_address_fn, address_fn),
1491                        substitute_address (XEXP (exp, 1),
1492                                            no_address_fn, address_fn),
1493                        substitute_address (XEXP (exp, 2),
1494                                            no_address_fn, address_fn));
1495     }
1496
1497   return (*no_address_fn) (exp);
1498 }
1499
1500 /* Make new attributes from the `length' attribute.  The following are made,
1501    each corresponding to a function called from `shorten_branches' or
1502    `get_attr_length':
1503
1504    *insn_default_length         This is the length of the insn to be returned
1505                                 by `get_attr_length' before `shorten_branches'
1506                                 has been called.  In each case where the length
1507                                 depends on relative addresses, the largest
1508                                 possible is used.  This routine is also used
1509                                 to compute the initial size of the insn.
1510
1511    *insn_variable_length_p      This returns 1 if the insn's length depends
1512                                 on relative addresses, zero otherwise.
1513
1514    *insn_current_length         This is only called when it is known that the
1515                                 insn has a variable length and returns the
1516                                 current length, based on relative addresses.
1517   */
1518
1519 static void
1520 make_length_attrs (void)
1521 {
1522   static const char *new_names[] =
1523     {
1524       "*insn_default_length",
1525       "*insn_min_length",
1526       "*insn_variable_length_p",
1527       "*insn_current_length"
1528     };
1529   static rtx (*const no_address_fn[]) (rtx)
1530     = {identity_fn,identity_fn, zero_fn, zero_fn};
1531   static rtx (*const address_fn[]) (rtx)
1532     = {max_fn, min_fn, one_fn, identity_fn};
1533   size_t i;
1534   struct attr_desc *length_attr, *new_attr;
1535   struct attr_value *av, *new_av;
1536   struct insn_ent *ie, *new_ie;
1537
1538   /* See if length attribute is defined.  If so, it must be numeric.  Make
1539      it special so we don't output anything for it.  */
1540   length_attr = find_attr (&length_str, 0);
1541   if (length_attr == 0)
1542     return;
1543
1544   if (! length_attr->is_numeric)
1545     fatal ("length attribute must be numeric");
1546
1547   length_attr->is_const = 0;
1548   length_attr->is_special = 1;
1549
1550   /* Make each new attribute, in turn.  */
1551   for (i = 0; i < ARRAY_SIZE (new_names); i++)
1552     {
1553       make_internal_attr (new_names[i],
1554                           substitute_address (length_attr->default_val->value,
1555                                               no_address_fn[i], address_fn[i]),
1556                           ATTR_NONE);
1557       new_attr = find_attr (&new_names[i], 0);
1558       for (av = length_attr->first_value; av; av = av->next)
1559         for (ie = av->first_insn; ie; ie = ie->next)
1560           {
1561             new_av = get_attr_value (substitute_address (av->value,
1562                                                          no_address_fn[i],
1563                                                          address_fn[i]),
1564                                      new_attr, ie->def->insn_code);
1565             new_ie = oballoc (struct insn_ent);
1566             new_ie->def = ie->def;
1567             insert_insn_ent (new_av, new_ie);
1568           }
1569     }
1570 }
1571
1572 /* Utility functions called from above routine.  */
1573
1574 static rtx
1575 identity_fn (rtx exp)
1576 {
1577   return exp;
1578 }
1579
1580 static rtx
1581 zero_fn (rtx exp ATTRIBUTE_UNUSED)
1582 {
1583   return make_numeric_value (0);
1584 }
1585
1586 static rtx
1587 one_fn (rtx exp ATTRIBUTE_UNUSED)
1588 {
1589   return make_numeric_value (1);
1590 }
1591
1592 static rtx
1593 max_fn (rtx exp)
1594 {
1595   int unknown;
1596   return make_numeric_value (max_attr_value (exp, &unknown));
1597 }
1598
1599 static rtx
1600 min_fn (rtx exp)
1601 {
1602   int unknown;
1603   return make_numeric_value (min_attr_value (exp, &unknown));
1604 }
1605
1606 static void
1607 write_length_unit_log (void)
1608 {
1609   struct attr_desc *length_attr = find_attr (&length_str, 0);
1610   struct attr_value *av;
1611   struct insn_ent *ie;
1612   unsigned int length_unit_log, length_or;
1613   int unknown = 0;
1614
1615   if (length_attr == 0)
1616     return;
1617   length_or = or_attr_value (length_attr->default_val->value, &unknown);
1618   for (av = length_attr->first_value; av; av = av->next)
1619     for (ie = av->first_insn; ie; ie = ie->next)
1620       length_or |= or_attr_value (av->value, &unknown);
1621
1622   if (unknown)
1623     length_unit_log = 0;
1624   else
1625     {
1626       length_or = ~length_or;
1627       for (length_unit_log = 0; length_or & 1; length_or >>= 1)
1628         length_unit_log++;
1629     }
1630   printf ("EXPORTED_CONST int length_unit_log = %u;\n", length_unit_log);
1631 }
1632
1633 /* Take a COND expression and see if any of the conditions in it can be
1634    simplified.  If any are known true or known false for the particular insn
1635    code, the COND can be further simplified.
1636
1637    Also call ourselves on any COND operations that are values of this COND.
1638
1639    We do not modify EXP; rather, we make and return a new rtx.  */
1640
1641 static rtx
1642 simplify_cond (rtx exp, int insn_code, int insn_index)
1643 {
1644   int i, j;
1645   /* We store the desired contents here,
1646      then build a new expression if they don't match EXP.  */
1647   rtx defval = XEXP (exp, 1);
1648   rtx new_defval = XEXP (exp, 1);
1649   int len = XVECLEN (exp, 0);
1650   rtx *tests = XNEWVEC (rtx, len);
1651   int allsame = 1;
1652   rtx ret;
1653
1654   /* This lets us free all storage allocated below, if appropriate.  */
1655   obstack_finish (rtl_obstack);
1656
1657   memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
1658
1659   /* See if default value needs simplification.  */
1660   if (GET_CODE (defval) == COND)
1661     new_defval = simplify_cond (defval, insn_code, insn_index);
1662
1663   /* Simplify the subexpressions, and see what tests we can get rid of.  */
1664
1665   for (i = 0; i < len; i += 2)
1666     {
1667       rtx newtest, newval;
1668
1669       /* Simplify this test.  */
1670       newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
1671       tests[i] = newtest;
1672
1673       newval = tests[i + 1];
1674       /* See if this value may need simplification.  */
1675       if (GET_CODE (newval) == COND)
1676         newval = simplify_cond (newval, insn_code, insn_index);
1677
1678       /* Look for ways to delete or combine this test.  */
1679       if (newtest == true_rtx)
1680         {
1681           /* If test is true, make this value the default
1682              and discard this + any following tests.  */
1683           len = i;
1684           defval = tests[i + 1];
1685           new_defval = newval;
1686         }
1687
1688       else if (newtest == false_rtx)
1689         {
1690           /* If test is false, discard it and its value.  */
1691           for (j = i; j < len - 2; j++)
1692             tests[j] = tests[j + 2];
1693           i -= 2;
1694           len -= 2;
1695         }
1696
1697       else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
1698         {
1699           /* If this value and the value for the prev test are the same,
1700              merge the tests.  */
1701
1702           tests[i - 2]
1703             = insert_right_side (IOR, tests[i - 2], newtest,
1704                                  insn_code, insn_index);
1705
1706           /* Delete this test/value.  */
1707           for (j = i; j < len - 2; j++)
1708             tests[j] = tests[j + 2];
1709           len -= 2;
1710           i -= 2;
1711         }
1712
1713       else
1714         tests[i + 1] = newval;
1715     }
1716
1717   /* If the last test in a COND has the same value
1718      as the default value, that test isn't needed.  */
1719
1720   while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
1721     len -= 2;
1722
1723   /* See if we changed anything.  */
1724   if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
1725     allsame = 0;
1726   else
1727     for (i = 0; i < len; i++)
1728       if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
1729         {
1730           allsame = 0;
1731           break;
1732         }
1733
1734   if (len == 0)
1735     {
1736       if (GET_CODE (defval) == COND)
1737         ret = simplify_cond (defval, insn_code, insn_index);
1738       else
1739         ret = defval;
1740     }
1741   else if (allsame)
1742     ret = exp;
1743   else
1744     {
1745       rtx newexp = rtx_alloc (COND);
1746
1747       XVEC (newexp, 0) = rtvec_alloc (len);
1748       memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
1749       XEXP (newexp, 1) = new_defval;
1750       ret = newexp;
1751     }
1752   free (tests);
1753   return ret;
1754 }
1755
1756 /* Remove an insn entry from an attribute value.  */
1757
1758 static void
1759 remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
1760 {
1761   struct insn_ent *previe;
1762
1763   if (av->first_insn == ie)
1764     av->first_insn = ie->next;
1765   else
1766     {
1767       for (previe = av->first_insn; previe->next != ie; previe = previe->next)
1768         ;
1769       previe->next = ie->next;
1770     }
1771
1772   av->num_insns--;
1773   if (ie->def->insn_code == -1)
1774     av->has_asm_insn = 0;
1775
1776   num_insn_ents--;
1777 }
1778
1779 /* Insert an insn entry in an attribute value list.  */
1780
1781 static void
1782 insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
1783 {
1784   ie->next = av->first_insn;
1785   av->first_insn = ie;
1786   av->num_insns++;
1787   if (ie->def->insn_code == -1)
1788     av->has_asm_insn = 1;
1789
1790   num_insn_ents++;
1791 }
1792
1793 /* This is a utility routine to take an expression that is a tree of either
1794    AND or IOR expressions and insert a new term.  The new term will be
1795    inserted at the right side of the first node whose code does not match
1796    the root.  A new node will be created with the root's code.  Its left
1797    side will be the old right side and its right side will be the new
1798    term.
1799
1800    If the `term' is itself a tree, all its leaves will be inserted.  */
1801
1802 static rtx
1803 insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
1804 {
1805   rtx newexp;
1806
1807   /* Avoid consing in some special cases.  */
1808   if (code == AND && term == true_rtx)
1809     return exp;
1810   if (code == AND && term == false_rtx)
1811     return false_rtx;
1812   if (code == AND && exp == true_rtx)
1813     return term;
1814   if (code == AND && exp == false_rtx)
1815     return false_rtx;
1816   if (code == IOR && term == true_rtx)
1817     return true_rtx;
1818   if (code == IOR && term == false_rtx)
1819     return exp;
1820   if (code == IOR && exp == true_rtx)
1821     return true_rtx;
1822   if (code == IOR && exp == false_rtx)
1823     return term;
1824   if (attr_equal_p (exp, term))
1825     return exp;
1826
1827   if (GET_CODE (term) == code)
1828     {
1829       exp = insert_right_side (code, exp, XEXP (term, 0),
1830                                insn_code, insn_index);
1831       exp = insert_right_side (code, exp, XEXP (term, 1),
1832                                insn_code, insn_index);
1833
1834       return exp;
1835     }
1836
1837   if (GET_CODE (exp) == code)
1838     {
1839       rtx new_rtx = insert_right_side (code, XEXP (exp, 1),
1840                                        term, insn_code, insn_index);
1841       if (new_rtx != XEXP (exp, 1))
1842         /* Make a copy of this expression and call recursively.  */
1843         newexp = attr_rtx (code, XEXP (exp, 0), new_rtx);
1844       else
1845         newexp = exp;
1846     }
1847   else
1848     {
1849       /* Insert the new term.  */
1850       newexp = attr_rtx (code, exp, term);
1851     }
1852
1853   return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
1854 }
1855
1856 /* If we have an expression which AND's a bunch of
1857         (not (eq_attrq "alternative" "n"))
1858    terms, we may have covered all or all but one of the possible alternatives.
1859    If so, we can optimize.  Similarly for IOR's of EQ_ATTR.
1860
1861    This routine is passed an expression and either AND or IOR.  It returns a
1862    bitmask indicating which alternatives are mentioned within EXP.  */
1863
1864 static int
1865 compute_alternative_mask (rtx exp, enum rtx_code code)
1866 {
1867   const char *string;
1868   if (GET_CODE (exp) == code)
1869     return compute_alternative_mask (XEXP (exp, 0), code)
1870            | compute_alternative_mask (XEXP (exp, 1), code);
1871
1872   else if (code == AND && GET_CODE (exp) == NOT
1873            && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
1874            && XSTR (XEXP (exp, 0), 0) == alternative_name)
1875     string = XSTR (XEXP (exp, 0), 1);
1876
1877   else if (code == IOR && GET_CODE (exp) == EQ_ATTR
1878            && XSTR (exp, 0) == alternative_name)
1879     string = XSTR (exp, 1);
1880
1881   else if (GET_CODE (exp) == EQ_ATTR_ALT)
1882     {
1883       if (code == AND && XINT (exp, 1))
1884         return XINT (exp, 0);
1885
1886       if (code == IOR && !XINT (exp, 1))
1887         return XINT (exp, 0);
1888
1889       return 0;
1890     }
1891   else
1892     return 0;
1893
1894   if (string[1] == 0)
1895     return 1 << (string[0] - '0');
1896   return 1 << atoi (string);
1897 }
1898
1899 /* Given I, a single-bit mask, return RTX to compare the `alternative'
1900    attribute with the value represented by that bit.  */
1901
1902 static rtx
1903 make_alternative_compare (int mask)
1904 {
1905   return mk_attr_alt (mask);
1906 }
1907
1908 /* If we are processing an (eq_attr "attr" "value") test, we find the value
1909    of "attr" for this insn code.  From that value, we can compute a test
1910    showing when the EQ_ATTR will be true.  This routine performs that
1911    computation.  If a test condition involves an address, we leave the EQ_ATTR
1912    intact because addresses are only valid for the `length' attribute.
1913
1914    EXP is the EQ_ATTR expression and ATTR is the attribute to which
1915    it refers.  VALUE is the value of that attribute for the insn
1916    corresponding to INSN_CODE and INSN_INDEX.  */
1917
1918 static rtx
1919 evaluate_eq_attr (rtx exp, struct attr_desc *attr, rtx value,
1920                   int insn_code, int insn_index)
1921 {
1922   rtx orexp, andexp;
1923   rtx right;
1924   rtx newexp;
1925   int i;
1926
1927   while (GET_CODE (value) == ATTR)
1928     {
1929       struct attr_value *av = NULL;
1930
1931       attr = find_attr (&XSTR (value, 0), 0);
1932
1933       if (insn_code_values)
1934         {
1935           struct attr_value_list *iv;
1936           for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
1937             if (iv->attr == attr)
1938               {
1939                 av = iv->av;
1940                 break;
1941               }
1942         }
1943       else
1944         {
1945           struct insn_ent *ie;
1946           for (av = attr->first_value; av; av = av->next)
1947             for (ie = av->first_insn; ie; ie = ie->next)
1948               if (ie->def->insn_code == insn_code)
1949                 goto got_av;
1950         }
1951       if (av)
1952         {
1953         got_av:
1954           value = av->value;
1955         }
1956     }
1957
1958   switch (GET_CODE (value))
1959     {
1960     case CONST_STRING:
1961       if (! strcmp_check (XSTR (value, 0), XSTR (exp, 1)))
1962         newexp = true_rtx;
1963       else
1964         newexp = false_rtx;
1965       break;
1966
1967     case SYMBOL_REF:
1968       {
1969         const char *prefix;
1970         char *string, *p;
1971
1972         gcc_assert (GET_CODE (exp) == EQ_ATTR);
1973         prefix = attr->enum_name ? attr->enum_name : attr->name;
1974         string = ACONCAT ((prefix, "_", XSTR (exp, 1), NULL));
1975         for (p = string; *p; p++)
1976           *p = TOUPPER (*p);
1977
1978         newexp = attr_rtx (EQ, value,
1979                            attr_rtx (SYMBOL_REF,
1980                                      DEF_ATTR_STRING (string)));
1981         break;
1982       }
1983
1984     case COND:
1985       /* We construct an IOR of all the cases for which the
1986          requested attribute value is present.  Since we start with
1987          FALSE, if it is not present, FALSE will be returned.
1988
1989          Each case is the AND of the NOT's of the previous conditions with the
1990          current condition; in the default case the current condition is TRUE.
1991
1992          For each possible COND value, call ourselves recursively.
1993
1994          The extra TRUE and FALSE expressions will be eliminated by another
1995          call to the simplification routine.  */
1996
1997       orexp = false_rtx;
1998       andexp = true_rtx;
1999
2000       for (i = 0; i < XVECLEN (value, 0); i += 2)
2001         {
2002           rtx this_cond = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
2003                                                     insn_code, insn_index);
2004
2005           right = insert_right_side (AND, andexp, this_cond,
2006                                      insn_code, insn_index);
2007           right = insert_right_side (AND, right,
2008                                      evaluate_eq_attr (exp, attr,
2009                                                        XVECEXP (value, 0,
2010                                                                 i + 1),
2011                                                        insn_code, insn_index),
2012                                      insn_code, insn_index);
2013           orexp = insert_right_side (IOR, orexp, right,
2014                                      insn_code, insn_index);
2015
2016           /* Add this condition into the AND expression.  */
2017           newexp = attr_rtx (NOT, this_cond);
2018           andexp = insert_right_side (AND, andexp, newexp,
2019                                       insn_code, insn_index);
2020         }
2021
2022       /* Handle the default case.  */
2023       right = insert_right_side (AND, andexp,
2024                                  evaluate_eq_attr (exp, attr, XEXP (value, 1),
2025                                                    insn_code, insn_index),
2026                                  insn_code, insn_index);
2027       newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2028       break;
2029
2030     default:
2031       gcc_unreachable ();
2032     }
2033
2034   /* If uses an address, must return original expression.  But set the
2035      ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again.  */
2036
2037   address_used = 0;
2038   walk_attr_value (newexp);
2039
2040   if (address_used)
2041     {
2042       if (! ATTR_IND_SIMPLIFIED_P (exp))
2043         return copy_rtx_unchanging (exp);
2044       return exp;
2045     }
2046   else
2047     return newexp;
2048 }
2049
2050 /* This routine is called when an AND of a term with a tree of AND's is
2051    encountered.  If the term or its complement is present in the tree, it
2052    can be replaced with TRUE or FALSE, respectively.
2053
2054    Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2055    be true and hence are complementary.
2056
2057    There is one special case:  If we see
2058         (and (not (eq_attr "att" "v1"))
2059              (eq_attr "att" "v2"))
2060    this can be replaced by (eq_attr "att" "v2").  To do this we need to
2061    replace the term, not anything in the AND tree.  So we pass a pointer to
2062    the term.  */
2063
2064 static rtx
2065 simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2066 {
2067   rtx left, right;
2068   rtx newexp;
2069   rtx temp;
2070   int left_eliminates_term, right_eliminates_term;
2071
2072   if (GET_CODE (exp) == AND)
2073     {
2074       left  = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2075       right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2076       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2077         {
2078           newexp = attr_rtx (AND, left, right);
2079
2080           exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2081         }
2082     }
2083
2084   else if (GET_CODE (exp) == IOR)
2085     {
2086       /* For the IOR case, we do the same as above, except that we can
2087          only eliminate `term' if both sides of the IOR would do so.  */
2088       temp = *pterm;
2089       left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2090       left_eliminates_term = (temp == true_rtx);
2091
2092       temp = *pterm;
2093       right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2094       right_eliminates_term = (temp == true_rtx);
2095
2096       if (left_eliminates_term && right_eliminates_term)
2097         *pterm = true_rtx;
2098
2099       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2100         {
2101           newexp = attr_rtx (IOR, left, right);
2102
2103           exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2104         }
2105     }
2106
2107   /* Check for simplifications.  Do some extra checking here since this
2108      routine is called so many times.  */
2109
2110   if (exp == *pterm)
2111     return true_rtx;
2112
2113   else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2114     return false_rtx;
2115
2116   else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2117     return false_rtx;
2118
2119   else if (GET_CODE (exp) == EQ_ATTR_ALT && GET_CODE (*pterm) == EQ_ATTR_ALT)
2120     {
2121       if (attr_alt_subset_p (*pterm, exp))
2122         return true_rtx;
2123
2124       if (attr_alt_subset_of_compl_p (*pterm, exp))
2125         return false_rtx;
2126
2127       if (attr_alt_subset_p (exp, *pterm))
2128         *pterm = true_rtx;
2129
2130       return exp;
2131     }
2132
2133   else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2134     {
2135       if (XSTR (exp, 0) != XSTR (*pterm, 0))
2136         return exp;
2137
2138       if (! strcmp_check (XSTR (exp, 1), XSTR (*pterm, 1)))
2139         return true_rtx;
2140       else
2141         return false_rtx;
2142     }
2143
2144   else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2145            && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2146     {
2147       if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2148         return exp;
2149
2150       if (! strcmp_check (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2151         return false_rtx;
2152       else
2153         return true_rtx;
2154     }
2155
2156   else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2157            && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2158     {
2159       if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2160         return exp;
2161
2162       if (! strcmp_check (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2163         return false_rtx;
2164       else
2165         *pterm = true_rtx;
2166     }
2167
2168   else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2169     {
2170       if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2171         return true_rtx;
2172     }
2173
2174   else if (GET_CODE (exp) == NOT)
2175     {
2176       if (attr_equal_p (XEXP (exp, 0), *pterm))
2177         return false_rtx;
2178     }
2179
2180   else if (GET_CODE (*pterm) == NOT)
2181     {
2182       if (attr_equal_p (XEXP (*pterm, 0), exp))
2183         return false_rtx;
2184     }
2185
2186   else if (attr_equal_p (exp, *pterm))
2187     return true_rtx;
2188
2189   return exp;
2190 }
2191
2192 /* Similar to `simplify_and_tree', but for IOR trees.  */
2193
2194 static rtx
2195 simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2196 {
2197   rtx left, right;
2198   rtx newexp;
2199   rtx temp;
2200   int left_eliminates_term, right_eliminates_term;
2201
2202   if (GET_CODE (exp) == IOR)
2203     {
2204       left  = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2205       right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2206       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2207         {
2208           newexp = attr_rtx (GET_CODE (exp), left, right);
2209
2210           exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2211         }
2212     }
2213
2214   else if (GET_CODE (exp) == AND)
2215     {
2216       /* For the AND case, we do the same as above, except that we can
2217          only eliminate `term' if both sides of the AND would do so.  */
2218       temp = *pterm;
2219       left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2220       left_eliminates_term = (temp == false_rtx);
2221
2222       temp = *pterm;
2223       right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2224       right_eliminates_term = (temp == false_rtx);
2225
2226       if (left_eliminates_term && right_eliminates_term)
2227         *pterm = false_rtx;
2228
2229       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2230         {
2231           newexp = attr_rtx (GET_CODE (exp), left, right);
2232
2233           exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2234         }
2235     }
2236
2237   if (attr_equal_p (exp, *pterm))
2238     return false_rtx;
2239
2240   else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
2241     return true_rtx;
2242
2243   else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
2244     return true_rtx;
2245
2246   else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2247            && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2248            && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
2249     *pterm = false_rtx;
2250
2251   else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2252            && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
2253            && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
2254     return false_rtx;
2255
2256   return exp;
2257 }
2258
2259 /* Compute approximate cost of the expression.  Used to decide whether
2260    expression is cheap enough for inline.  */
2261 static int
2262 attr_rtx_cost (rtx x)
2263 {
2264   int cost = 0;
2265   enum rtx_code code;
2266   if (!x)
2267     return 0;
2268   code = GET_CODE (x);
2269   switch (code)
2270     {
2271     case MATCH_OPERAND:
2272       if (XSTR (x, 1)[0])
2273         return 10;
2274       else
2275         return 0;
2276
2277     case EQ_ATTR_ALT:
2278       return 0;
2279
2280     case EQ_ATTR:
2281       /* Alternatives don't result into function call.  */
2282       if (!strcmp_check (XSTR (x, 0), alternative_name))
2283         return 0;
2284       else
2285         return 5;
2286     default:
2287       {
2288         int i, j;
2289         const char *fmt = GET_RTX_FORMAT (code);
2290         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2291           {
2292             switch (fmt[i])
2293               {
2294               case 'V':
2295               case 'E':
2296                 for (j = 0; j < XVECLEN (x, i); j++)
2297                   cost += attr_rtx_cost (XVECEXP (x, i, j));
2298                 break;
2299               case 'e':
2300                 cost += attr_rtx_cost (XEXP (x, i));
2301                 break;
2302               }
2303           }
2304       }
2305       break;
2306     }
2307   return cost;
2308 }
2309
2310 /* Simplify test expression and use temporary obstack in order to avoid
2311    memory bloat.  Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
2312    and avoid unnecessary copying if possible.  */
2313
2314 static rtx
2315 simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
2316 {
2317   rtx x;
2318   struct obstack *old;
2319   if (ATTR_IND_SIMPLIFIED_P (exp))
2320     return exp;
2321   old = rtl_obstack;
2322   rtl_obstack = temp_obstack;
2323   x = simplify_test_exp (exp, insn_code, insn_index);
2324   rtl_obstack = old;
2325   if (x == exp || rtl_obstack == temp_obstack)
2326     return x;
2327   return attr_copy_rtx (x);
2328 }
2329
2330 /* Returns true if S1 is a subset of S2.  */
2331
2332 static bool
2333 attr_alt_subset_p (rtx s1, rtx s2)
2334 {
2335   switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2336     {
2337     case (0 << 1) | 0:
2338       return !(XINT (s1, 0) &~ XINT (s2, 0));
2339
2340     case (0 << 1) | 1:
2341       return !(XINT (s1, 0) & XINT (s2, 0));
2342
2343     case (1 << 1) | 0:
2344       return false;
2345
2346     case (1 << 1) | 1:
2347       return !(XINT (s2, 0) &~ XINT (s1, 0));
2348
2349     default:
2350       gcc_unreachable ();
2351     }
2352 }
2353
2354 /* Returns true if S1 is a subset of complement of S2.  */
2355
2356 static bool
2357 attr_alt_subset_of_compl_p (rtx s1, rtx s2)
2358 {
2359   switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2360     {
2361     case (0 << 1) | 0:
2362       return !(XINT (s1, 0) & XINT (s2, 0));
2363
2364     case (0 << 1) | 1:
2365       return !(XINT (s1, 0) & ~XINT (s2, 0));
2366
2367     case (1 << 1) | 0:
2368       return !(XINT (s2, 0) &~ XINT (s1, 0));
2369
2370     case (1 << 1) | 1:
2371       return false;
2372
2373     default:
2374       gcc_unreachable ();
2375     }
2376 }
2377
2378 /* Return EQ_ATTR_ALT expression representing intersection of S1 and S2.  */
2379
2380 static rtx
2381 attr_alt_intersection (rtx s1, rtx s2)
2382 {
2383   rtx result = rtx_alloc (EQ_ATTR_ALT);
2384
2385   switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2386     {
2387     case (0 << 1) | 0:
2388       XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
2389       break;
2390     case (0 << 1) | 1:
2391       XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
2392       break;
2393     case (1 << 1) | 0:
2394       XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
2395       break;
2396     case (1 << 1) | 1:
2397       XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
2398       break;
2399     default:
2400       gcc_unreachable ();
2401     }
2402   XINT (result, 1) = XINT (s1, 1) & XINT (s2, 1);
2403
2404   return result;
2405 }
2406
2407 /* Return EQ_ATTR_ALT expression representing union of S1 and S2.  */
2408
2409 static rtx
2410 attr_alt_union (rtx s1, rtx s2)
2411 {
2412   rtx result = rtx_alloc (EQ_ATTR_ALT);
2413
2414   switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2415     {
2416     case (0 << 1) | 0:
2417       XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
2418       break;
2419     case (0 << 1) | 1:
2420       XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
2421       break;
2422     case (1 << 1) | 0:
2423       XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
2424       break;
2425     case (1 << 1) | 1:
2426       XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
2427       break;
2428     default:
2429       gcc_unreachable ();
2430     }
2431
2432   XINT (result, 1) = XINT (s1, 1) | XINT (s2, 1);
2433   return result;
2434 }
2435
2436 /* Return EQ_ATTR_ALT expression representing complement of S.  */
2437
2438 static rtx
2439 attr_alt_complement (rtx s)
2440 {
2441   rtx result = rtx_alloc (EQ_ATTR_ALT);
2442
2443   XINT (result, 0) = XINT (s, 0);
2444   XINT (result, 1) = 1 - XINT (s, 1);
2445
2446   return result;
2447 }
2448
2449 /* Return EQ_ATTR_ALT expression representing set containing elements set
2450    in E.  */
2451
2452 static rtx
2453 mk_attr_alt (int e)
2454 {
2455   rtx result = rtx_alloc (EQ_ATTR_ALT);
2456
2457   XINT (result, 0) = e;
2458   XINT (result, 1) = 0;
2459
2460   return result;
2461 }
2462
2463 /* Given an expression, see if it can be simplified for a particular insn
2464    code based on the values of other attributes being tested.  This can
2465    eliminate nested get_attr_... calls.
2466
2467    Note that if an endless recursion is specified in the patterns, the
2468    optimization will loop.  However, it will do so in precisely the cases where
2469    an infinite recursion loop could occur during compilation.  It's better that
2470    it occurs here!  */
2471
2472 static rtx
2473 simplify_test_exp (rtx exp, int insn_code, int insn_index)
2474 {
2475   rtx left, right;
2476   struct attr_desc *attr;
2477   struct attr_value *av;
2478   struct insn_ent *ie;
2479   struct attr_value_list *iv;
2480   int i;
2481   rtx newexp = exp;
2482   bool left_alt, right_alt;
2483
2484   /* Don't re-simplify something we already simplified.  */
2485   if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
2486     return exp;
2487
2488   switch (GET_CODE (exp))
2489     {
2490     case AND:
2491       left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2492       if (left == false_rtx)
2493         return false_rtx;
2494       right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2495       if (right == false_rtx)
2496         return false_rtx;
2497
2498       if (GET_CODE (left) == EQ_ATTR_ALT
2499           && GET_CODE (right) == EQ_ATTR_ALT)
2500         {
2501           exp = attr_alt_intersection (left, right);
2502           return simplify_test_exp (exp, insn_code, insn_index);
2503         }
2504
2505       /* If either side is an IOR and we have (eq_attr "alternative" ..")
2506          present on both sides, apply the distributive law since this will
2507          yield simplifications.  */
2508       if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
2509           && compute_alternative_mask (left, IOR)
2510           && compute_alternative_mask (right, IOR))
2511         {
2512           if (GET_CODE (left) == IOR)
2513             {
2514               rtx tem = left;
2515               left = right;
2516               right = tem;
2517             }
2518
2519           newexp = attr_rtx (IOR,
2520                              attr_rtx (AND, left, XEXP (right, 0)),
2521                              attr_rtx (AND, left, XEXP (right, 1)));
2522
2523           return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2524         }
2525
2526       /* Try with the term on both sides.  */
2527       right = simplify_and_tree (right, &left, insn_code, insn_index);
2528       if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2529         left = simplify_and_tree (left, &right, insn_code, insn_index);
2530
2531       if (left == false_rtx || right == false_rtx)
2532         return false_rtx;
2533       else if (left == true_rtx)
2534         {
2535           return right;
2536         }
2537       else if (right == true_rtx)
2538         {
2539           return left;
2540         }
2541       /* See if all or all but one of the insn's alternatives are specified
2542          in this tree.  Optimize if so.  */
2543
2544       if (GET_CODE (left) == NOT)
2545         left_alt = (GET_CODE (XEXP (left, 0)) == EQ_ATTR
2546                     && XSTR (XEXP (left, 0), 0) == alternative_name);
2547       else
2548         left_alt = (GET_CODE (left) == EQ_ATTR_ALT
2549                     && XINT (left, 1));
2550
2551       if (GET_CODE (right) == NOT)
2552         right_alt = (GET_CODE (XEXP (right, 0)) == EQ_ATTR
2553                      && XSTR (XEXP (right, 0), 0) == alternative_name);
2554       else
2555         right_alt = (GET_CODE (right) == EQ_ATTR_ALT
2556                      && XINT (right, 1));
2557
2558       if (insn_code >= 0
2559           && (GET_CODE (left) == AND
2560               || left_alt
2561               || GET_CODE (right) == AND
2562               || right_alt))
2563         {
2564           i = compute_alternative_mask (exp, AND);
2565           if (i & ~insn_alternatives[insn_code])
2566             fatal ("invalid alternative specified for pattern number %d",
2567                    insn_index);
2568
2569           /* If all alternatives are excluded, this is false.  */
2570           i ^= insn_alternatives[insn_code];
2571           if (i == 0)
2572             return false_rtx;
2573           else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2574             {
2575               /* If just one excluded, AND a comparison with that one to the
2576                  front of the tree.  The others will be eliminated by
2577                  optimization.  We do not want to do this if the insn has one
2578                  alternative and we have tested none of them!  */
2579               left = make_alternative_compare (i);
2580               right = simplify_and_tree (exp, &left, insn_code, insn_index);
2581               newexp = attr_rtx (AND, left, right);
2582
2583               return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2584             }
2585         }
2586
2587       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2588         {
2589           newexp = attr_rtx (AND, left, right);
2590           return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2591         }
2592       break;
2593
2594     case IOR:
2595       left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2596       if (left == true_rtx)
2597         return true_rtx;
2598       right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2599       if (right == true_rtx)
2600         return true_rtx;
2601
2602       if (GET_CODE (left) == EQ_ATTR_ALT
2603           && GET_CODE (right) == EQ_ATTR_ALT)
2604         {
2605           exp = attr_alt_union (left, right);
2606           return simplify_test_exp (exp, insn_code, insn_index);
2607         }
2608
2609       right = simplify_or_tree (right, &left, insn_code, insn_index);
2610       if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2611         left = simplify_or_tree (left, &right, insn_code, insn_index);
2612
2613       if (right == true_rtx || left == true_rtx)
2614         return true_rtx;
2615       else if (left == false_rtx)
2616         {
2617           return right;
2618         }
2619       else if (right == false_rtx)
2620         {
2621           return left;
2622         }
2623
2624       /* Test for simple cases where the distributive law is useful.  I.e.,
2625             convert (ior (and (x) (y))
2626                          (and (x) (z)))
2627             to      (and (x)
2628                          (ior (y) (z)))
2629        */
2630
2631       else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2632                && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
2633         {
2634           newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
2635
2636           left = XEXP (left, 0);
2637           right = newexp;
2638           newexp = attr_rtx (AND, left, right);
2639           return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2640         }
2641
2642       /* See if all or all but one of the insn's alternatives are specified
2643          in this tree.  Optimize if so.  */
2644
2645       else if (insn_code >= 0
2646                && (GET_CODE (left) == IOR
2647                    || (GET_CODE (left) == EQ_ATTR_ALT
2648                        && !XINT (left, 1))
2649                    || (GET_CODE (left) == EQ_ATTR
2650                        && XSTR (left, 0) == alternative_name)
2651                    || GET_CODE (right) == IOR
2652                    || (GET_CODE (right) == EQ_ATTR_ALT
2653                        && !XINT (right, 1))
2654                    || (GET_CODE (right) == EQ_ATTR
2655                        && XSTR (right, 0) == alternative_name)))
2656         {
2657           i = compute_alternative_mask (exp, IOR);
2658           if (i & ~insn_alternatives[insn_code])
2659             fatal ("invalid alternative specified for pattern number %d",
2660                    insn_index);
2661
2662           /* If all alternatives are included, this is true.  */
2663           i ^= insn_alternatives[insn_code];
2664           if (i == 0)
2665             return true_rtx;
2666           else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2667             {
2668               /* If just one excluded, IOR a comparison with that one to the
2669                  front of the tree.  The others will be eliminated by
2670                  optimization.  We do not want to do this if the insn has one
2671                  alternative and we have tested none of them!  */
2672               left = make_alternative_compare (i);
2673               right = simplify_and_tree (exp, &left, insn_code, insn_index);
2674               newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
2675
2676               return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2677             }
2678         }
2679
2680       if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2681         {
2682           newexp = attr_rtx (IOR, left, right);
2683           return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2684         }
2685       break;
2686
2687     case NOT:
2688       if (GET_CODE (XEXP (exp, 0)) == NOT)
2689         {
2690           left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
2691                                     insn_code, insn_index);
2692           return left;
2693         }
2694
2695       left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2696       if (GET_CODE (left) == NOT)
2697         return XEXP (left, 0);
2698
2699       if (left == false_rtx)
2700         return true_rtx;
2701       if (left == true_rtx)
2702         return false_rtx;
2703
2704       if (GET_CODE (left) == EQ_ATTR_ALT)
2705         {
2706           exp = attr_alt_complement (left);
2707           return simplify_test_exp (exp, insn_code, insn_index);
2708         }
2709
2710       /* Try to apply De`Morgan's laws.  */
2711       if (GET_CODE (left) == IOR)
2712         {
2713           newexp = attr_rtx (AND,
2714                              attr_rtx (NOT, XEXP (left, 0)),
2715                              attr_rtx (NOT, XEXP (left, 1)));
2716
2717           newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2718         }
2719       else if (GET_CODE (left) == AND)
2720         {
2721           newexp = attr_rtx (IOR,
2722                              attr_rtx (NOT, XEXP (left, 0)),
2723                              attr_rtx (NOT, XEXP (left, 1)));
2724
2725           newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2726         }
2727       else if (left != XEXP (exp, 0))
2728         {
2729           newexp = attr_rtx (NOT, left);
2730         }
2731       break;
2732
2733     case EQ_ATTR_ALT:
2734       if (!XINT (exp, 0))
2735         return XINT (exp, 1) ? true_rtx : false_rtx;
2736       break;
2737
2738     case EQ_ATTR:
2739       if (XSTR (exp, 0) == alternative_name)
2740         {
2741           newexp = mk_attr_alt (1 << atoi (XSTR (exp, 1)));
2742           break;
2743         }
2744
2745       /* Look at the value for this insn code in the specified attribute.
2746          We normally can replace this comparison with the condition that
2747          would give this insn the values being tested for.  */
2748       if (insn_code >= 0
2749           && (attr = find_attr (&XSTR (exp, 0), 0)) != NULL)
2750         {
2751           rtx x;
2752
2753           av = NULL;
2754           if (insn_code_values)
2755             {
2756               for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
2757                 if (iv->attr == attr)
2758                   {
2759                     av = iv->av;
2760                     break;
2761                   }
2762             }
2763           else
2764             {
2765               for (av = attr->first_value; av; av = av->next)
2766                 for (ie = av->first_insn; ie; ie = ie->next)
2767                   if (ie->def->insn_code == insn_code)
2768                     goto got_av;
2769             }
2770
2771           if (av)
2772             {
2773             got_av:
2774               x = evaluate_eq_attr (exp, attr, av->value,
2775                                     insn_code, insn_index);
2776               x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
2777               if (attr_rtx_cost(x) < 20)
2778                 return x;
2779             }
2780         }
2781       break;
2782
2783     default:
2784       break;
2785     }
2786
2787   /* We have already simplified this expression.  Simplifying it again
2788      won't buy anything unless we weren't given a valid insn code
2789      to process (i.e., we are canonicalizing something.).  */
2790   if (insn_code != -2
2791       && ! ATTR_IND_SIMPLIFIED_P (newexp))
2792     return copy_rtx_unchanging (newexp);
2793
2794   return newexp;
2795 }
2796
2797 /* Optimize the attribute lists by seeing if we can determine conditional
2798    values from the known values of other attributes.  This will save subroutine
2799    calls during the compilation.  */
2800
2801 static void
2802 optimize_attrs (void)
2803 {
2804   struct attr_desc *attr;
2805   struct attr_value *av;
2806   struct insn_ent *ie;
2807   rtx newexp;
2808   int i;
2809   struct attr_value_list *ivbuf;
2810   struct attr_value_list *iv;
2811
2812   /* For each insn code, make a list of all the insn_ent's for it,
2813      for all values for all attributes.  */
2814
2815   if (num_insn_ents == 0)
2816     return;
2817
2818   /* Make 2 extra elements, for "code" values -2 and -1.  */
2819   insn_code_values = XCNEWVEC (struct attr_value_list *, insn_code_number + 2);
2820
2821   /* Offset the table address so we can index by -2 or -1.  */
2822   insn_code_values += 2;
2823
2824   iv = ivbuf = XNEWVEC (struct attr_value_list, num_insn_ents);
2825
2826   for (i = 0; i < MAX_ATTRS_INDEX; i++)
2827     for (attr = attrs[i]; attr; attr = attr->next)
2828       for (av = attr->first_value; av; av = av->next)
2829         for (ie = av->first_insn; ie; ie = ie->next)
2830           {
2831             iv->attr = attr;
2832             iv->av = av;
2833             iv->ie = ie;
2834             iv->next = insn_code_values[ie->def->insn_code];
2835             insn_code_values[ie->def->insn_code] = iv;
2836             iv++;
2837           }
2838
2839   /* Sanity check on num_insn_ents.  */
2840   gcc_assert (iv == ivbuf + num_insn_ents);
2841
2842   /* Process one insn code at a time.  */
2843   for (i = -2; i < insn_code_number; i++)
2844     {
2845       /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
2846          We use it to mean "already simplified for this insn".  */
2847       for (iv = insn_code_values[i]; iv; iv = iv->next)
2848         clear_struct_flag (iv->av->value);
2849
2850       for (iv = insn_code_values[i]; iv; iv = iv->next)
2851         {
2852           struct obstack *old = rtl_obstack;
2853
2854           attr = iv->attr;
2855           av = iv->av;
2856           ie = iv->ie;
2857           if (GET_CODE (av->value) != COND)
2858             continue;
2859
2860           rtl_obstack = temp_obstack;
2861           newexp = av->value;
2862           while (GET_CODE (newexp) == COND)
2863             {
2864               rtx newexp2 = simplify_cond (newexp, ie->def->insn_code,
2865                                            ie->def->insn_index);
2866               if (newexp2 == newexp)
2867                 break;
2868               newexp = newexp2;
2869             }
2870
2871           rtl_obstack = old;
2872           if (newexp != av->value)
2873             {
2874               newexp = attr_copy_rtx (newexp);
2875               remove_insn_ent (av, ie);
2876               av = get_attr_value (newexp, attr, ie->def->insn_code);
2877               iv->av = av;
2878               insert_insn_ent (av, ie);
2879             }
2880         }
2881     }
2882
2883   free (ivbuf);
2884   free (insn_code_values - 2);
2885   insn_code_values = NULL;
2886 }
2887
2888 /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions.  */
2889
2890 static void
2891 clear_struct_flag (rtx x)
2892 {
2893   int i;
2894   int j;
2895   enum rtx_code code;
2896   const char *fmt;
2897
2898   ATTR_CURR_SIMPLIFIED_P (x) = 0;
2899   if (ATTR_IND_SIMPLIFIED_P (x))
2900     return;
2901
2902   code = GET_CODE (x);
2903
2904   switch (code)
2905     {
2906     case REG:
2907     case CONST_INT:
2908     case CONST_DOUBLE:
2909     case CONST_VECTOR:
2910     case SYMBOL_REF:
2911     case CODE_LABEL:
2912     case PC:
2913     case CC0:
2914     case EQ_ATTR:
2915     case ATTR_FLAG:
2916       return;
2917
2918     default:
2919       break;
2920     }
2921
2922   /* Compare the elements.  If any pair of corresponding elements
2923      fail to match, return 0 for the whole things.  */
2924
2925   fmt = GET_RTX_FORMAT (code);
2926   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2927     {
2928       switch (fmt[i])
2929         {
2930         case 'V':
2931         case 'E':
2932           for (j = 0; j < XVECLEN (x, i); j++)
2933             clear_struct_flag (XVECEXP (x, i, j));
2934           break;
2935
2936         case 'e':
2937           clear_struct_flag (XEXP (x, i));
2938           break;
2939         }
2940     }
2941 }
2942
2943 /* Add attribute value NAME to the beginning of ATTR's list.  */
2944
2945 static void
2946 add_attr_value (struct attr_desc *attr, const char *name)
2947 {
2948   struct attr_value *av;
2949
2950   av = oballoc (struct attr_value);
2951   av->value = attr_rtx (CONST_STRING, name);
2952   av->next = attr->first_value;
2953   attr->first_value = av;
2954   av->first_insn = NULL;
2955   av->num_insns = 0;
2956   av->has_asm_insn = 0;
2957 }
2958
2959 /* Create table entries for DEFINE_ATTR or DEFINE_ENUM_ATTR.  */
2960
2961 static void
2962 gen_attr (rtx exp, int lineno)
2963 {
2964   struct enum_type *et;
2965   struct enum_value *ev;
2966   struct attr_desc *attr;
2967   const char *name_ptr;
2968   char *p;
2969
2970   /* Make a new attribute structure.  Check for duplicate by looking at
2971      attr->default_val, since it is initialized by this routine.  */
2972   attr = find_attr (&XSTR (exp, 0), 1);
2973   if (attr->default_val)
2974     {
2975       error_with_line (lineno, "duplicate definition for attribute %s",
2976                        attr->name);
2977       message_with_line (attr->lineno, "previous definition");
2978       return;
2979     }
2980   attr->lineno = lineno;
2981
2982   if (GET_CODE (exp) == DEFINE_ENUM_ATTR)
2983     {
2984       attr->enum_name = XSTR (exp, 1);
2985       et = lookup_enum_type (XSTR (exp, 1));
2986       if (!et || !et->md_p)
2987         error_with_line (lineno, "No define_enum called `%s' defined",
2988                          attr->name);
2989       for (ev = et->values; ev; ev = ev->next)
2990         add_attr_value (attr, ev->name);
2991     }
2992   else if (*XSTR (exp, 1) == '\0')
2993     attr->is_numeric = 1;
2994   else
2995     {
2996       name_ptr = XSTR (exp, 1);
2997       while ((p = next_comma_elt (&name_ptr)) != NULL)
2998         add_attr_value (attr, p);
2999     }
3000
3001   if (GET_CODE (XEXP (exp, 2)) == CONST)
3002     {
3003       attr->is_const = 1;
3004       if (attr->is_numeric)
3005         error_with_line (lineno,
3006                          "constant attributes may not take numeric values");
3007
3008       /* Get rid of the CONST node.  It is allowed only at top-level.  */
3009       XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
3010     }
3011
3012   if (! strcmp_check (attr->name, length_str) && ! attr->is_numeric)
3013     error_with_line (lineno, "`length' attribute must take numeric values");
3014
3015   /* Set up the default value.  */
3016   XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
3017   attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
3018 }
3019
3020 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
3021    alternatives in the constraints.  Assume all MATCH_OPERANDs have the same
3022    number of alternatives as this should be checked elsewhere.  */
3023
3024 static int
3025 count_alternatives (rtx exp)
3026 {
3027   int i, j, n;
3028   const char *fmt;
3029
3030   if (GET_CODE (exp) == MATCH_OPERAND)
3031     return n_comma_elts (XSTR (exp, 2));
3032
3033   for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3034        i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3035     switch (*fmt++)
3036       {
3037       case 'e':
3038       case 'u':
3039         n = count_alternatives (XEXP (exp, i));
3040         if (n)
3041           return n;
3042         break;
3043
3044       case 'E':
3045       case 'V':
3046         if (XVEC (exp, i) != NULL)
3047           for (j = 0; j < XVECLEN (exp, i); j++)
3048             {
3049               n = count_alternatives (XVECEXP (exp, i, j));
3050               if (n)
3051                 return n;
3052             }
3053       }
3054
3055   return 0;
3056 }
3057
3058 /* Returns nonzero if the given expression contains an EQ_ATTR with the
3059    `alternative' attribute.  */
3060
3061 static int
3062 compares_alternatives_p (rtx exp)
3063 {
3064   int i, j;
3065   const char *fmt;
3066
3067   if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
3068     return 1;
3069
3070   for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3071        i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3072     switch (*fmt++)
3073       {
3074       case 'e':
3075       case 'u':
3076         if (compares_alternatives_p (XEXP (exp, i)))
3077           return 1;
3078         break;
3079
3080       case 'E':
3081         for (j = 0; j < XVECLEN (exp, i); j++)
3082           if (compares_alternatives_p (XVECEXP (exp, i, j)))
3083             return 1;
3084         break;
3085       }
3086
3087   return 0;
3088 }
3089
3090 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES.  */
3091
3092 static void
3093 gen_insn (rtx exp, int lineno)
3094 {
3095   struct insn_def *id;
3096
3097   id = oballoc (struct insn_def);
3098   id->next = defs;
3099   defs = id;
3100   id->def = exp;
3101   id->lineno = lineno;
3102
3103   switch (GET_CODE (exp))
3104     {
3105     case DEFINE_INSN:
3106       id->insn_code = insn_code_number;
3107       id->insn_index = insn_index_number;
3108       id->num_alternatives = count_alternatives (exp);
3109       if (id->num_alternatives == 0)
3110         id->num_alternatives = 1;
3111       id->vec_idx = 4;
3112       break;
3113
3114     case DEFINE_PEEPHOLE:
3115       id->insn_code = insn_code_number;
3116       id->insn_index = insn_index_number;
3117       id->num_alternatives = count_alternatives (exp);
3118       if (id->num_alternatives == 0)
3119         id->num_alternatives = 1;
3120       id->vec_idx = 3;
3121       break;
3122
3123     case DEFINE_ASM_ATTRIBUTES:
3124       id->insn_code = -1;
3125       id->insn_index = -1;
3126       id->num_alternatives = 1;
3127       id->vec_idx = 0;
3128       got_define_asm_attributes = 1;
3129       break;
3130
3131     default:
3132       gcc_unreachable ();
3133     }
3134 }
3135
3136 /* Process a DEFINE_DELAY.  Validate the vector length, check if annul
3137    true or annul false is specified, and make a `struct delay_desc'.  */
3138
3139 static void
3140 gen_delay (rtx def, int lineno)
3141 {
3142   struct delay_desc *delay;
3143   int i;
3144
3145   if (XVECLEN (def, 1) % 3 != 0)
3146     {
3147       error_with_line (lineno,
3148                        "number of elements in DEFINE_DELAY must"
3149                        " be multiple of three");
3150       return;
3151     }
3152
3153   for (i = 0; i < XVECLEN (def, 1); i += 3)
3154     {
3155       if (XVECEXP (def, 1, i + 1))
3156         have_annul_true = 1;
3157       if (XVECEXP (def, 1, i + 2))
3158         have_annul_false = 1;
3159     }
3160
3161   delay = oballoc (struct delay_desc);
3162   delay->def = def;
3163   delay->num = ++num_delays;
3164   delay->next = delays;
3165   delay->lineno = lineno;
3166   delays = delay;
3167 }
3168
3169 /* Names of attributes that could be possibly cached.  */
3170 static const char *cached_attrs[32];
3171 /* Number of such attributes.  */
3172 static int cached_attr_count;
3173 /* Bitmasks of possibly cached attributes.  */
3174 static unsigned int attrs_seen_once, attrs_seen_more_than_once;
3175 static unsigned int attrs_to_cache;
3176 static unsigned int attrs_cached_inside, attrs_cached_after;
3177
3178 /* Finds non-const attributes that could be possibly cached.
3179    When create is TRUE, fills in cached_attrs array.
3180    Computes ATTRS_SEEN_ONCE and ATTRS_SEEN_MORE_THAN_ONCE
3181    bitmasks.  */
3182
3183 static void
3184 find_attrs_to_cache (rtx exp, bool create)
3185 {
3186   int i;
3187   const char *name;
3188   struct attr_desc *attr;
3189
3190   if (exp == NULL)
3191     return;
3192
3193   switch (GET_CODE (exp))
3194     {
3195     case NOT:
3196       if (GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3197         find_attrs_to_cache (XEXP (exp, 0), create);
3198       return;
3199
3200     case EQ_ATTR:
3201       name = XSTR (exp, 0);
3202       if (name == alternative_name)
3203         return;
3204       for (i = 0; i < cached_attr_count; i++)
3205         if (name == cached_attrs[i])
3206           {
3207             if ((attrs_seen_once & (1U << i)) != 0)
3208               attrs_seen_more_than_once |= (1U << i);
3209             else
3210               attrs_seen_once |= (1U << i);
3211             return;
3212           }
3213       if (!create)
3214         return;
3215       attr = find_attr (&name, 0);
3216       gcc_assert (attr);
3217       if (attr->is_const)
3218         return;
3219       if (cached_attr_count == 32)
3220         return;
3221       cached_attrs[cached_attr_count] = XSTR (exp, 0);
3222       attrs_seen_once |= (1U << cached_attr_count);
3223       cached_attr_count++;
3224       return;
3225
3226     case AND:
3227     case IOR:
3228       find_attrs_to_cache (XEXP (exp, 0), create);
3229       find_attrs_to_cache (XEXP (exp, 1), create);
3230       return;
3231
3232     case COND:
3233       for (i = 0; i < XVECLEN (exp, 0); i += 2)
3234         find_attrs_to_cache (XVECEXP (exp, 0, i), create);
3235       return;
3236
3237     default:
3238       return;
3239     }
3240 }
3241
3242 /* Given a piece of RTX, print a C expression to test its truth value.
3243    We use AND and IOR both for logical and bit-wise operations, so
3244    interpret them as logical unless they are inside a comparison expression.  */
3245
3246 /* Interpret AND/IOR as bit-wise operations instead of logical.  */
3247 #define FLG_BITWISE             1
3248 /* Set if cached attribute will be known initialized in else block after
3249    this condition.  This is true for LHS of toplevel && and || and
3250    even for RHS of ||, but not for RHS of &&.  */
3251 #define FLG_AFTER               2
3252 /* Set if cached attribute will be known initialized in then block after
3253    this condition.  This is true for LHS of toplevel && and || and
3254    even for RHS of &&, but not for RHS of ||.  */
3255 #define FLG_INSIDE              4
3256 /* Cleared when an operand of &&.  */
3257 #define FLG_OUTSIDE_AND         8
3258
3259 static unsigned int
3260 write_test_expr (rtx exp, unsigned int attrs_cached, int flags)
3261 {
3262   int comparison_operator = 0;
3263   RTX_CODE code;
3264   struct attr_desc *attr;
3265
3266   /* In order not to worry about operator precedence, surround our part of
3267      the expression with parentheses.  */
3268
3269   printf ("(");
3270   code = GET_CODE (exp);
3271   switch (code)
3272     {
3273     /* Binary operators.  */
3274     case GEU: case GTU:
3275     case LEU: case LTU:
3276       printf ("(unsigned) ");
3277       /* Fall through.  */
3278
3279     case EQ: case NE:
3280     case GE: case GT:
3281     case LE: case LT:
3282       comparison_operator = FLG_BITWISE;
3283
3284     case PLUS:   case MINUS:  case MULT:     case DIV:      case MOD:
3285     case AND:    case IOR:    case XOR:
3286     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3287       if ((code != AND && code != IOR) || (flags & FLG_BITWISE))
3288         {
3289           flags &= ~(FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND);
3290           write_test_expr (XEXP (exp, 0), attrs_cached,
3291                            flags | comparison_operator);
3292         }
3293       else
3294         {
3295           if (code == AND)
3296             flags &= ~FLG_OUTSIDE_AND;
3297           if (GET_CODE (XEXP (exp, 0)) == code
3298               || GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3299               || (GET_CODE (XEXP (exp, 0)) == NOT
3300                   && GET_CODE (XEXP (XEXP (exp, 0), 0)) == EQ_ATTR))
3301             attrs_cached
3302               = write_test_expr (XEXP (exp, 0), attrs_cached, flags);
3303           else
3304             write_test_expr (XEXP (exp, 0), attrs_cached, flags);
3305         }
3306       switch (code)
3307         {
3308         case EQ:
3309           printf (" == ");
3310           break;
3311         case NE:
3312           printf (" != ");
3313           break;
3314         case GE:
3315           printf (" >= ");
3316           break;
3317         case GT:
3318           printf (" > ");
3319           break;
3320         case GEU:
3321           printf (" >= (unsigned) ");
3322           break;
3323         case GTU:
3324           printf (" > (unsigned) ");
3325           break;
3326         case LE:
3327           printf (" <= ");
3328           break;
3329         case LT:
3330           printf (" < ");
3331           break;
3332         case LEU:
3333           printf (" <= (unsigned) ");
3334           break;
3335         case LTU:
3336           printf (" < (unsigned) ");
3337           break;
3338         case PLUS:
3339           printf (" + ");
3340           break;
3341         case MINUS:
3342           printf (" - ");
3343           break;
3344         case MULT:
3345           printf (" * ");
3346           break;
3347         case DIV:
3348           printf (" / ");
3349           break;
3350         case MOD:
3351           printf (" %% ");
3352           break;
3353         case AND:
3354           if (flags & FLG_BITWISE)
3355             printf (" & ");
3356           else
3357             printf (" && ");
3358           break;
3359         case IOR:
3360           if (flags & FLG_BITWISE)
3361             printf (" | ");
3362           else
3363             printf (" || ");
3364           break;
3365         case XOR:
3366           printf (" ^ ");
3367           break;
3368         case ASHIFT:
3369           printf (" << ");
3370           break;
3371         case LSHIFTRT:
3372         case ASHIFTRT:
3373           printf (" >> ");
3374           break;
3375         default:
3376           gcc_unreachable ();
3377         }
3378
3379       if (code == AND)
3380         {
3381           /* For if (something && (cached_x = get_attr_x (insn)) == X)
3382              cached_x is only known to be initialized in then block.  */
3383           flags &= ~FLG_AFTER;
3384         }
3385       else if (code == IOR)
3386         {
3387           if (flags & FLG_OUTSIDE_AND)
3388             /* For if (something || (cached_x = get_attr_x (insn)) == X)
3389                cached_x is only known to be initialized in else block
3390                and else if conditions.  */
3391             flags &= ~FLG_INSIDE;
3392           else
3393             /* For if ((something || (cached_x = get_attr_x (insn)) == X)
3394                        && something_else)
3395                cached_x is not know to be initialized anywhere.  */
3396             flags &= ~(FLG_AFTER | FLG_INSIDE);
3397         }
3398       if ((code == AND || code == IOR)
3399           && (GET_CODE (XEXP (exp, 1)) == code
3400               || GET_CODE (XEXP (exp, 1)) == EQ_ATTR
3401               || (GET_CODE (XEXP (exp, 1)) == NOT
3402                   && GET_CODE (XEXP (XEXP (exp, 1), 0)) == EQ_ATTR)))
3403         attrs_cached
3404           = write_test_expr (XEXP (exp, 1), attrs_cached, flags);
3405       else
3406         write_test_expr (XEXP (exp, 1), attrs_cached,
3407                          flags | comparison_operator);
3408       break;
3409
3410     case NOT:
3411       /* Special-case (not (eq_attrq "alternative" "x")) */
3412       if (! (flags & FLG_BITWISE) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3413         {
3414           if (XSTR (XEXP (exp, 0), 0) == alternative_name)
3415             {
3416               printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
3417               break;
3418             }
3419
3420           printf ("! ");
3421           attrs_cached = write_test_expr (XEXP (exp, 0), attrs_cached, flags);
3422           break;
3423         }
3424
3425       /* Otherwise, fall through to normal unary operator.  */
3426
3427     /* Unary operators.  */
3428     case ABS:  case NEG:
3429       switch (code)
3430         {
3431         case NOT:
3432           if (flags & FLG_BITWISE)
3433             printf ("~ ");
3434           else
3435             printf ("! ");
3436           break;
3437         case ABS:
3438           printf ("abs ");
3439           break;
3440         case NEG:
3441           printf ("-");
3442           break;
3443         default:
3444           gcc_unreachable ();
3445         }
3446
3447       flags &= ~(FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND);
3448       write_test_expr (XEXP (exp, 0), attrs_cached, flags);
3449       break;
3450
3451     case EQ_ATTR_ALT:
3452         {
3453           int set = XINT (exp, 0), bit = 0;
3454
3455           if (flags & FLG_BITWISE)
3456             fatal ("EQ_ATTR_ALT not valid inside comparison");
3457
3458           if (!set)
3459             fatal ("Empty EQ_ATTR_ALT should be optimized out");
3460
3461           if (!(set & (set - 1)))
3462             {
3463               if (!(set & 0xffff))
3464                 {
3465                   bit += 16;
3466                   set >>= 16;
3467                 }
3468               if (!(set & 0xff))
3469                 {
3470                   bit += 8;
3471                   set >>= 8;
3472                 }
3473               if (!(set & 0xf))
3474                 {
3475                   bit += 4;
3476                   set >>= 4;
3477                 }
3478               if (!(set & 0x3))
3479                 {
3480                   bit += 2;
3481                   set >>= 2;
3482                 }
3483               if (!(set & 1))
3484                 bit++;
3485
3486               printf ("which_alternative %s= %d",
3487                       XINT (exp, 1) ? "!" : "=", bit);
3488             }
3489           else
3490             {
3491               printf ("%s((1 << which_alternative) & %#x)",
3492                       XINT (exp, 1) ? "!" : "", set);
3493             }
3494         }
3495       break;
3496
3497     /* Comparison test of an attribute with a value.  Most of these will
3498        have been removed by optimization.   Handle "alternative"
3499        specially and give error if EQ_ATTR present inside a comparison.  */
3500     case EQ_ATTR:
3501       if (flags & FLG_BITWISE)
3502         fatal ("EQ_ATTR not valid inside comparison");
3503
3504       if (XSTR (exp, 0) == alternative_name)
3505         {
3506           printf ("which_alternative == %s", XSTR (exp, 1));
3507           break;
3508         }
3509
3510       attr = find_attr (&XSTR (exp, 0), 0);
3511       gcc_assert (attr);
3512
3513       /* Now is the time to expand the value of a constant attribute.  */
3514       if (attr->is_const)
3515         {
3516           write_test_expr (evaluate_eq_attr (exp, attr,
3517                                              attr->default_val->value, -2, -2),
3518                            attrs_cached, 0);
3519         }
3520       else
3521         {
3522           int i;
3523           for (i = 0; i < cached_attr_count; i++)
3524             if (attr->name == cached_attrs[i])
3525               break;
3526           if (i < cached_attr_count && (attrs_cached & (1U << i)) != 0)
3527             printf ("cached_%s", attr->name);
3528           else if (i < cached_attr_count && (attrs_to_cache & (1U << i)) != 0)
3529             {
3530               printf ("(cached_%s = get_attr_%s (insn))",
3531                       attr->name, attr->name);
3532               if (flags & FLG_AFTER)
3533                 attrs_cached_after |= (1U << i);
3534               if (flags & FLG_INSIDE)
3535                 attrs_cached_inside |= (1U << i);
3536               attrs_cached |= (1U << i);
3537             }
3538           else
3539             printf ("get_attr_%s (insn)", attr->name);
3540           printf (" == ");
3541           write_attr_valueq (attr, XSTR (exp, 1));
3542         }
3543       break;
3544
3545     /* Comparison test of flags for define_delays.  */
3546     case ATTR_FLAG:
3547       if (flags & FLG_BITWISE)
3548         fatal ("ATTR_FLAG not valid inside comparison");
3549       printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
3550       break;
3551
3552     /* See if an operand matches a predicate.  */
3553     case MATCH_OPERAND:
3554       /* If only a mode is given, just ensure the mode matches the operand.
3555          If neither a mode nor predicate is given, error.  */
3556       if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
3557         {
3558           if (GET_MODE (exp) == VOIDmode)
3559             fatal ("null MATCH_OPERAND specified as test");
3560           else
3561             printf ("GET_MODE (operands[%d]) == %smode",
3562                     XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3563         }
3564       else
3565         printf ("%s (operands[%d], %smode)",
3566                 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3567       break;
3568
3569     /* Constant integer.  */
3570     case CONST_INT:
3571       printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
3572       break;
3573
3574     /* A random C expression.  */
3575     case SYMBOL_REF:
3576       print_c_condition (XSTR (exp, 0));
3577       break;
3578
3579     /* The address of the branch target.  */
3580     case MATCH_DUP:
3581       printf ("INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
3582               XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
3583       break;
3584
3585     case PC:
3586       /* The address of the current insn.  We implement this actually as the
3587          address of the current insn for backward branches, but the last
3588          address of the next insn for forward branches, and both with
3589          adjustments that account for the worst-case possible stretching of
3590          intervening alignments between this insn and its destination.  */
3591       printf ("insn_current_reference_address (insn)");
3592       break;
3593
3594     case CONST_STRING:
3595       printf ("%s", XSTR (exp, 0));
3596       break;
3597
3598     case IF_THEN_ELSE:
3599       write_test_expr (XEXP (exp, 0), attrs_cached, 0);
3600       printf (" ? ");
3601       write_test_expr (XEXP (exp, 1), attrs_cached, FLG_BITWISE);
3602       printf (" : ");
3603       write_test_expr (XEXP (exp, 2), attrs_cached, FLG_BITWISE);
3604       break;
3605
3606     default:
3607       fatal ("bad RTX code `%s' in attribute calculation\n",
3608              GET_RTX_NAME (code));
3609     }
3610
3611   printf (")");
3612   return attrs_cached;
3613 }
3614
3615 /* Given an attribute value, return the maximum CONST_STRING argument
3616    encountered.  Set *UNKNOWNP and return INT_MAX if the value is unknown.  */
3617
3618 static int
3619 max_attr_value (rtx exp, int *unknownp)
3620 {
3621   int current_max;
3622   int i, n;
3623
3624   switch (GET_CODE (exp))
3625     {
3626     case CONST_STRING:
3627       current_max = atoi (XSTR (exp, 0));
3628       break;
3629
3630     case COND:
3631       current_max = max_attr_value (XEXP (exp, 1), unknownp);
3632       for (i = 0; i < XVECLEN (exp, 0); i += 2)
3633         {
3634           n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3635           if (n > current_max)
3636             current_max = n;
3637         }
3638       break;
3639
3640     case IF_THEN_ELSE:
3641       current_max = max_attr_value (XEXP (exp, 1), unknownp);
3642       n = max_attr_value (XEXP (exp, 2), unknownp);
3643       if (n > current_max)
3644         current_max = n;
3645       break;
3646
3647     default:
3648       *unknownp = 1;
3649       current_max = INT_MAX;
3650       break;
3651     }
3652
3653   return current_max;
3654 }
3655
3656 /* Given an attribute value, return the minimum CONST_STRING argument
3657    encountered.  Set *UNKNOWNP and return 0 if the value is unknown.  */
3658
3659 static int
3660 min_attr_value (rtx exp, int *unknownp)
3661 {
3662   int current_min;
3663   int i, n;
3664
3665   switch (GET_CODE (exp))
3666     {
3667     case CONST_STRING:
3668       current_min = atoi (XSTR (exp, 0));
3669       break;
3670
3671     case COND:
3672       current_min = min_attr_value (XEXP (exp, 1), unknownp);
3673       for (i = 0; i < XVECLEN (exp, 0); i += 2)
3674         {
3675           n = min_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3676           if (n < current_min)
3677             current_min = n;
3678         }
3679       break;
3680
3681     case IF_THEN_ELSE:
3682       current_min = min_attr_value (XEXP (exp, 1), unknownp);
3683       n = min_attr_value (XEXP (exp, 2), unknownp);
3684       if (n < current_min)
3685         current_min = n;
3686       break;
3687
3688     default:
3689       *unknownp = 1;
3690       current_min = INT_MAX;
3691       break;
3692     }
3693
3694   return current_min;
3695 }
3696
3697 /* Given an attribute value, return the result of ORing together all
3698    CONST_STRING arguments encountered.  Set *UNKNOWNP and return -1
3699    if the numeric value is not known.  */
3700
3701 static int
3702 or_attr_value (rtx exp, int *unknownp)
3703 {
3704   int current_or;
3705   int i;
3706
3707   switch (GET_CODE (exp))
3708     {
3709     case CONST_STRING:
3710       current_or = atoi (XSTR (exp, 0));
3711       break;
3712
3713     case COND:
3714       current_or = or_attr_value (XEXP (exp, 1), unknownp);
3715       for (i = 0; i < XVECLEN (exp, 0); i += 2)
3716         current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3717       break;
3718
3719     case IF_THEN_ELSE:
3720       current_or = or_attr_value (XEXP (exp, 1), unknownp);
3721       current_or |= or_attr_value (XEXP (exp, 2), unknownp);
3722       break;
3723
3724     default:
3725       *unknownp = 1;
3726       current_or = -1;
3727       break;
3728     }
3729
3730   return current_or;
3731 }
3732
3733 /* Scan an attribute value, possibly a conditional, and record what actions
3734    will be required to do any conditional tests in it.
3735
3736    Specifically, set
3737         `must_extract'    if we need to extract the insn operands
3738         `must_constrain'  if we must compute `which_alternative'
3739         `address_used'    if an address expression was used
3740         `length_used'     if an (eq_attr "length" ...) was used
3741  */
3742
3743 static void
3744 walk_attr_value (rtx exp)
3745 {
3746   int i, j;
3747   const char *fmt;
3748   RTX_CODE code;
3749
3750   if (exp == NULL)
3751     return;
3752
3753   code = GET_CODE (exp);
3754   switch (code)
3755     {
3756     case SYMBOL_REF:
3757       if (! ATTR_IND_SIMPLIFIED_P (exp))
3758         /* Since this is an arbitrary expression, it can look at anything.
3759            However, constant expressions do not depend on any particular
3760            insn.  */
3761         must_extract = must_constrain = 1;
3762       return;
3763
3764     case MATCH_OPERAND:
3765       must_extract = 1;
3766       return;
3767
3768     case EQ_ATTR_ALT:
3769       must_extract = must_constrain = 1;
3770       break;
3771
3772     case EQ_ATTR:
3773       if (XSTR (exp, 0) == alternative_name)
3774         must_extract = must_constrain = 1;
3775       else if (strcmp_check (XSTR (exp, 0), length_str) == 0)
3776         length_used = 1;
3777       return;
3778
3779     case MATCH_DUP:
3780       must_extract = 1;
3781       address_used = 1;
3782       return;
3783
3784     case PC:
3785       address_used = 1;
3786       return;
3787
3788     case ATTR_FLAG:
3789       return;
3790
3791     default:
3792       break;
3793     }
3794
3795   for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
3796     switch (*fmt++)
3797       {
3798       case 'e':
3799       case 'u':
3800         walk_attr_value (XEXP (exp, i));
3801         break;
3802
3803       case 'E':
3804         if (XVEC (exp, i) != NULL)
3805           for (j = 0; j < XVECLEN (exp, i); j++)
3806             walk_attr_value (XVECEXP (exp, i, j));
3807         break;
3808       }
3809 }
3810
3811 /* Write out a function to obtain the attribute for a given INSN.  */
3812
3813 static void
3814 write_attr_get (struct attr_desc *attr)
3815 {
3816   struct attr_value *av, *common_av;
3817   int i, j;
3818
3819   /* Find the most used attribute value.  Handle that as the `default' of the
3820      switch we will generate.  */
3821   common_av = find_most_used (attr);
3822
3823   /* Write out start of function, then all values with explicit `case' lines,
3824      then a `default', then the value with the most uses.  */
3825   if (attr->enum_name)
3826     printf ("enum %s\n", attr->enum_name);
3827   else if (!attr->is_numeric)
3828     printf ("enum attr_%s\n", attr->name);
3829   else
3830     printf ("int\n");
3831
3832   /* If the attribute name starts with a star, the remainder is the name of
3833      the subroutine to use, instead of `get_attr_...'.  */
3834   if (attr->name[0] == '*')
3835     printf ("%s (rtx insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
3836   else if (attr->is_const == 0)
3837     printf ("get_attr_%s (rtx insn ATTRIBUTE_UNUSED)\n", attr->name);
3838   else
3839     {
3840       printf ("get_attr_%s (void)\n", attr->name);
3841       printf ("{\n");
3842
3843       for (av = attr->first_value; av; av = av->next)
3844         if (av->num_insns == 1)
3845           write_attr_set (attr, 2, av->value, "return", ";",
3846                           true_rtx, av->first_insn->def->insn_code,
3847                           av->first_insn->def->insn_index, 0);
3848         else if (av->num_insns != 0)
3849           write_attr_set (attr, 2, av->value, "return", ";",
3850                           true_rtx, -2, 0, 0);
3851
3852       printf ("}\n\n");
3853       return;
3854     }
3855
3856   printf ("{\n");
3857
3858   /* Find attributes that are worth caching in the conditions.  */
3859   cached_attr_count = 0;
3860   attrs_seen_more_than_once = 0;
3861   for (av = attr->first_value; av; av = av->next)
3862     {
3863       attrs_seen_once = 0;
3864       find_attrs_to_cache (av->value, true);
3865     }
3866   /* Remove those that aren't worth caching from the array.  */
3867   for (i = 0, j = 0; i < cached_attr_count; i++)
3868     if ((attrs_seen_more_than_once & (1U << i)) != 0)
3869       {
3870         const char *name = cached_attrs[i];
3871         struct attr_desc *cached_attr;
3872         if (i != j)
3873           cached_attrs[j] = name;
3874         cached_attr = find_attr (&name, 0);
3875         gcc_assert (cached_attr && cached_attr->is_const == 0);
3876         if (cached_attr->enum_name)
3877           printf ("  enum %s", cached_attr->enum_name);
3878         else if (!cached_attr->is_numeric)
3879           printf ("  enum attr_%s", cached_attr->name);
3880         else
3881           printf ("  int");
3882         printf (" cached_%s ATTRIBUTE_UNUSED;\n", name);
3883         j++;
3884       }
3885   cached_attr_count = j;
3886   if (cached_attr_count)
3887     printf ("\n");
3888
3889   printf ("  switch (recog_memoized (insn))\n");
3890   printf ("    {\n");
3891
3892   for (av = attr->first_value; av; av = av->next)
3893     if (av != common_av)
3894       write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
3895
3896   write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
3897   printf ("    }\n}\n\n");
3898   cached_attr_count = 0;
3899 }
3900
3901 /* Given an AND tree of known true terms (because we are inside an `if' with
3902    that as the condition or are in an `else' clause) and an expression,
3903    replace any known true terms with TRUE.  Use `simplify_and_tree' to do
3904    the bulk of the work.  */
3905
3906 static rtx
3907 eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
3908 {
3909   rtx term;
3910
3911   known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
3912
3913   if (GET_CODE (known_true) == AND)
3914     {
3915       exp = eliminate_known_true (XEXP (known_true, 0), exp,
3916                                   insn_code, insn_index);
3917       exp = eliminate_known_true (XEXP (known_true, 1), exp,
3918                                   insn_code, insn_index);
3919     }
3920   else
3921     {
3922       term = known_true;
3923       exp = simplify_and_tree (exp, &term, insn_code, insn_index);
3924     }
3925
3926   return exp;
3927 }
3928
3929 /* Write out a series of tests and assignment statements to perform tests and
3930    sets of an attribute value.  We are passed an indentation amount and prefix
3931    and suffix strings to write around each attribute value (e.g., "return"
3932    and ";").  */
3933
3934 static void
3935 write_attr_set (struct attr_desc *attr, int indent, rtx value,
3936                 const char *prefix, const char *suffix, rtx known_true,
3937                 int insn_code, int insn_index, unsigned int attrs_cached)
3938 {
3939   if (GET_CODE (value) == COND)
3940     {
3941       /* Assume the default value will be the default of the COND unless we
3942          find an always true expression.  */
3943       rtx default_val = XEXP (value, 1);
3944       rtx our_known_true = known_true;
3945       rtx newexp;
3946       int first_if = 1;
3947       int i;
3948
3949       if (cached_attr_count)
3950         {
3951           attrs_seen_once = 0;
3952           attrs_seen_more_than_once = 0;
3953           for (i = 0; i < XVECLEN (value, 0); i += 2)
3954             find_attrs_to_cache (XVECEXP (value, 0, i), false);
3955           attrs_to_cache |= attrs_seen_more_than_once;
3956         }
3957
3958       for (i = 0; i < XVECLEN (value, 0); i += 2)
3959         {
3960           rtx testexp;
3961           rtx inner_true;
3962
3963           testexp = eliminate_known_true (our_known_true,
3964                                           XVECEXP (value, 0, i),
3965                                           insn_code, insn_index);
3966           newexp = attr_rtx (NOT, testexp);
3967           newexp = insert_right_side (AND, our_known_true, newexp,
3968                                       insn_code, insn_index);
3969
3970           /* If the test expression is always true or if the next `known_true'
3971              expression is always false, this is the last case, so break
3972              out and let this value be the `else' case.  */
3973           if (testexp == true_rtx || newexp == false_rtx)
3974             {
3975               default_val = XVECEXP (value, 0, i + 1);
3976               break;
3977             }
3978
3979           /* Compute the expression to pass to our recursive call as being
3980              known true.  */
3981           inner_true = insert_right_side (AND, our_known_true,
3982                                           testexp, insn_code, insn_index);
3983
3984           /* If this is always false, skip it.  */
3985           if (inner_true == false_rtx)
3986             continue;
3987
3988           attrs_cached_inside = attrs_cached;
3989           attrs_cached_after = attrs_cached;
3990           write_indent (indent);
3991           printf ("%sif ", first_if ? "" : "else ");
3992           first_if = 0;
3993           write_test_expr (testexp, attrs_cached,
3994                            (FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND));
3995           attrs_cached = attrs_cached_after;
3996           printf ("\n");
3997           write_indent (indent + 2);
3998           printf ("{\n");
3999
4000           write_attr_set (attr, indent + 4,
4001                           XVECEXP (value, 0, i + 1), prefix, suffix,
4002                           inner_true, insn_code, insn_index,
4003                           attrs_cached_inside);
4004           write_indent (indent + 2);
4005           printf ("}\n");
4006           our_known_true = newexp;
4007         }
4008
4009       if (! first_if)
4010         {
4011           write_indent (indent);
4012           printf ("else\n");
4013           write_indent (indent + 2);
4014           printf ("{\n");
4015         }
4016
4017       write_attr_set (attr, first_if ? indent : indent + 4, default_val,
4018                       prefix, suffix, our_known_true, insn_code, insn_index,
4019                       attrs_cached);
4020
4021       if (! first_if)
4022         {
4023           write_indent (indent + 2);
4024           printf ("}\n");
4025         }
4026     }
4027   else
4028     {
4029       write_indent (indent);
4030       printf ("%s ", prefix);
4031       write_attr_value (attr, value);
4032       printf ("%s\n", suffix);
4033     }
4034 }
4035
4036 /* Write a series of case statements for every instruction in list IE.
4037    INDENT is the amount of indentation to write before each case.  */
4038
4039 static void
4040 write_insn_cases (struct insn_ent *ie, int indent)
4041 {
4042   for (; ie != 0; ie = ie->next)
4043     if (ie->def->insn_code != -1)
4044       {
4045         write_indent (indent);
4046         if (GET_CODE (ie->def->def) == DEFINE_PEEPHOLE)
4047           printf ("case %d:  /* define_peephole, line %d */\n",
4048                   ie->def->insn_code, ie->def->lineno);
4049         else
4050           printf ("case %d:  /* %s */\n",
4051                   ie->def->insn_code, XSTR (ie->def->def, 0));
4052       }
4053 }
4054
4055 /* Write out the computation for one attribute value.  */
4056
4057 static void
4058 write_attr_case (struct attr_desc *attr, struct attr_value *av,
4059                  int write_case_lines, const char *prefix, const char *suffix,
4060                  int indent, rtx known_true)
4061 {
4062   if (av->num_insns == 0)
4063     return;
4064
4065   if (av->has_asm_insn)
4066     {
4067       write_indent (indent);
4068       printf ("case -1:\n");
4069       write_indent (indent + 2);
4070       printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
4071       write_indent (indent + 2);
4072       printf ("    && asm_noperands (PATTERN (insn)) < 0)\n");
4073       write_indent (indent + 2);
4074       printf ("  fatal_insn_not_found (insn);\n");
4075     }
4076
4077   if (write_case_lines)
4078     write_insn_cases (av->first_insn, indent);
4079   else
4080     {
4081       write_indent (indent);
4082       printf ("default:\n");
4083     }
4084
4085   /* See what we have to do to output this value.  */
4086   must_extract = must_constrain = address_used = 0;
4087   walk_attr_value (av->value);
4088
4089   if (must_constrain)
4090     {
4091       write_indent (indent + 2);
4092       printf ("extract_constrain_insn_cached (insn);\n");
4093     }
4094   else if (must_extract)
4095     {
4096       write_indent (indent + 2);
4097       printf ("extract_insn_cached (insn);\n");
4098     }
4099
4100   attrs_to_cache = 0;
4101   if (av->num_insns == 1)
4102     write_attr_set (attr, indent + 2, av->value, prefix, suffix,
4103                     known_true, av->first_insn->def->insn_code,
4104                     av->first_insn->def->insn_index, 0);
4105   else
4106     write_attr_set (attr, indent + 2, av->value, prefix, suffix,
4107                     known_true, -2, 0, 0);
4108
4109   if (strncmp (prefix, "return", 6))
4110     {
4111       write_indent (indent + 2);
4112       printf ("break;\n");
4113     }
4114   printf ("\n");
4115 }
4116
4117 /* Utilities to write in various forms.  */
4118
4119 static void
4120 write_attr_valueq (struct attr_desc *attr, const char *s)
4121 {
4122   if (attr->is_numeric)
4123     {
4124       int num = atoi (s);
4125
4126       printf ("%d", num);
4127
4128       if (num > 9 || num < 0)
4129         printf (" /* %#x */", num);
4130     }
4131   else
4132     {
4133       write_upcase (attr->enum_name ? attr->enum_name : attr->name);
4134       printf ("_");
4135       write_upcase (s);
4136     }
4137 }
4138
4139 static void
4140 write_attr_value (struct attr_desc *attr, rtx value)
4141 {
4142   int op;
4143
4144   switch (GET_CODE (value))
4145     {
4146     case CONST_STRING:
4147       write_attr_valueq (attr, XSTR (value, 0));
4148       break;
4149
4150     case CONST_INT:
4151       printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
4152       break;
4153
4154     case SYMBOL_REF:
4155       print_c_condition (XSTR (value, 0));
4156       break;
4157
4158     case ATTR:
4159       {
4160         struct attr_desc *attr2 = find_attr (&XSTR (value, 0), 0);
4161         if (attr->enum_name)
4162           printf ("(enum %s)", attr->enum_name);
4163         else if (!attr->is_numeric)
4164           printf ("(enum attr_%s)", attr->name);
4165         else if (!attr2->is_numeric)
4166           printf ("(int)");
4167
4168         printf ("get_attr_%s (%s)", attr2->name,
4169                 (attr2->is_const ? "" : "insn"));
4170       }
4171       break;
4172
4173     case PLUS:
4174       op = '+';
4175       goto do_operator;
4176     case MINUS:
4177       op = '-';
4178       goto do_operator;
4179     case MULT:
4180       op = '*';
4181       goto do_operator;
4182     case DIV:
4183       op = '/';
4184       goto do_operator;
4185     case MOD:
4186       op = '%';
4187       goto do_operator;
4188
4189     do_operator:
4190       write_attr_value (attr, XEXP (value, 0));
4191       putchar (' ');
4192       putchar (op);
4193       putchar (' ');
4194       write_attr_value (attr, XEXP (value, 1));
4195       break;
4196
4197     default:
4198       gcc_unreachable ();
4199     }
4200 }
4201
4202 static void
4203 write_upcase (const char *str)
4204 {
4205   while (*str)
4206     {
4207       /* The argument of TOUPPER should not have side effects.  */
4208       putchar (TOUPPER(*str));
4209       str++;
4210     }
4211 }
4212
4213 static void
4214 write_indent (int indent)
4215 {
4216   for (; indent > 8; indent -= 8)
4217     printf ("\t");
4218
4219   for (; indent; indent--)
4220     printf (" ");
4221 }
4222
4223 /* Write a subroutine that is given an insn that requires a delay slot, a
4224    delay slot ordinal, and a candidate insn.  It returns nonzero if the
4225    candidate can be placed in the specified delay slot of the insn.
4226
4227    We can write as many as three subroutines.  `eligible_for_delay'
4228    handles normal delay slots, `eligible_for_annul_true' indicates that
4229    the specified insn can be annulled if the branch is true, and likewise
4230    for `eligible_for_annul_false'.
4231
4232    KIND is a string distinguishing these three cases ("delay", "annul_true",
4233    or "annul_false").  */
4234
4235 static void
4236 write_eligible_delay (const char *kind)
4237 {
4238   struct delay_desc *delay;
4239   int max_slots;
4240   char str[50];
4241   const char *pstr;
4242   struct attr_desc *attr;
4243   struct attr_value *av, *common_av;
4244   int i;
4245
4246   /* Compute the maximum number of delay slots required.  We use the delay
4247      ordinal times this number plus one, plus the slot number as an index into
4248      the appropriate predicate to test.  */
4249
4250   for (delay = delays, max_slots = 0; delay; delay = delay->next)
4251     if (XVECLEN (delay->def, 1) / 3 > max_slots)
4252       max_slots = XVECLEN (delay->def, 1) / 3;
4253
4254   /* Write function prelude.  */
4255
4256   printf ("int\n");
4257   printf ("eligible_for_%s (rtx delay_insn ATTRIBUTE_UNUSED, int slot, rtx candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
4258           kind);
4259   printf ("{\n");
4260   printf ("  rtx insn;\n");
4261   printf ("\n");
4262   printf ("  gcc_assert (slot < %d);\n", max_slots);
4263   printf ("\n");
4264   /* Allow dbr_schedule to pass labels, etc.  This can happen if try_split
4265      converts a compound instruction into a loop.  */
4266   printf ("  if (!INSN_P (candidate_insn))\n");
4267   printf ("    return 0;\n");
4268   printf ("\n");
4269
4270   /* If more than one delay type, find out which type the delay insn is.  */
4271
4272   if (num_delays > 1)
4273     {
4274       attr = find_attr (&delay_type_str, 0);
4275       gcc_assert (attr);
4276       common_av = find_most_used (attr);
4277
4278       printf ("  insn = delay_insn;\n");
4279       printf ("  switch (recog_memoized (insn))\n");
4280       printf ("    {\n");
4281
4282       sprintf (str, " * %d;\n      break;", max_slots);
4283       for (av = attr->first_value; av; av = av->next)
4284         if (av != common_av)
4285           write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
4286
4287       write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
4288       printf ("    }\n\n");
4289
4290       /* Ensure matched.  Otherwise, shouldn't have been called.  */
4291       printf ("  gcc_assert (slot >= %d);\n\n", max_slots);
4292     }
4293
4294   /* If just one type of delay slot, write simple switch.  */
4295   if (num_delays == 1 && max_slots == 1)
4296     {
4297       printf ("  insn = candidate_insn;\n");
4298       printf ("  switch (recog_memoized (insn))\n");
4299       printf ("    {\n");
4300
4301       attr = find_attr (&delay_1_0_str, 0);
4302       gcc_assert (attr);
4303       common_av = find_most_used (attr);
4304
4305       for (av = attr->first_value; av; av = av->next)
4306         if (av != common_av)
4307           write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4308
4309       write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4310       printf ("    }\n");
4311     }
4312
4313   else
4314     {
4315       /* Write a nested CASE.  The first indicates which condition we need to
4316          test, and the inner CASE tests the condition.  */
4317       printf ("  insn = candidate_insn;\n");
4318       printf ("  switch (slot)\n");
4319       printf ("    {\n");
4320
4321       for (delay = delays; delay; delay = delay->next)
4322         for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
4323           {
4324             printf ("    case %d:\n",
4325                     (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
4326             printf ("      switch (recog_memoized (insn))\n");
4327             printf ("\t{\n");
4328
4329             sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
4330             pstr = str;
4331             attr = find_attr (&pstr, 0);
4332             gcc_assert (attr);
4333             common_av = find_most_used (attr);
4334
4335             for (av = attr->first_value; av; av = av->next)
4336               if (av != common_av)
4337                 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
4338
4339             write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
4340             printf ("      }\n");
4341           }
4342
4343       printf ("    default:\n");
4344       printf ("      gcc_unreachable ();\n");
4345       printf ("    }\n");
4346     }
4347
4348   printf ("}\n\n");
4349 }
4350
4351 /* This page contains miscellaneous utility routines.  */
4352
4353 /* Given a pointer to a (char *), return a malloc'ed string containing the
4354    next comma-separated element.  Advance the pointer to after the string
4355    scanned, or the end-of-string.  Return NULL if at end of string.  */
4356
4357 static char *
4358 next_comma_elt (const char **pstr)
4359 {
4360   const char *start;
4361
4362   start = scan_comma_elt (pstr);
4363
4364   if (start == NULL)
4365     return NULL;
4366
4367   return attr_string (start, *pstr - start);
4368 }
4369
4370 /* Return a `struct attr_desc' pointer for a given named attribute.  If CREATE
4371    is nonzero, build a new attribute, if one does not exist.  *NAME_P is
4372    replaced by a pointer to a canonical copy of the string.  */
4373
4374 static struct attr_desc *
4375 find_attr (const char **name_p, int create)
4376 {
4377   struct attr_desc *attr;
4378   int index;
4379   const char *name = *name_p;
4380
4381   /* Before we resort to using `strcmp', see if the string address matches
4382      anywhere.  In most cases, it should have been canonicalized to do so.  */
4383   if (name == alternative_name)
4384     return NULL;
4385
4386   index = name[0] & (MAX_ATTRS_INDEX - 1);
4387   for (attr = attrs[index]; attr; attr = attr->next)
4388     if (name == attr->name)
4389       return attr;
4390
4391   /* Otherwise, do it the slow way.  */
4392   for (attr = attrs[index]; attr; attr = attr->next)
4393     if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
4394       {
4395         *name_p = attr->name;
4396         return attr;
4397       }
4398
4399   if (! create)
4400     return NULL;
4401
4402   attr = oballoc (struct attr_desc);
4403   attr->name = DEF_ATTR_STRING (name);
4404   attr->enum_name = 0;
4405   attr->first_value = attr->default_val = NULL;
4406   attr->is_numeric = attr->is_const = attr->is_special = 0;
4407   attr->next = attrs[index];
4408   attrs[index] = attr;
4409
4410   *name_p = attr->name;
4411
4412   return attr;
4413 }
4414
4415 /* Create internal attribute with the given default value.  */
4416
4417 static void
4418 make_internal_attr (const char *name, rtx value, int special)
4419 {
4420   struct attr_desc *attr;
4421
4422   attr = find_attr (&name, 1);
4423   gcc_assert (!attr->default_val);
4424
4425   attr->is_numeric = 1;
4426   attr->is_const = 0;
4427   attr->is_special = (special & ATTR_SPECIAL) != 0;
4428   attr->default_val = get_attr_value (value, attr, -2);
4429 }
4430
4431 /* Find the most used value of an attribute.  */
4432
4433 static struct attr_value *
4434 find_most_used (struct attr_desc *attr)
4435 {
4436   struct attr_value *av;
4437   struct attr_value *most_used;
4438   int nuses;
4439
4440   most_used = NULL;
4441   nuses = -1;
4442
4443   for (av = attr->first_value; av; av = av->next)
4444     if (av->num_insns > nuses)
4445       nuses = av->num_insns, most_used = av;
4446
4447   return most_used;
4448 }
4449
4450 /* Return (attr_value "n") */
4451
4452 static rtx
4453 make_numeric_value (int n)
4454 {
4455   static rtx int_values[20];
4456   rtx exp;
4457   char *p;
4458
4459   gcc_assert (n >= 0);
4460
4461   if (n < 20 && int_values[n])
4462     return int_values[n];
4463
4464   p = attr_printf (MAX_DIGITS, "%d", n);
4465   exp = attr_rtx (CONST_STRING, p);
4466
4467   if (n < 20)
4468     int_values[n] = exp;
4469
4470   return exp;
4471 }
4472
4473 static rtx
4474 copy_rtx_unchanging (rtx orig)
4475 {
4476   if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
4477     return orig;
4478
4479   ATTR_CURR_SIMPLIFIED_P (orig) = 1;
4480   return orig;
4481 }
4482
4483 /* Determine if an insn has a constant number of delay slots, i.e., the
4484    number of delay slots is not a function of the length of the insn.  */
4485
4486 static void
4487 write_const_num_delay_slots (void)
4488 {
4489   struct attr_desc *attr = find_attr (&num_delay_slots_str, 0);
4490   struct attr_value *av;
4491
4492   if (attr)
4493     {
4494       printf ("int\nconst_num_delay_slots (rtx insn)\n");
4495       printf ("{\n");
4496       printf ("  switch (recog_memoized (insn))\n");
4497       printf ("    {\n");
4498
4499       for (av = attr->first_value; av; av = av->next)
4500         {
4501           length_used = 0;
4502           walk_attr_value (av->value);
4503           if (length_used)
4504             write_insn_cases (av->first_insn, 4);
4505         }
4506
4507       printf ("    default:\n");
4508       printf ("      return 1;\n");
4509       printf ("    }\n}\n\n");
4510     }
4511 }
4512 \f
4513 /* Synthetic attributes used by insn-automata.c and the scheduler.
4514    These are primarily concerned with (define_insn_reservation)
4515    patterns.  */
4516
4517 struct insn_reserv
4518 {
4519   struct insn_reserv *next;
4520
4521   const char *name;
4522   int default_latency;
4523   rtx condexp;
4524
4525   /* Sequence number of this insn.  */
4526   int insn_num;
4527
4528   /* Whether a (define_bypass) construct names this insn in its
4529      output list.  */
4530   bool bypassed;
4531 };
4532
4533 static struct insn_reserv *all_insn_reservs = 0;
4534 static struct insn_reserv **last_insn_reserv_p = &all_insn_reservs;
4535 static size_t n_insn_reservs;
4536
4537 /* Store information from a DEFINE_INSN_RESERVATION for future
4538    attribute generation.  */
4539 static void
4540 gen_insn_reserv (rtx def)
4541 {
4542   struct insn_reserv *decl = oballoc (struct insn_reserv);
4543
4544   decl->name            = DEF_ATTR_STRING (XSTR (def, 0));
4545   decl->default_latency = XINT (def, 1);
4546   decl->condexp         = check_attr_test (XEXP (def, 2), 0, 0);
4547   decl->insn_num        = n_insn_reservs;
4548   decl->bypassed        = false;
4549   decl->next            = 0;
4550
4551   *last_insn_reserv_p = decl;
4552   last_insn_reserv_p  = &decl->next;
4553   n_insn_reservs++;
4554 }
4555
4556 /* Store information from a DEFINE_BYPASS for future attribute
4557    generation.  The only thing we care about is the list of output
4558    insns, which will later be used to tag reservation structures with
4559    a 'bypassed' bit.  */
4560
4561 struct bypass_list
4562 {
4563   struct bypass_list *next;
4564   const char *pattern;
4565 };
4566
4567 static struct bypass_list *all_bypasses;
4568 static size_t n_bypasses;
4569
4570 static void
4571 gen_bypass_1 (const char *s, size_t len)
4572 {
4573   struct bypass_list *b;
4574
4575   if (len == 0)
4576     return;
4577
4578   s = attr_string (s, len);
4579   for (b = all_bypasses; b; b = b->next)
4580     if (s == b->pattern)
4581       return;  /* already got that one */
4582
4583   b = oballoc (struct bypass_list);
4584   b->pattern = s;
4585   b->next = all_bypasses;
4586   all_bypasses = b;
4587   n_bypasses++;
4588 }
4589
4590 static void
4591 gen_bypass (rtx def)
4592 {
4593   const char *p, *base;
4594
4595   for (p = base = XSTR (def, 1); *p; p++)
4596     if (*p == ',')
4597       {
4598         gen_bypass_1 (base, p - base);
4599         do
4600           p++;
4601         while (ISSPACE (*p));
4602         base = p;
4603       }
4604   gen_bypass_1 (base, p - base);
4605 }
4606
4607 /* Find and mark all of the bypassed insns.  */
4608 static void
4609 process_bypasses (void)
4610 {
4611   struct bypass_list *b;
4612   struct insn_reserv *r;
4613
4614   /* The reservation list is likely to be much longer than the bypass
4615      list.  */
4616   for (r = all_insn_reservs; r; r = r->next)
4617     for (b = all_bypasses; b; b = b->next)
4618       if (fnmatch (b->pattern, r->name, 0) == 0)
4619         r->bypassed = true;
4620 }
4621
4622 /* Check that attribute NAME is used in define_insn_reservation condition
4623    EXP.  Return true if it is.  */
4624 static bool
4625 check_tune_attr (const char *name, rtx exp)
4626 {
4627   switch (GET_CODE (exp))
4628     {
4629     case AND:
4630       if (check_tune_attr (name, XEXP (exp, 0)))
4631         return true;
4632       return check_tune_attr (name, XEXP (exp, 1));
4633
4634     case IOR:
4635       return (check_tune_attr (name, XEXP (exp, 0))
4636               && check_tune_attr (name, XEXP (exp, 1)));
4637
4638     case EQ_ATTR:
4639       return XSTR (exp, 0) == name;
4640
4641     default:
4642       return false;
4643     }
4644 }
4645
4646 /* Try to find a const attribute (usually cpu or tune) that is used
4647    in all define_insn_reservation conditions.  */
4648 static struct attr_desc *
4649 find_tune_attr (rtx exp)
4650 {
4651   struct attr_desc *attr;
4652
4653   switch (GET_CODE (exp))
4654     {
4655     case AND:
4656     case IOR:
4657       attr = find_tune_attr (XEXP (exp, 0));
4658       if (attr)
4659         return attr;
4660       return find_tune_attr (XEXP (exp, 1));
4661
4662     case EQ_ATTR:
4663       if (XSTR (exp, 0) == alternative_name)
4664         return NULL;
4665
4666       attr = find_attr (&XSTR (exp, 0), 0);
4667       gcc_assert (attr);
4668
4669       if (attr->is_const && !attr->is_special)
4670         {
4671           struct insn_reserv *decl;
4672
4673           for (decl = all_insn_reservs; decl; decl = decl->next)
4674             if (! check_tune_attr (attr->name, decl->condexp))
4675               return NULL;
4676           return attr;
4677         }
4678       return NULL;
4679
4680     default:
4681       return NULL;
4682     }
4683 }
4684
4685 /* Create all of the attributes that describe automaton properties.  */
4686 static void
4687 make_automaton_attrs (void)
4688 {
4689   int i;
4690   struct insn_reserv *decl;
4691   rtx code_exp, lats_exp, byps_exp;
4692   struct attr_desc *tune_attr;
4693
4694   if (n_insn_reservs == 0)
4695     return;
4696
4697   tune_attr = find_tune_attr (all_insn_reservs->condexp);
4698   if (tune_attr != NULL)
4699     {
4700       rtx *condexps = XNEWVEC (rtx, n_insn_reservs * 3);
4701       struct attr_value *val;
4702       bool first = true;
4703
4704       gcc_assert (tune_attr->is_const
4705                   && !tune_attr->is_special
4706                   && !tune_attr->is_numeric);
4707       for (val = tune_attr->first_value; val; val = val->next)
4708         {
4709           if (val == tune_attr->default_val)
4710             continue;
4711           gcc_assert (GET_CODE (val->value) == CONST_STRING);
4712           printf ("static int internal_dfa_insn_code_%s (rtx);\n"
4713                   "static int insn_default_latency_%s (rtx);\n",
4714                   XSTR (val->value, 0), XSTR (val->value, 0));
4715         }
4716
4717       printf ("\n");
4718       printf ("int (*internal_dfa_insn_code) (rtx);\n");
4719       printf ("int (*insn_default_latency) (rtx);\n");
4720       printf ("\n");
4721       printf ("void\n");
4722       printf ("init_sched_attrs (void)\n");
4723       printf ("{\n");
4724
4725       for (val = tune_attr->first_value; val; val = val->next)
4726         {
4727           int j;
4728           char *name;
4729           rtx test = attr_rtx (EQ_ATTR, tune_attr->name, XSTR (val->value, 0));
4730
4731           if (val == tune_attr->default_val)
4732             continue;
4733           for (decl = all_insn_reservs, i = 0;
4734                decl;
4735                decl = decl->next)
4736             {
4737               rtx ctest = test;
4738               rtx condexp
4739                 = simplify_and_tree (decl->condexp, &ctest, -2, 0);
4740               if (condexp == false_rtx)
4741                 continue;
4742               if (condexp == true_rtx)
4743                 break;
4744               condexps[i] = condexp;
4745               condexps[i + 1] = make_numeric_value (decl->insn_num);
4746               condexps[i + 2] = make_numeric_value (decl->default_latency);
4747               i += 3;
4748             }
4749
4750           code_exp = rtx_alloc (COND);
4751           lats_exp = rtx_alloc (COND);
4752
4753           j = i / 3 * 2;
4754           XVEC (code_exp, 0) = rtvec_alloc (j);
4755           XVEC (lats_exp, 0) = rtvec_alloc (j);
4756
4757           if (decl)
4758             {
4759               XEXP (code_exp, 1) = make_numeric_value (decl->insn_num);
4760               XEXP (lats_exp, 1) = make_numeric_value (decl->default_latency);
4761             }
4762           else
4763             {
4764               XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
4765               XEXP (lats_exp, 1) = make_numeric_value (0);
4766             }
4767
4768           while (i > 0)
4769             {
4770               i -= 3;
4771               j -= 2;
4772               XVECEXP (code_exp, 0, j) = condexps[i];
4773               XVECEXP (lats_exp, 0, j) = condexps[i];
4774
4775               XVECEXP (code_exp, 0, j + 1) = condexps[i + 1];
4776               XVECEXP (lats_exp, 0, j + 1) = condexps[i + 2];
4777             }
4778
4779           name = XNEWVEC (char,
4780                           sizeof ("*internal_dfa_insn_code_")
4781                           + strlen (XSTR (val->value, 0)));
4782           strcpy (name, "*internal_dfa_insn_code_");
4783           strcat (name, XSTR (val->value, 0));
4784           make_internal_attr (name, code_exp, ATTR_NONE);
4785           strcpy (name, "*insn_default_latency_");
4786           strcat (name, XSTR (val->value, 0));
4787           make_internal_attr (name, lats_exp, ATTR_NONE);
4788           XDELETEVEC (name);
4789
4790           if (first)
4791             {
4792               printf ("  if (");
4793               first = false;
4794             }
4795           else
4796             printf ("  else if (");
4797           write_test_expr (test, 0, 0);
4798           printf (")\n");
4799           printf ("    {\n");
4800           printf ("      internal_dfa_insn_code\n");
4801           printf ("        = internal_dfa_insn_code_%s;\n",
4802                   XSTR (val->value, 0));
4803           printf ("      insn_default_latency\n");
4804           printf ("        = insn_default_latency_%s;\n",
4805                   XSTR (val->value, 0));
4806           printf ("    }\n");
4807         }
4808
4809       printf ("  else\n");
4810       printf ("    gcc_unreachable ();\n");
4811       printf ("}\n");
4812       printf ("\n");
4813
4814       XDELETEVEC (condexps);
4815     }
4816   else
4817     {
4818       code_exp = rtx_alloc (COND);
4819       lats_exp = rtx_alloc (COND);
4820
4821       XVEC (code_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
4822       XVEC (lats_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
4823
4824       XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
4825       XEXP (lats_exp, 1) = make_numeric_value (0);
4826
4827       for (decl = all_insn_reservs, i = 0;
4828            decl;
4829            decl = decl->next, i += 2)
4830         {
4831           XVECEXP (code_exp, 0, i)   = decl->condexp;
4832           XVECEXP (lats_exp, 0, i)   = decl->condexp;
4833
4834           XVECEXP (code_exp, 0, i+1) = make_numeric_value (decl->insn_num);
4835           XVECEXP (lats_exp, 0, i+1)
4836             = make_numeric_value (decl->default_latency);
4837         }
4838       make_internal_attr ("*internal_dfa_insn_code", code_exp, ATTR_NONE);
4839       make_internal_attr ("*insn_default_latency",   lats_exp, ATTR_NONE);
4840     }
4841
4842   if (n_bypasses == 0)
4843     byps_exp = make_numeric_value (0);
4844   else
4845     {
4846       process_bypasses ();
4847
4848       byps_exp = rtx_alloc (COND);
4849       XVEC (byps_exp, 0) = rtvec_alloc (n_bypasses * 2);
4850       XEXP (byps_exp, 1) = make_numeric_value (0);
4851       for (decl = all_insn_reservs, i = 0;
4852            decl;
4853            decl = decl->next)
4854         if (decl->bypassed)
4855           {
4856             XVECEXP (byps_exp, 0, i)   = decl->condexp;
4857             XVECEXP (byps_exp, 0, i+1) = make_numeric_value (1);
4858             i += 2;
4859           }
4860     }
4861
4862   make_internal_attr ("*bypass_p",               byps_exp, ATTR_NONE);
4863 }
4864
4865 int
4866 main (int argc, char **argv)
4867 {
4868   rtx desc;
4869   struct attr_desc *attr;
4870   struct insn_def *id;
4871   rtx tem;
4872   int i;
4873
4874   progname = "genattrtab";
4875
4876   if (!init_rtx_reader_args (argc, argv))
4877     return (FATAL_EXIT_CODE);
4878
4879   obstack_init (hash_obstack);
4880   obstack_init (temp_obstack);
4881
4882   /* Set up true and false rtx's */
4883   true_rtx = rtx_alloc (CONST_INT);
4884   XWINT (true_rtx, 0) = 1;
4885   false_rtx = rtx_alloc (CONST_INT);
4886   XWINT (false_rtx, 0) = 0;
4887   ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
4888   ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
4889
4890   alternative_name = DEF_ATTR_STRING ("alternative");
4891   length_str = DEF_ATTR_STRING ("length");
4892   delay_type_str = DEF_ATTR_STRING ("*delay_type");
4893   delay_1_0_str = DEF_ATTR_STRING ("*delay_1_0");
4894   num_delay_slots_str = DEF_ATTR_STRING ("*num_delay_slots");
4895
4896   printf ("/* Generated automatically by the program `genattrtab'\n\
4897 from the machine description file `md'.  */\n\n");
4898
4899   /* Read the machine description.  */
4900
4901   while (1)
4902     {
4903       int lineno;
4904
4905       desc = read_md_rtx (&lineno, &insn_code_number);
4906       if (desc == NULL)
4907         break;
4908
4909       switch (GET_CODE (desc))
4910         {
4911         case DEFINE_INSN:
4912         case DEFINE_PEEPHOLE:
4913         case DEFINE_ASM_ATTRIBUTES:
4914           gen_insn (desc, lineno);
4915           break;
4916
4917         case DEFINE_ATTR:
4918         case DEFINE_ENUM_ATTR:
4919           gen_attr (desc, lineno);
4920           break;
4921
4922         case DEFINE_DELAY:
4923           gen_delay (desc, lineno);
4924           break;
4925
4926         case DEFINE_INSN_RESERVATION:
4927           gen_insn_reserv (desc);
4928           break;
4929
4930         case DEFINE_BYPASS:
4931           gen_bypass (desc);
4932           break;
4933
4934         default:
4935           break;
4936         }
4937       if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
4938         insn_index_number++;
4939     }
4940
4941   if (have_error)
4942     return FATAL_EXIT_CODE;
4943
4944   insn_code_number++;
4945
4946   /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one.  */
4947   if (! got_define_asm_attributes)
4948     {
4949       tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
4950       XVEC (tem, 0) = rtvec_alloc (0);
4951       gen_insn (tem, 0);
4952     }
4953
4954   /* Expand DEFINE_DELAY information into new attribute.  */
4955   if (num_delays)
4956     expand_delays ();
4957
4958   printf ("#include \"config.h\"\n");
4959   printf ("#include \"system.h\"\n");
4960   printf ("#include \"coretypes.h\"\n");
4961   printf ("#include \"tm.h\"\n");
4962   printf ("#include \"rtl.h\"\n");
4963   printf ("#include \"insn-attr.h\"\n");
4964   printf ("#include \"tm_p.h\"\n");
4965   printf ("#include \"insn-config.h\"\n");
4966   printf ("#include \"recog.h\"\n");
4967   printf ("#include \"regs.h\"\n");
4968   printf ("#include \"output.h\"\n");
4969   printf ("#include \"diagnostic-core.h\"\n");
4970   printf ("#include \"flags.h\"\n");
4971   printf ("#include \"function.h\"\n");
4972   printf ("\n");
4973   printf ("#define operands recog_data.operand\n\n");
4974
4975   /* Make `insn_alternatives'.  */
4976   insn_alternatives = oballocvec (int, insn_code_number);
4977   for (id = defs; id; id = id->next)
4978     if (id->insn_code >= 0)
4979       insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
4980
4981   /* Make `insn_n_alternatives'.  */
4982   insn_n_alternatives = oballocvec (int, insn_code_number);
4983   for (id = defs; id; id = id->next)
4984     if (id->insn_code >= 0)
4985       insn_n_alternatives[id->insn_code] = id->num_alternatives;
4986
4987   /* Construct extra attributes for automata.  */
4988   make_automaton_attrs ();
4989
4990   /* Prepare to write out attribute subroutines by checking everything stored
4991      away and building the attribute cases.  */
4992
4993   check_defs ();
4994
4995   for (i = 0; i < MAX_ATTRS_INDEX; i++)
4996     for (attr = attrs[i]; attr; attr = attr->next)
4997       attr->default_val->value
4998         = check_attr_value (attr->default_val->value, attr);
4999
5000   if (have_error)
5001     return FATAL_EXIT_CODE;
5002
5003   for (i = 0; i < MAX_ATTRS_INDEX; i++)
5004     for (attr = attrs[i]; attr; attr = attr->next)
5005       fill_attr (attr);
5006
5007   /* Construct extra attributes for `length'.  */
5008   make_length_attrs ();
5009
5010   /* Perform any possible optimizations to speed up compilation.  */
5011   optimize_attrs ();
5012
5013   /* Now write out all the `gen_attr_...' routines.  Do these before the
5014      special routines so that they get defined before they are used.  */
5015
5016   for (i = 0; i < MAX_ATTRS_INDEX; i++)
5017     for (attr = attrs[i]; attr; attr = attr->next)
5018       {
5019         if (! attr->is_special && ! attr->is_const)
5020           write_attr_get (attr);
5021       }
5022
5023   /* Write out delay eligibility information, if DEFINE_DELAY present.
5024      (The function to compute the number of delay slots will be written
5025      below.)  */
5026   if (num_delays)
5027     {
5028       write_eligible_delay ("delay");
5029       if (have_annul_true)
5030         write_eligible_delay ("annul_true");
5031       if (have_annul_false)
5032         write_eligible_delay ("annul_false");
5033     }
5034
5035   /* Write out constant delay slot info.  */
5036   write_const_num_delay_slots ();
5037
5038   write_length_unit_log ();
5039
5040   fflush (stdout);
5041   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
5042 }