OSDN Git Service

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