OSDN Git Service

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