OSDN Git Service

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