OSDN Git Service

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