OSDN Git Service

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