OSDN Git Service

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