OSDN Git Service

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