OSDN Git Service

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