OSDN Git Service

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