OSDN Git Service

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