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