OSDN Git Service

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