OSDN Git Service

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