Initial revision
[official-gcc.git] / gcc / genattrtab.c
blobbde3f5199a29f325f29dff081f5b76a0fbc41654
1 /* Generate code from machine description to compute values of attributes.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* This program handles insn attributes and the DEFINE_DELAY and
23 DEFINE_FUNCTION_UNIT definitions.
25 It produces a series of functions named `get_attr_...', one for each insn
26 attribute. Each of these is given the rtx for an insn and returns a member
27 of the enum for the attribute.
29 These subroutines have the form of a `switch' on the INSN_CODE (via
30 `recog_memoized'). Each case either returns a constant attribute value
31 or a value that depends on tests on other attributes, the form of
32 operands, or some random C expression (encoded with a SYMBOL_REF
33 expression).
35 If the attribute `alternative', or a random C expression is present,
36 `constrain_operands' is called. If either of these cases of a reference to
37 an operand is found, `insn_extract' is called.
39 The special attribute `length' is also recognized. For this operand,
40 expressions involving the address of an operand or the current insn,
41 (address (pc)), are valid. In this case, an initial pass is made to
42 set all lengths that do not depend on address. Those that do are set to
43 the maximum length. Then each insn that depends on an address is checked
44 and possibly has its length changed. The process repeats until no further
45 changed are made. The resulting lengths are saved for use by
46 `get_attr_length'.
48 A special form of DEFINE_ATTR, where the expression for default value is a
49 CONST expression, indicates an attribute that is constant for a given run
50 of the compiler. The subroutine generated for these attributes has no
51 parameters as it does not depend on any particular insn. Constant
52 attributes are typically used to specify which variety of processor is
53 used.
55 Internal attributes are defined to handle DEFINE_DELAY and
56 DEFINE_FUNCTION_UNIT. Special routines are output for these cases.
58 This program works by keeping a list of possible values for each attribute.
59 These include the basic attribute choices, default values for attribute, and
60 all derived quantities.
62 As the description file is read, the definition for each insn is saved in a
63 `struct insn_def'. When the file reading is complete, a `struct insn_ent'
64 is created for each insn and chained to the corresponding attribute value,
65 either that specified, or the default.
67 An optimization phase is then run. This simplifies expressions for each
68 insn. EQ_ATTR tests are resolved, whenever possible, to a test that
69 indicates when the attribute has the specified value for the insn. This
70 avoids recursive calls during compilation.
72 The strategy used when processing DEFINE_DELAY and DEFINE_FUNCTION_UNIT
73 definitions is to create arbitrarily complex expressions and have the
74 optimization simplify them.
76 Once optimization is complete, any required routines and definitions
77 will be written.
79 An optimization that is not yet implemented is to hoist the constant
80 expressions entirely out of the routines and definitions that are written.
81 A way to do this is to iterate over all possible combinations of values
82 for constant attributes and generate a set of functions for that given
83 combination. An initialization function would be written that evaluates
84 the attributes and installs the corresponding set of routines and
85 definitions (each would be accessed through a pointer).
87 We use the flags in an RTX as follows:
88 `unchanging' (RTX_UNCHANGING_P): This rtx is fully simplified
89 independent of the insn code.
90 `in_struct' (MEM_IN_STRUCT_P): This rtx is fully simplified
91 for the insn code currently being processed (see optimize_attrs).
92 `integrated' (RTX_INTEGRATED_P): This rtx is permanent and unique
93 (see attr_rtx).
94 `volatil' (MEM_VOLATILE_P): During simplify_by_exploding the value of an
95 EQ_ATTR rtx is true if !volatil and false if volatil. */
98 #include "hconfig.h"
99 /* varargs must always be included after *config.h. */
100 #ifdef __STDC__
101 #include <stdarg.h>
102 #else
103 #include <varargs.h>
104 #endif
105 #include "rtl.h"
106 #include "insn-config.h" /* For REGISTER_CONSTRAINTS */
107 #include <stdio.h>
109 #ifndef VMS
110 #ifndef USG
111 #include <sys/time.h>
112 #include <sys/resource.h>
113 #endif
114 #endif
116 /* We must include obstack.h after <sys/time.h>, to avoid lossage with
117 /usr/include/sys/stdtypes.h on Sun OS 4.x. */
118 #include "obstack.h"
120 static struct obstack obstack, obstack1, obstack2;
121 struct obstack *rtl_obstack = &obstack;
122 struct obstack *hash_obstack = &obstack1;
123 struct obstack *temp_obstack = &obstack2;
125 #define obstack_chunk_alloc xmalloc
126 #define obstack_chunk_free free
128 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
129 char **insn_name_ptr = 0;
131 extern void free ();
132 extern rtx read_rtx ();
134 static void fatal ();
135 void fancy_abort ();
137 /* enough space to reserve for printing out ints */
138 #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
140 /* Define structures used to record attributes and values. */
142 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
143 encountered, we store all the relevant information into a
144 `struct insn_def'. This is done to allow attribute definitions to occur
145 anywhere in the file. */
147 struct insn_def
149 int insn_code; /* Instruction number. */
150 int insn_index; /* Expression numer in file, for errors. */
151 struct insn_def *next; /* Next insn in chain. */
152 rtx def; /* The DEFINE_... */
153 int num_alternatives; /* Number of alternatives. */
154 int vec_idx; /* Index of attribute vector in `def'. */
157 /* Once everything has been read in, we store in each attribute value a list
158 of insn codes that have that value. Here is the structure used for the
159 list. */
161 struct insn_ent
163 int insn_code; /* Instruction number. */
164 int insn_index; /* Index of definition in file */
165 struct insn_ent *next; /* Next in chain. */
168 /* Each value of an attribute (either constant or computed) is assigned a
169 structure which is used as the listhead of the insns that have that
170 value. */
172 struct attr_value
174 rtx value; /* Value of attribute. */
175 struct attr_value *next; /* Next attribute value in chain. */
176 struct insn_ent *first_insn; /* First insn with this value. */
177 int num_insns; /* Number of insns with this value. */
178 int has_asm_insn; /* True if this value used for `asm' insns */
181 /* Structure for each attribute. */
183 struct attr_desc
185 char *name; /* Name of attribute. */
186 struct attr_desc *next; /* Next attribute. */
187 int is_numeric; /* Values of this attribute are numeric. */
188 int negative_ok; /* Allow negative numeric values. */
189 int unsigned_p; /* Make the output function unsigned int. */
190 int is_const; /* Attribute value constant for each run. */
191 int is_special; /* Don't call `write_attr_set'. */
192 struct attr_value *first_value; /* First value of this attribute. */
193 struct attr_value *default_val; /* Default value for this attribute. */
196 #define NULL_ATTR (struct attr_desc *) NULL
198 /* A range of values. */
200 struct range
202 int min;
203 int max;
206 /* Structure for each DEFINE_DELAY. */
208 struct delay_desc
210 rtx def; /* DEFINE_DELAY expression. */
211 struct delay_desc *next; /* Next DEFINE_DELAY. */
212 int num; /* Number of DEFINE_DELAY, starting at 1. */
215 /* Record information about each DEFINE_FUNCTION_UNIT. */
217 struct function_unit_op
219 rtx condexp; /* Expression TRUE for applicable insn. */
220 struct function_unit_op *next; /* Next operation for this function unit. */
221 int num; /* Ordinal for this operation type in unit. */
222 int ready; /* Cost until data is ready. */
223 int issue_delay; /* Cost until unit can accept another insn. */
224 rtx conflict_exp; /* Expression TRUE for insns incurring issue delay. */
225 rtx issue_exp; /* Expression computing issue delay. */
228 /* Record information about each function unit mentioned in a
229 DEFINE_FUNCTION_UNIT. */
231 struct function_unit
233 char *name; /* Function unit name. */
234 struct function_unit *next; /* Next function unit. */
235 int num; /* Ordinal of this unit type. */
236 int multiplicity; /* Number of units of this type. */
237 int simultaneity; /* Maximum number of simultaneous insns
238 on this function unit or 0 if unlimited. */
239 rtx condexp; /* Expression TRUE for insn needing unit. */
240 int num_opclasses; /* Number of different operation types. */
241 struct function_unit_op *ops; /* Pointer to first operation type. */
242 int needs_conflict_function; /* Nonzero if a conflict function required. */
243 int needs_blockage_function; /* Nonzero if a blockage function required. */
244 int needs_range_function; /* Nonzero if blockage range function needed.*/
245 rtx default_cost; /* Conflict cost, if constant. */
246 struct range issue_delay; /* Range of issue delay values. */
247 int max_blockage; /* Maximum time an insn blocks the unit. */
250 /* Listheads of above structures. */
252 /* This one is indexed by the first character of the attribute name. */
253 #define MAX_ATTRS_INDEX 256
254 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
255 static struct insn_def *defs;
256 static struct delay_desc *delays;
257 static struct function_unit *units;
259 /* An expression where all the unknown terms are EQ_ATTR tests can be
260 rearranged into a COND provided we can enumerate all possible
261 combinations of the unknown values. The set of combinations become the
262 tests of the COND; the value of the expression given that combination is
263 computed and becomes the corresponding value. To do this, we must be
264 able to enumerate all values for each attribute used in the expression
265 (currently, we give up if we find a numeric attribute).
267 If the set of EQ_ATTR tests used in an expression tests the value of N
268 different attributes, the list of all possible combinations can be made
269 by walking the N-dimensional attribute space defined by those
270 attributes. We record each of these as a struct dimension.
272 The algorithm relies on sharing EQ_ATTR nodes: if two nodes in an
273 expression are the same, the will also have the same address. We find
274 all the EQ_ATTR nodes by marking them MEM_VOLATILE_P. This bit later
275 represents the value of an EQ_ATTR node, so once all nodes are marked,
276 they are also given an initial value of FALSE.
278 We then separate the set of EQ_ATTR nodes into dimensions for each
279 attribute and put them on the VALUES list. Terms are added as needed by
280 `add_values_to_cover' so that all possible values of the attribute are
281 tested.
283 Each dimension also has a current value. This is the node that is
284 currently considered to be TRUE. If this is one of the nodes added by
285 `add_values_to_cover', all the EQ_ATTR tests in the original expression
286 will be FALSE. Otherwise, only the CURRENT_VALUE will be true.
288 NUM_VALUES is simply the length of the VALUES list and is there for
289 convenience.
291 Once the dimensions are created, the algorithm enumerates all possible
292 values and computes the current value of the given expression. */
294 struct dimension
296 struct attr_desc *attr; /* Attribute for this dimension. */
297 rtx values; /* List of attribute values used. */
298 rtx current_value; /* Position in the list for the TRUE value. */
299 int num_values; /* Length of the values list. */
302 /* Other variables. */
304 static int insn_code_number;
305 static int insn_index_number;
306 static int got_define_asm_attributes;
307 static int must_extract;
308 static int must_constrain;
309 static int address_used;
310 static int length_used;
311 static int num_delays;
312 static int have_annul_true, have_annul_false;
313 static int num_units;
314 static int num_insn_ents;
316 /* Used as operand to `operate_exp': */
318 enum operator {PLUS_OP, MINUS_OP, POS_MINUS_OP, EQ_OP, OR_OP, MAX_OP, MIN_OP, RANGE_OP};
320 /* Stores, for each insn code, the number of constraint alternatives. */
322 static int *insn_n_alternatives;
324 /* Stores, for each insn code, a bitmap that has bits on for each possible
325 alternative. */
327 static int *insn_alternatives;
329 /* If nonzero, assume that the `alternative' attr has this value.
330 This is the hashed, unique string for the numeral
331 whose value is chosen alternative. */
333 static char *current_alternative_string;
335 /* Used to simplify expressions. */
337 static rtx true_rtx, false_rtx;
339 /* Used to reduce calls to `strcmp' */
341 static char *alternative_name;
343 /* Simplify an expression. Only call the routine if there is something to
344 simplify. */
345 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
346 (RTX_UNCHANGING_P (EXP) || MEM_IN_STRUCT_P (EXP) ? (EXP) \
347 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
349 /* Simplify (eq_attr ("alternative") ...)
350 when we are working with a particular alternative. */
351 #define SIMPLIFY_ALTERNATIVE(EXP) \
352 if (current_alternative_string \
353 && GET_CODE ((EXP)) == EQ_ATTR \
354 && XSTR ((EXP), 0) == alternative_name) \
355 (EXP) = (XSTR ((EXP), 1) == current_alternative_string \
356 ? true_rtx : false_rtx);
358 /* These are referenced by rtlanal.c and hence need to be defined somewhere.
359 They won't actually be used. */
361 rtx frame_pointer_rtx, hard_frame_pointer_rtx, stack_pointer_rtx;
362 rtx arg_pointer_rtx;
364 static rtx attr_rtx PVPROTO((enum rtx_code, ...));
365 #ifdef HAVE_VPRINTF
366 static char *attr_printf PVPROTO((int, char *, ...));
367 #else
368 static char *attr_printf ();
369 #endif
371 static char *attr_string PROTO((char *, int));
372 static rtx check_attr_test PROTO((rtx, int));
373 static rtx check_attr_value PROTO((rtx, struct attr_desc *));
374 static rtx convert_set_attr_alternative PROTO((rtx, int, int, int));
375 static rtx convert_set_attr PROTO((rtx, int, int, int));
376 static void check_defs PROTO((void));
377 static rtx convert_const_symbol_ref PROTO((rtx, struct attr_desc *));
378 static rtx make_canonical PROTO((struct attr_desc *, rtx));
379 static struct attr_value *get_attr_value PROTO((rtx, struct attr_desc *, int));
380 static rtx copy_rtx_unchanging PROTO((rtx));
381 static rtx copy_boolean PROTO((rtx));
382 static void expand_delays PROTO((void));
383 static rtx operate_exp PROTO((enum operator, rtx, rtx));
384 static void expand_units PROTO((void));
385 static rtx simplify_knowing PROTO((rtx, rtx));
386 static rtx encode_units_mask PROTO((rtx));
387 static void fill_attr PROTO((struct attr_desc *));
388 /* dpx2 compiler chokes if we specify the arg types of the args. */
389 static rtx substitute_address PROTO((rtx, rtx (*) (), rtx (*) ()));
390 static void make_length_attrs PROTO((void));
391 static rtx identity_fn PROTO((rtx));
392 static rtx zero_fn PROTO((rtx));
393 static rtx one_fn PROTO((rtx));
394 static rtx max_fn PROTO((rtx));
395 static rtx simplify_cond PROTO((rtx, int, int));
396 static rtx simplify_by_alternatives PROTO((rtx, int, int));
397 static rtx simplify_by_exploding PROTO((rtx));
398 static int find_and_mark_used_attributes PROTO((rtx, rtx *, int *));
399 static void unmark_used_attributes PROTO((rtx, struct dimension *, int));
400 static int add_values_to_cover PROTO((struct dimension *));
401 static int increment_current_value PROTO((struct dimension *, int));
402 static rtx test_for_current_value PROTO((struct dimension *, int));
403 static rtx simplify_with_current_value PROTO((rtx, struct dimension *, int));
404 static rtx simplify_with_current_value_aux PROTO((rtx));
405 static void clear_struct_flag PROTO((rtx));
406 static int count_sub_rtxs PROTO((rtx, int));
407 static void remove_insn_ent PROTO((struct attr_value *, struct insn_ent *));
408 static void insert_insn_ent PROTO((struct attr_value *, struct insn_ent *));
409 static rtx insert_right_side PROTO((enum rtx_code, rtx, rtx, int, int));
410 static rtx make_alternative_compare PROTO((int));
411 static int compute_alternative_mask PROTO((rtx, enum rtx_code));
412 static rtx evaluate_eq_attr PROTO((rtx, rtx, int, int));
413 static rtx simplify_and_tree PROTO((rtx, rtx *, int, int));
414 static rtx simplify_or_tree PROTO((rtx, rtx *, int, int));
415 static rtx simplify_test_exp PROTO((rtx, int, int));
416 static void optimize_attrs PROTO((void));
417 static void gen_attr PROTO((rtx));
418 static int count_alternatives PROTO((rtx));
419 static int compares_alternatives_p PROTO((rtx));
420 static int contained_in_p PROTO((rtx, rtx));
421 static void gen_insn PROTO((rtx));
422 static void gen_delay PROTO((rtx));
423 static void gen_unit PROTO((rtx));
424 static void write_test_expr PROTO((rtx, int));
425 static int max_attr_value PROTO((rtx));
426 static void walk_attr_value PROTO((rtx));
427 static void write_attr_get PROTO((struct attr_desc *));
428 static rtx eliminate_known_true PROTO((rtx, rtx, int, int));
429 static void write_attr_set PROTO((struct attr_desc *, int, rtx, char *,
430 char *, rtx, int, int));
431 static void write_attr_case PROTO((struct attr_desc *, struct attr_value *,
432 int, char *, char *, int, rtx));
433 static void write_attr_valueq PROTO((struct attr_desc *, char *));
434 static void write_attr_value PROTO((struct attr_desc *, rtx));
435 static void write_upcase PROTO((char *));
436 static void write_indent PROTO((int));
437 static void write_eligible_delay PROTO((char *));
438 static void write_function_unit_info PROTO((void));
439 static void write_complex_function PROTO((struct function_unit *, char *,
440 char *));
441 static int n_comma_elts PROTO((char *));
442 static char *next_comma_elt PROTO((char **));
443 static struct attr_desc *find_attr PROTO((char *, int));
444 static void make_internal_attr PROTO((char *, rtx, int));
445 static struct attr_value *find_most_used PROTO((struct attr_desc *));
446 static rtx find_single_value PROTO((struct attr_desc *));
447 static rtx make_numeric_value PROTO((int));
448 static void extend_range PROTO((struct range *, int, int));
449 char *xrealloc PROTO((char *, unsigned));
450 char *xmalloc PROTO((unsigned));
452 #define oballoc(size) obstack_alloc (hash_obstack, size)
455 /* Hash table for sharing RTL and strings. */
457 /* Each hash table slot is a bucket containing a chain of these structures.
458 Strings are given negative hash codes; RTL expressions are given positive
459 hash codes. */
461 struct attr_hash
463 struct attr_hash *next; /* Next structure in the bucket. */
464 int hashcode; /* Hash code of this rtx or string. */
465 union
467 char *str; /* The string (negative hash codes) */
468 rtx rtl; /* or the RTL recorded here. */
469 } u;
472 /* Now here is the hash table. When recording an RTL, it is added to
473 the slot whose index is the hash code mod the table size. Note
474 that the hash table is used for several kinds of RTL (see attr_rtx)
475 and for strings. While all these live in the same table, they are
476 completely independent, and the hash code is computed differently
477 for each. */
479 #define RTL_HASH_SIZE 4093
480 struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
482 /* Here is how primitive or already-shared RTL's hash
483 codes are made. */
484 #define RTL_HASH(RTL) ((HOST_WIDE_INT) (RTL) & 0777777)
486 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
488 static void
489 attr_hash_add_rtx (hashcode, rtl)
490 int hashcode;
491 rtx rtl;
493 register struct attr_hash *h;
495 h = (struct attr_hash *) obstack_alloc (hash_obstack,
496 sizeof (struct attr_hash));
497 h->hashcode = hashcode;
498 h->u.rtl = rtl;
499 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
500 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
503 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
505 static void
506 attr_hash_add_string (hashcode, str)
507 int hashcode;
508 char *str;
510 register struct attr_hash *h;
512 h = (struct attr_hash *) obstack_alloc (hash_obstack,
513 sizeof (struct attr_hash));
514 h->hashcode = -hashcode;
515 h->u.str = str;
516 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
517 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
520 /* Generate an RTL expression, but avoid duplicates.
521 Set the RTX_INTEGRATED_P flag for these permanent objects.
523 In some cases we cannot uniquify; then we return an ordinary
524 impermanent rtx with RTX_INTEGRATED_P clear.
526 Args are like gen_rtx, but without the mode:
528 rtx attr_rtx (code, [element1, ..., elementn]) */
530 /*VARARGS1*/
531 static rtx
532 attr_rtx VPROTO((enum rtx_code code, ...))
534 #ifndef __STDC__
535 enum rtx_code code;
536 #endif
537 va_list p;
538 register int i; /* Array indices... */
539 register char *fmt; /* Current rtx's format... */
540 register rtx rt_val; /* RTX to return to caller... */
541 int hashcode;
542 register struct attr_hash *h;
543 struct obstack *old_obstack = rtl_obstack;
545 VA_START (p, code);
547 #ifndef __STDC__
548 code = va_arg (p, enum rtx_code);
549 #endif
551 /* For each of several cases, search the hash table for an existing entry.
552 Use that entry if one is found; otherwise create a new RTL and add it
553 to the table. */
555 if (GET_RTX_CLASS (code) == '1')
557 rtx arg0 = va_arg (p, rtx);
559 /* A permanent object cannot point to impermanent ones. */
560 if (! RTX_INTEGRATED_P (arg0))
562 rt_val = rtx_alloc (code);
563 XEXP (rt_val, 0) = arg0;
564 va_end (p);
565 return rt_val;
568 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
569 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
570 if (h->hashcode == hashcode
571 && GET_CODE (h->u.rtl) == code
572 && XEXP (h->u.rtl, 0) == arg0)
573 goto found;
575 if (h == 0)
577 rtl_obstack = hash_obstack;
578 rt_val = rtx_alloc (code);
579 XEXP (rt_val, 0) = arg0;
582 else if (GET_RTX_CLASS (code) == 'c'
583 || GET_RTX_CLASS (code) == '2'
584 || GET_RTX_CLASS (code) == '<')
586 rtx arg0 = va_arg (p, rtx);
587 rtx arg1 = va_arg (p, rtx);
589 /* A permanent object cannot point to impermanent ones. */
590 if (! RTX_INTEGRATED_P (arg0) || ! RTX_INTEGRATED_P (arg1))
592 rt_val = rtx_alloc (code);
593 XEXP (rt_val, 0) = arg0;
594 XEXP (rt_val, 1) = arg1;
595 va_end (p);
596 return rt_val;
599 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
600 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
601 if (h->hashcode == hashcode
602 && GET_CODE (h->u.rtl) == code
603 && XEXP (h->u.rtl, 0) == arg0
604 && XEXP (h->u.rtl, 1) == arg1)
605 goto found;
607 if (h == 0)
609 rtl_obstack = hash_obstack;
610 rt_val = rtx_alloc (code);
611 XEXP (rt_val, 0) = arg0;
612 XEXP (rt_val, 1) = arg1;
615 else if (GET_RTX_LENGTH (code) == 1
616 && GET_RTX_FORMAT (code)[0] == 's')
618 char * arg0 = va_arg (p, char *);
620 if (code == SYMBOL_REF)
621 arg0 = attr_string (arg0, strlen (arg0));
623 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
624 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
625 if (h->hashcode == hashcode
626 && GET_CODE (h->u.rtl) == code
627 && XSTR (h->u.rtl, 0) == arg0)
628 goto found;
630 if (h == 0)
632 rtl_obstack = hash_obstack;
633 rt_val = rtx_alloc (code);
634 XSTR (rt_val, 0) = arg0;
637 else if (GET_RTX_LENGTH (code) == 2
638 && GET_RTX_FORMAT (code)[0] == 's'
639 && GET_RTX_FORMAT (code)[1] == 's')
641 char *arg0 = va_arg (p, char *);
642 char *arg1 = va_arg (p, char *);
644 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
645 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
646 if (h->hashcode == hashcode
647 && GET_CODE (h->u.rtl) == code
648 && XSTR (h->u.rtl, 0) == arg0
649 && XSTR (h->u.rtl, 1) == arg1)
650 goto found;
652 if (h == 0)
654 rtl_obstack = hash_obstack;
655 rt_val = rtx_alloc (code);
656 XSTR (rt_val, 0) = arg0;
657 XSTR (rt_val, 1) = arg1;
660 else if (code == CONST_INT)
662 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
663 if (arg0 == 0)
664 return false_rtx;
665 if (arg0 == 1)
666 return true_rtx;
667 goto nohash;
669 else
671 nohash:
672 rt_val = rtx_alloc (code); /* Allocate the storage space. */
674 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
675 for (i = 0; i < GET_RTX_LENGTH (code); i++)
677 switch (*fmt++)
679 case '0': /* Unused field. */
680 break;
682 case 'i': /* An integer? */
683 XINT (rt_val, i) = va_arg (p, int);
684 break;
686 case 'w': /* A wide integer? */
687 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
688 break;
690 case 's': /* A string? */
691 XSTR (rt_val, i) = va_arg (p, char *);
692 break;
694 case 'e': /* An expression? */
695 case 'u': /* An insn? Same except when printing. */
696 XEXP (rt_val, i) = va_arg (p, rtx);
697 break;
699 case 'E': /* An RTX vector? */
700 XVEC (rt_val, i) = va_arg (p, rtvec);
701 break;
703 default:
704 abort();
707 va_end (p);
708 return rt_val;
711 rtl_obstack = old_obstack;
712 va_end (p);
713 attr_hash_add_rtx (hashcode, rt_val);
714 RTX_INTEGRATED_P (rt_val) = 1;
715 return rt_val;
717 found:
718 va_end (p);
719 return h->u.rtl;
722 /* Create a new string printed with the printf line arguments into a space
723 of at most LEN bytes:
725 rtx attr_printf (len, format, [arg1, ..., argn]) */
727 #ifdef HAVE_VPRINTF
729 /*VARARGS2*/
730 static char *
731 attr_printf VPROTO((register int len, char *fmt, ...))
733 #ifndef __STDC__
734 register int len;
735 char *fmt;
736 #endif
737 va_list p;
738 register char *str;
740 VA_START (p, fmt);
742 #ifndef __STDC__
743 len = va_arg (p, int);
744 fmt = va_arg (p, char *);
745 #endif
747 /* Print the string into a temporary location. */
748 str = (char *) alloca (len);
749 vsprintf (str, fmt, p);
750 va_end (p);
752 return attr_string (str, strlen (str));
755 #else /* not HAVE_VPRINTF */
757 static char *
758 attr_printf (len, fmt, arg1, arg2, arg3)
759 int len;
760 char *fmt;
761 char *arg1, *arg2, *arg3; /* also int */
763 register char *str;
765 /* Print the string into a temporary location. */
766 str = (char *) alloca (len);
767 sprintf (str, fmt, arg1, arg2, arg3);
769 return attr_string (str, strlen (str));
771 #endif /* not HAVE_VPRINTF */
774 attr_eq (name, value)
775 char *name, *value;
777 return attr_rtx (EQ_ATTR, attr_string (name, strlen (name)),
778 attr_string (value, strlen (value)));
781 char *
782 attr_numeral (n)
783 int n;
785 return XSTR (make_numeric_value (n), 0);
788 /* Return a permanent (possibly shared) copy of a string STR (not assumed
789 to be null terminated) with LEN bytes. */
791 static char *
792 attr_string (str, len)
793 char *str;
794 int len;
796 register struct attr_hash *h;
797 int hashcode;
798 int i;
799 register char *new_str;
801 /* Compute the hash code. */
802 hashcode = (len + 1) * 613 + (unsigned)str[0];
803 for (i = 1; i <= len; i += 2)
804 hashcode = ((hashcode * 613) + (unsigned)str[i]);
805 if (hashcode < 0)
806 hashcode = -hashcode;
808 /* Search the table for the string. */
809 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
810 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
811 && !strncmp (h->u.str, str, len))
812 return h->u.str; /* <-- return if found. */
814 /* Not found; create a permanent copy and add it to the hash table. */
815 new_str = (char *) obstack_alloc (hash_obstack, len + 1);
816 bcopy (str, new_str, len);
817 new_str[len] = '\0';
818 attr_hash_add_string (hashcode, new_str);
820 return new_str; /* Return the new string. */
823 /* Check two rtx's for equality of contents,
824 taking advantage of the fact that if both are hashed
825 then they can't be equal unless they are the same object. */
828 attr_equal_p (x, y)
829 rtx x, y;
831 return (x == y || (! (RTX_INTEGRATED_P (x) && RTX_INTEGRATED_P (y))
832 && rtx_equal_p (x, y)));
835 /* Copy an attribute value expression,
836 descending to all depths, but not copying any
837 permanent hashed subexpressions. */
840 attr_copy_rtx (orig)
841 register rtx orig;
843 register rtx copy;
844 register int i, j;
845 register RTX_CODE code;
846 register char *format_ptr;
848 /* No need to copy a permanent object. */
849 if (RTX_INTEGRATED_P (orig))
850 return orig;
852 code = GET_CODE (orig);
854 switch (code)
856 case REG:
857 case QUEUED:
858 case CONST_INT:
859 case CONST_DOUBLE:
860 case SYMBOL_REF:
861 case CODE_LABEL:
862 case PC:
863 case CC0:
864 return orig;
867 copy = rtx_alloc (code);
868 PUT_MODE (copy, GET_MODE (orig));
869 copy->in_struct = orig->in_struct;
870 copy->volatil = orig->volatil;
871 copy->unchanging = orig->unchanging;
872 copy->integrated = orig->integrated;
874 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
876 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
878 switch (*format_ptr++)
880 case 'e':
881 XEXP (copy, i) = XEXP (orig, i);
882 if (XEXP (orig, i) != NULL)
883 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
884 break;
886 case 'E':
887 case 'V':
888 XVEC (copy, i) = XVEC (orig, i);
889 if (XVEC (orig, i) != NULL)
891 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
892 for (j = 0; j < XVECLEN (copy, i); j++)
893 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
895 break;
897 case 'n':
898 case 'i':
899 XINT (copy, i) = XINT (orig, i);
900 break;
902 case 'w':
903 XWINT (copy, i) = XWINT (orig, i);
904 break;
906 case 's':
907 case 'S':
908 XSTR (copy, i) = XSTR (orig, i);
909 break;
911 default:
912 abort ();
915 return copy;
918 /* Given a test expression for an attribute, ensure it is validly formed.
919 IS_CONST indicates whether the expression is constant for each compiler
920 run (a constant expression may not test any particular insn).
922 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
923 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
924 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
926 Update the string address in EQ_ATTR expression to be the same used
927 in the attribute (or `alternative_name') to speed up subsequent
928 `find_attr' calls and eliminate most `strcmp' calls.
930 Return the new expression, if any. */
932 static rtx
933 check_attr_test (exp, is_const)
934 rtx exp;
935 int is_const;
937 struct attr_desc *attr;
938 struct attr_value *av;
939 char *name_ptr, *p;
940 rtx orexp, newexp;
942 switch (GET_CODE (exp))
944 case EQ_ATTR:
945 /* Handle negation test. */
946 if (XSTR (exp, 1)[0] == '!')
947 return check_attr_test (attr_rtx (NOT,
948 attr_eq (XSTR (exp, 0),
949 &XSTR (exp, 1)[1])),
950 is_const);
952 else if (n_comma_elts (XSTR (exp, 1)) == 1)
954 attr = find_attr (XSTR (exp, 0), 0);
955 if (attr == NULL)
957 if (! strcmp (XSTR (exp, 0), "alternative"))
959 XSTR (exp, 0) = alternative_name;
960 /* This can't be simplified any further. */
961 RTX_UNCHANGING_P (exp) = 1;
962 return exp;
964 else
965 fatal ("Unknown attribute `%s' in EQ_ATTR", XEXP (exp, 0));
968 if (is_const && ! attr->is_const)
969 fatal ("Constant expression uses insn attribute `%s' in EQ_ATTR",
970 XEXP (exp, 0));
972 /* Copy this just to make it permanent,
973 so expressions using it can be permanent too. */
974 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
976 /* It shouldn't be possible to simplify the value given to a
977 constant attribute, so don't expand this until it's time to
978 write the test expression. */
979 if (attr->is_const)
980 RTX_UNCHANGING_P (exp) = 1;
982 if (attr->is_numeric)
984 for (p = XSTR (exp, 1); *p; p++)
985 if (*p < '0' || *p > '9')
986 fatal ("Attribute `%s' takes only numeric values",
987 XEXP (exp, 0));
989 else
991 for (av = attr->first_value; av; av = av->next)
992 if (GET_CODE (av->value) == CONST_STRING
993 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
994 break;
996 if (av == NULL)
997 fatal ("Unknown value `%s' for `%s' attribute",
998 XEXP (exp, 1), XEXP (exp, 0));
1001 else
1003 /* Make an IOR tree of the possible values. */
1004 orexp = false_rtx;
1005 name_ptr = XSTR (exp, 1);
1006 while ((p = next_comma_elt (&name_ptr)) != NULL)
1008 newexp = attr_eq (XSTR (exp, 0), p);
1009 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
1012 return check_attr_test (orexp, is_const);
1014 break;
1016 case ATTR_FLAG:
1017 break;
1019 case CONST_INT:
1020 /* Either TRUE or FALSE. */
1021 if (XWINT (exp, 0))
1022 return true_rtx;
1023 else
1024 return false_rtx;
1026 case IOR:
1027 case AND:
1028 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const);
1029 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const);
1030 break;
1032 case NOT:
1033 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const);
1034 break;
1036 case MATCH_OPERAND:
1037 if (is_const)
1038 fatal ("RTL operator \"%s\" not valid in constant attribute test",
1039 GET_RTX_NAME (MATCH_OPERAND));
1040 /* These cases can't be simplified. */
1041 RTX_UNCHANGING_P (exp) = 1;
1042 break;
1044 case LE: case LT: case GT: case GE:
1045 case LEU: case LTU: case GTU: case GEU:
1046 case NE: case EQ:
1047 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
1048 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
1049 exp = attr_rtx (GET_CODE (exp),
1050 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
1051 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
1052 /* These cases can't be simplified. */
1053 RTX_UNCHANGING_P (exp) = 1;
1054 break;
1056 case SYMBOL_REF:
1057 if (is_const)
1059 /* These cases are valid for constant attributes, but can't be
1060 simplified. */
1061 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1062 RTX_UNCHANGING_P (exp) = 1;
1063 break;
1065 default:
1066 fatal ("RTL operator \"%s\" not valid in attribute test",
1067 GET_RTX_NAME (GET_CODE (exp)));
1070 return exp;
1073 /* Given an expression, ensure that it is validly formed and that all named
1074 attribute values are valid for the given attribute. Issue a fatal error
1075 if not. If no attribute is specified, assume a numeric attribute.
1077 Return a perhaps modified replacement expression for the value. */
1079 static rtx
1080 check_attr_value (exp, attr)
1081 rtx exp;
1082 struct attr_desc *attr;
1084 struct attr_value *av;
1085 char *p;
1086 int i;
1088 switch (GET_CODE (exp))
1090 case CONST_INT:
1091 if (attr && ! attr->is_numeric)
1092 fatal ("CONST_INT not valid for non-numeric `%s' attribute",
1093 attr->name);
1095 if (INTVAL (exp) < 0)
1096 fatal ("Negative numeric value specified for `%s' attribute",
1097 attr->name);
1099 break;
1101 case CONST_STRING:
1102 if (! strcmp (XSTR (exp, 0), "*"))
1103 break;
1105 if (attr == 0 || attr->is_numeric)
1107 p = XSTR (exp, 0);
1108 if (attr && attr->negative_ok && *p == '-')
1109 p++;
1110 for (; *p; p++)
1111 if (*p > '9' || *p < '0')
1112 fatal ("Non-numeric value for numeric `%s' attribute",
1113 attr ? attr->name : "internal");
1114 break;
1117 for (av = attr->first_value; av; av = av->next)
1118 if (GET_CODE (av->value) == CONST_STRING
1119 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
1120 break;
1122 if (av == NULL)
1123 fatal ("Unknown value `%s' for `%s' attribute",
1124 XSTR (exp, 0), attr ? attr->name : "internal");
1126 break;
1128 case IF_THEN_ELSE:
1129 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
1130 attr ? attr->is_const : 0);
1131 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1132 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
1133 break;
1135 case COND:
1136 if (XVECLEN (exp, 0) % 2 != 0)
1137 fatal ("First operand of COND must have even length");
1139 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1141 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
1142 attr ? attr->is_const : 0);
1143 XVECEXP (exp, 0, i + 1)
1144 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
1147 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1148 break;
1150 case SYMBOL_REF:
1151 if (attr && attr->is_const)
1152 /* A constant SYMBOL_REF is valid as a constant attribute test and
1153 is expanded later by make_canonical into a COND. */
1154 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1155 /* Otherwise, fall through... */
1157 default:
1158 fatal ("Invalid operation `%s' for attribute value",
1159 GET_RTX_NAME (GET_CODE (exp)));
1162 return exp;
1165 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1166 It becomes a COND with each test being (eq_attr "alternative "n") */
1168 static rtx
1169 convert_set_attr_alternative (exp, num_alt, insn_code, insn_index)
1170 rtx exp;
1171 int num_alt;
1172 int insn_code, insn_index;
1174 rtx condexp;
1175 int i;
1177 if (XVECLEN (exp, 1) != num_alt)
1178 fatal ("Bad number of entries in SET_ATTR_ALTERNATIVE for insn %d",
1179 insn_index);
1181 /* Make a COND with all tests but the last. Select the last value via the
1182 default. */
1183 condexp = rtx_alloc (COND);
1184 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1186 for (i = 0; i < num_alt - 1; i++)
1188 char *p;
1189 p = attr_numeral (i);
1191 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1192 #if 0
1193 /* Sharing this EQ_ATTR rtl causes trouble. */
1194 XVECEXP (condexp, 0, 2 * i) = rtx_alloc (EQ_ATTR);
1195 XSTR (XVECEXP (condexp, 0, 2 * i), 0) = alternative_name;
1196 XSTR (XVECEXP (condexp, 0, 2 * i), 1) = p;
1197 #endif
1198 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1201 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1203 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1206 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1207 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1209 static rtx
1210 convert_set_attr (exp, num_alt, insn_code, insn_index)
1211 rtx exp;
1212 int num_alt;
1213 int insn_code, insn_index;
1215 rtx newexp;
1216 char *name_ptr;
1217 char *p;
1218 int n;
1220 /* See how many alternative specified. */
1221 n = n_comma_elts (XSTR (exp, 1));
1222 if (n == 1)
1223 return attr_rtx (SET,
1224 attr_rtx (ATTR, XSTR (exp, 0)),
1225 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1227 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1228 XSTR (newexp, 0) = XSTR (exp, 0);
1229 XVEC (newexp, 1) = rtvec_alloc (n);
1231 /* Process each comma-separated name. */
1232 name_ptr = XSTR (exp, 1);
1233 n = 0;
1234 while ((p = next_comma_elt (&name_ptr)) != NULL)
1235 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1237 return convert_set_attr_alternative (newexp, num_alt, insn_code, insn_index);
1240 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1241 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1242 expressions. */
1244 static void
1245 check_defs ()
1247 struct insn_def *id;
1248 struct attr_desc *attr;
1249 int i;
1250 rtx value;
1252 for (id = defs; id; id = id->next)
1254 if (XVEC (id->def, id->vec_idx) == NULL)
1255 continue;
1257 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1259 value = XVECEXP (id->def, id->vec_idx, i);
1260 switch (GET_CODE (value))
1262 case SET:
1263 if (GET_CODE (XEXP (value, 0)) != ATTR)
1264 fatal ("Bad attribute set in pattern %d", id->insn_index);
1265 break;
1267 case SET_ATTR_ALTERNATIVE:
1268 value = convert_set_attr_alternative (value,
1269 id->num_alternatives,
1270 id->insn_code,
1271 id->insn_index);
1272 break;
1274 case SET_ATTR:
1275 value = convert_set_attr (value, id->num_alternatives,
1276 id->insn_code, id->insn_index);
1277 break;
1279 default:
1280 fatal ("Invalid attribute code `%s' for pattern %d",
1281 GET_RTX_NAME (GET_CODE (value)), id->insn_index);
1284 if ((attr = find_attr (XSTR (XEXP (value, 0), 0), 0)) == NULL)
1285 fatal ("Unknown attribute `%s' for pattern number %d",
1286 XSTR (XEXP (value, 0), 0), id->insn_index);
1288 XVECEXP (id->def, id->vec_idx, i) = value;
1289 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1294 /* Given a constant SYMBOL_REF expression, convert to a COND that
1295 explicitly tests each enumerated value. */
1297 static rtx
1298 convert_const_symbol_ref (exp, attr)
1299 rtx exp;
1300 struct attr_desc *attr;
1302 rtx condexp;
1303 struct attr_value *av;
1304 int i;
1305 int num_alt = 0;
1307 for (av = attr->first_value; av; av = av->next)
1308 num_alt++;
1310 /* Make a COND with all tests but the last, and in the original order.
1311 Select the last value via the default. Note that the attr values
1312 are constructed in reverse order. */
1314 condexp = rtx_alloc (COND);
1315 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1316 av = attr->first_value;
1317 XEXP (condexp, 1) = av->value;
1319 for (i = num_alt - 2; av = av->next, i >= 0; i--)
1321 char *p, *string;
1322 rtx value;
1324 string = p = (char *) oballoc (2
1325 + strlen (attr->name)
1326 + strlen (XSTR (av->value, 0)));
1327 strcpy (p, attr->name);
1328 strcat (p, "_");
1329 strcat (p, XSTR (av->value, 0));
1330 for (; *p != '\0'; p++)
1331 if (*p >= 'a' && *p <= 'z')
1332 *p -= 'a' - 'A';
1334 value = attr_rtx (SYMBOL_REF, string);
1335 RTX_UNCHANGING_P (value) = 1;
1337 XVECEXP (condexp, 0, 2 * i) = attr_rtx (EQ, exp, value);
1339 XVECEXP (condexp, 0, 2 * i + 1) = av->value;
1342 return condexp;
1345 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1346 expressions by converting them into a COND. This removes cases from this
1347 program. Also, replace an attribute value of "*" with the default attribute
1348 value. */
1350 static rtx
1351 make_canonical (attr, exp)
1352 struct attr_desc *attr;
1353 rtx exp;
1355 int i;
1356 rtx newexp;
1358 switch (GET_CODE (exp))
1360 case CONST_INT:
1361 exp = make_numeric_value (INTVAL (exp));
1362 break;
1364 case CONST_STRING:
1365 if (! strcmp (XSTR (exp, 0), "*"))
1367 if (attr == 0 || attr->default_val == 0)
1368 fatal ("(attr_value \"*\") used in invalid context.");
1369 exp = attr->default_val->value;
1372 break;
1374 case SYMBOL_REF:
1375 if (!attr->is_const || RTX_UNCHANGING_P (exp))
1376 break;
1377 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1378 This makes the COND something that won't be considered an arbitrary
1379 expression by walk_attr_value. */
1380 RTX_UNCHANGING_P (exp) = 1;
1381 exp = convert_const_symbol_ref (exp, attr);
1382 RTX_UNCHANGING_P (exp) = 1;
1383 exp = check_attr_value (exp, attr);
1384 /* Goto COND case since this is now a COND. Note that while the
1385 new expression is rescanned, all symbol_ref notes are mared as
1386 unchanging. */
1387 goto cond;
1389 case IF_THEN_ELSE:
1390 newexp = rtx_alloc (COND);
1391 XVEC (newexp, 0) = rtvec_alloc (2);
1392 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1393 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1395 XEXP (newexp, 1) = XEXP (exp, 2);
1397 exp = newexp;
1398 /* Fall through to COND case since this is now a COND. */
1400 case COND:
1401 cond:
1403 int allsame = 1;
1404 rtx defval;
1406 /* First, check for degenerate COND. */
1407 if (XVECLEN (exp, 0) == 0)
1408 return make_canonical (attr, XEXP (exp, 1));
1409 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1411 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1413 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1414 XVECEXP (exp, 0, i + 1)
1415 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1416 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1417 allsame = 0;
1419 if (allsame)
1420 return defval;
1421 break;
1425 return exp;
1428 static rtx
1429 copy_boolean (exp)
1430 rtx exp;
1432 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1433 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1434 copy_boolean (XEXP (exp, 1)));
1435 return exp;
1438 /* Given a value and an attribute description, return a `struct attr_value *'
1439 that represents that value. This is either an existing structure, if the
1440 value has been previously encountered, or a newly-created structure.
1442 `insn_code' is the code of an insn whose attribute has the specified
1443 value (-2 if not processing an insn). We ensure that all insns for
1444 a given value have the same number of alternatives if the value checks
1445 alternatives. */
1447 static struct attr_value *
1448 get_attr_value (value, attr, insn_code)
1449 rtx value;
1450 struct attr_desc *attr;
1451 int insn_code;
1453 struct attr_value *av;
1454 int num_alt = 0;
1456 value = make_canonical (attr, value);
1457 if (compares_alternatives_p (value))
1459 if (insn_code < 0 || insn_alternatives == NULL)
1460 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1461 else
1462 num_alt = insn_alternatives[insn_code];
1465 for (av = attr->first_value; av; av = av->next)
1466 if (rtx_equal_p (value, av->value)
1467 && (num_alt == 0 || av->first_insn == NULL
1468 || insn_alternatives[av->first_insn->insn_code]))
1469 return av;
1471 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
1472 av->value = value;
1473 av->next = attr->first_value;
1474 attr->first_value = av;
1475 av->first_insn = NULL;
1476 av->num_insns = 0;
1477 av->has_asm_insn = 0;
1479 return av;
1482 /* After all DEFINE_DELAYs have been read in, create internal attributes
1483 to generate the required routines.
1485 First, we compute the number of delay slots for each insn (as a COND of
1486 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1487 delay type is specified, we compute a similar function giving the
1488 DEFINE_DELAY ordinal for each insn.
1490 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1491 tells whether a given insn can be in that delay slot.
1493 Normal attribute filling and optimization expands these to contain the
1494 information needed to handle delay slots. */
1496 static void
1497 expand_delays ()
1499 struct delay_desc *delay;
1500 rtx condexp;
1501 rtx newexp;
1502 int i;
1503 char *p;
1505 /* First, generate data for `num_delay_slots' function. */
1507 condexp = rtx_alloc (COND);
1508 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1509 XEXP (condexp, 1) = make_numeric_value (0);
1511 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1513 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1514 XVECEXP (condexp, 0, i + 1)
1515 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1518 make_internal_attr ("*num_delay_slots", condexp, 0);
1520 /* If more than one delay type, do the same for computing the delay type. */
1521 if (num_delays > 1)
1523 condexp = rtx_alloc (COND);
1524 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1525 XEXP (condexp, 1) = make_numeric_value (0);
1527 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1529 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1530 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1533 make_internal_attr ("*delay_type", condexp, 1);
1536 /* For each delay possibility and delay slot, compute an eligibility
1537 attribute for non-annulled insns and for each type of annulled (annul
1538 if true and annul if false). */
1539 for (delay = delays; delay; delay = delay->next)
1541 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1543 condexp = XVECEXP (delay->def, 1, i);
1544 if (condexp == 0) condexp = false_rtx;
1545 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1546 make_numeric_value (1), make_numeric_value (0));
1548 p = attr_printf (sizeof ("*delay__") + MAX_DIGITS*2, "*delay_%d_%d",
1549 delay->num, i / 3);
1550 make_internal_attr (p, newexp, 1);
1552 if (have_annul_true)
1554 condexp = XVECEXP (delay->def, 1, i + 1);
1555 if (condexp == 0) condexp = false_rtx;
1556 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1557 make_numeric_value (1),
1558 make_numeric_value (0));
1559 p = attr_printf (sizeof ("*annul_true__") + MAX_DIGITS*2,
1560 "*annul_true_%d_%d", delay->num, i / 3);
1561 make_internal_attr (p, newexp, 1);
1564 if (have_annul_false)
1566 condexp = XVECEXP (delay->def, 1, i + 2);
1567 if (condexp == 0) condexp = false_rtx;
1568 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1569 make_numeric_value (1),
1570 make_numeric_value (0));
1571 p = attr_printf (sizeof ("*annul_false__") + MAX_DIGITS*2,
1572 "*annul_false_%d_%d", delay->num, i / 3);
1573 make_internal_attr (p, newexp, 1);
1579 /* This function is given a left and right side expression and an operator.
1580 Each side is a conditional expression, each alternative of which has a
1581 numerical value. The function returns another conditional expression
1582 which, for every possible set of condition values, returns a value that is
1583 the operator applied to the values of the two sides.
1585 Since this is called early, it must also support IF_THEN_ELSE. */
1587 static rtx
1588 operate_exp (op, left, right)
1589 enum operator op;
1590 rtx left, right;
1592 int left_value, right_value;
1593 rtx newexp;
1594 int i;
1596 /* If left is a string, apply operator to it and the right side. */
1597 if (GET_CODE (left) == CONST_STRING)
1599 /* If right is also a string, just perform the operation. */
1600 if (GET_CODE (right) == CONST_STRING)
1602 left_value = atoi (XSTR (left, 0));
1603 right_value = atoi (XSTR (right, 0));
1604 switch (op)
1606 case PLUS_OP:
1607 i = left_value + right_value;
1608 break;
1610 case MINUS_OP:
1611 i = left_value - right_value;
1612 break;
1614 case POS_MINUS_OP: /* The positive part of LEFT - RIGHT. */
1615 if (left_value > right_value)
1616 i = left_value - right_value;
1617 else
1618 i = 0;
1619 break;
1621 case OR_OP:
1622 i = left_value | right_value;
1623 break;
1625 case EQ_OP:
1626 i = left_value == right_value;
1627 break;
1629 case RANGE_OP:
1630 i = (left_value << (HOST_BITS_PER_INT / 2)) | right_value;
1631 break;
1633 case MAX_OP:
1634 if (left_value > right_value)
1635 i = left_value;
1636 else
1637 i = right_value;
1638 break;
1640 case MIN_OP:
1641 if (left_value < right_value)
1642 i = left_value;
1643 else
1644 i = right_value;
1645 break;
1647 default:
1648 abort ();
1651 return make_numeric_value (i);
1653 else if (GET_CODE (right) == IF_THEN_ELSE)
1655 /* Apply recursively to all values within. */
1656 rtx newleft = operate_exp (op, left, XEXP (right, 1));
1657 rtx newright = operate_exp (op, left, XEXP (right, 2));
1658 if (rtx_equal_p (newleft, newright))
1659 return newleft;
1660 return attr_rtx (IF_THEN_ELSE, XEXP (right, 0), newleft, newright);
1662 else if (GET_CODE (right) == COND)
1664 int allsame = 1;
1665 rtx defval;
1667 newexp = rtx_alloc (COND);
1668 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (right, 0));
1669 defval = XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
1671 for (i = 0; i < XVECLEN (right, 0); i += 2)
1673 XVECEXP (newexp, 0, i) = XVECEXP (right, 0, i);
1674 XVECEXP (newexp, 0, i + 1)
1675 = operate_exp (op, left, XVECEXP (right, 0, i + 1));
1676 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1677 defval))
1678 allsame = 0;
1681 /* If the resulting cond is trivial (all alternatives
1682 give the same value), optimize it away. */
1683 if (allsame)
1685 obstack_free (rtl_obstack, newexp);
1686 return operate_exp (op, left, XEXP (right, 1));
1689 /* If the result is the same as the RIGHT operand,
1690 just use that. */
1691 if (rtx_equal_p (newexp, right))
1693 obstack_free (rtl_obstack, newexp);
1694 return right;
1697 return newexp;
1699 else
1700 fatal ("Badly formed attribute value");
1703 /* Otherwise, do recursion the other way. */
1704 else if (GET_CODE (left) == IF_THEN_ELSE)
1706 rtx newleft = operate_exp (op, XEXP (left, 1), right);
1707 rtx newright = operate_exp (op, XEXP (left, 2), right);
1708 if (rtx_equal_p (newleft, newright))
1709 return newleft;
1710 return attr_rtx (IF_THEN_ELSE, XEXP (left, 0), newleft, newright);
1712 else if (GET_CODE (left) == COND)
1714 int allsame = 1;
1715 rtx defval;
1717 newexp = rtx_alloc (COND);
1718 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (left, 0));
1719 defval = XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
1721 for (i = 0; i < XVECLEN (left, 0); i += 2)
1723 XVECEXP (newexp, 0, i) = XVECEXP (left, 0, i);
1724 XVECEXP (newexp, 0, i + 1)
1725 = operate_exp (op, XVECEXP (left, 0, i + 1), right);
1726 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1727 defval))
1728 allsame = 0;
1731 /* If the cond is trivial (all alternatives give the same value),
1732 optimize it away. */
1733 if (allsame)
1735 obstack_free (rtl_obstack, newexp);
1736 return operate_exp (op, XEXP (left, 1), right);
1739 /* If the result is the same as the LEFT operand,
1740 just use that. */
1741 if (rtx_equal_p (newexp, left))
1743 obstack_free (rtl_obstack, newexp);
1744 return left;
1747 return newexp;
1750 else
1751 fatal ("Badly formed attribute value.");
1752 /* NOTREACHED */
1753 return NULL;
1756 /* Once all attributes and DEFINE_FUNCTION_UNITs have been read, we
1757 construct a number of attributes.
1759 The first produces a function `function_units_used' which is given an
1760 insn and produces an encoding showing which function units are required
1761 for the execution of that insn. If the value is non-negative, the insn
1762 uses that unit; otherwise, the value is a one's compliment mask of units
1763 used.
1765 The second produces a function `result_ready_cost' which is used to
1766 determine the time that the result of an insn will be ready and hence
1767 a worst-case schedule.
1769 Both of these produce quite complex expressions which are then set as the
1770 default value of internal attributes. Normal attribute simplification
1771 should produce reasonable expressions.
1773 For each unit, a `<name>_unit_ready_cost' function will take an
1774 insn and give the delay until that unit will be ready with the result
1775 and a `<name>_unit_conflict_cost' function is given an insn already
1776 executing on the unit and a candidate to execute and will give the
1777 cost from the time the executing insn started until the candidate
1778 can start (ignore limitations on the number of simultaneous insns).
1780 For each unit, a `<name>_unit_blockage' function is given an insn
1781 already executing on the unit and a candidate to execute and will
1782 give the delay incurred due to function unit conflicts. The range of
1783 blockage cost values for a given executing insn is given by the
1784 `<name>_unit_blockage_range' function. These values are encoded in
1785 an int where the upper half gives the minimum value and the lower
1786 half gives the maximum value. */
1788 static void
1789 expand_units ()
1791 struct function_unit *unit, **unit_num;
1792 struct function_unit_op *op, **op_array, ***unit_ops;
1793 rtx unitsmask;
1794 rtx readycost;
1795 rtx newexp;
1796 char *str;
1797 int i, j, u, num, nvalues;
1799 /* Rebuild the condition for the unit to share the RTL expressions.
1800 Sharing is required by simplify_by_exploding. Build the issue delay
1801 expressions. Validate the expressions we were given for the conditions
1802 and conflict vector. Then make attributes for use in the conflict
1803 function. */
1805 for (unit = units; unit; unit = unit->next)
1807 unit->condexp = check_attr_test (unit->condexp, 0);
1809 for (op = unit->ops; op; op = op->next)
1811 rtx issue_delay = make_numeric_value (op->issue_delay);
1812 rtx issue_exp = issue_delay;
1814 /* Build, validate, and simplify the issue delay expression. */
1815 if (op->conflict_exp != true_rtx)
1816 issue_exp = attr_rtx (IF_THEN_ELSE, op->conflict_exp,
1817 issue_exp, make_numeric_value (0));
1818 issue_exp = check_attr_value (make_canonical (NULL_ATTR,
1819 issue_exp),
1820 NULL_ATTR);
1821 issue_exp = simplify_knowing (issue_exp, unit->condexp);
1822 op->issue_exp = issue_exp;
1824 /* Make an attribute for use in the conflict function if needed. */
1825 unit->needs_conflict_function = (unit->issue_delay.min
1826 != unit->issue_delay.max);
1827 if (unit->needs_conflict_function)
1829 str = attr_printf (strlen (unit->name) + sizeof ("*_cost_") + MAX_DIGITS,
1830 "*%s_cost_%d", unit->name, op->num);
1831 make_internal_attr (str, issue_exp, 1);
1834 /* Validate the condition. */
1835 op->condexp = check_attr_test (op->condexp, 0);
1839 /* Compute the mask of function units used. Initially, the unitsmask is
1840 zero. Set up a conditional to compute each unit's contribution. */
1841 unitsmask = make_numeric_value (0);
1842 newexp = rtx_alloc (IF_THEN_ELSE);
1843 XEXP (newexp, 2) = make_numeric_value (0);
1845 /* Merge each function unit into the unit mask attributes. */
1846 for (unit = units; unit; unit = unit->next)
1848 XEXP (newexp, 0) = unit->condexp;
1849 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1850 unitsmask = operate_exp (OR_OP, unitsmask, newexp);
1853 /* Simplify the unit mask expression, encode it, and make an attribute
1854 for the function_units_used function. */
1855 unitsmask = simplify_by_exploding (unitsmask);
1856 unitsmask = encode_units_mask (unitsmask);
1857 make_internal_attr ("*function_units_used", unitsmask, 2);
1859 /* Create an array of ops for each unit. Add an extra unit for the
1860 result_ready_cost function that has the ops of all other units. */
1861 unit_ops = (struct function_unit_op ***)
1862 alloca ((num_units + 1) * sizeof (struct function_unit_op **));
1863 unit_num = (struct function_unit **)
1864 alloca ((num_units + 1) * sizeof (struct function_unit *));
1866 unit_num[num_units] = unit = (struct function_unit *)
1867 alloca (sizeof (struct function_unit));
1868 unit->num = num_units;
1869 unit->num_opclasses = 0;
1871 for (unit = units; unit; unit = unit->next)
1873 unit_num[num_units]->num_opclasses += unit->num_opclasses;
1874 unit_num[unit->num] = unit;
1875 unit_ops[unit->num] = op_array = (struct function_unit_op **)
1876 alloca (unit->num_opclasses * sizeof (struct function_unit_op *));
1878 for (op = unit->ops; op; op = op->next)
1879 op_array[op->num] = op;
1882 /* Compose the array of ops for the extra unit. */
1883 unit_ops[num_units] = op_array = (struct function_unit_op **)
1884 alloca (unit_num[num_units]->num_opclasses
1885 * sizeof (struct function_unit_op *));
1887 for (unit = units, i = 0; unit; i += unit->num_opclasses, unit = unit->next)
1888 bcopy ((char *) unit_ops[unit->num], (char *) &op_array[i],
1889 unit->num_opclasses * sizeof (struct function_unit_op *));
1891 /* Compute the ready cost function for each unit by computing the
1892 condition for each non-default value. */
1893 for (u = 0; u <= num_units; u++)
1895 rtx orexp;
1896 int value;
1898 unit = unit_num[u];
1899 op_array = unit_ops[unit->num];
1900 num = unit->num_opclasses;
1902 /* Sort the array of ops into increasing ready cost order. */
1903 for (i = 0; i < num; i++)
1904 for (j = num - 1; j > i; j--)
1905 if (op_array[j-1]->ready < op_array[j]->ready)
1907 op = op_array[j];
1908 op_array[j] = op_array[j-1];
1909 op_array[j-1] = op;
1912 /* Determine how many distinct non-default ready cost values there
1913 are. We use a default ready cost value of 1. */
1914 nvalues = 0; value = 1;
1915 for (i = num - 1; i >= 0; i--)
1916 if (op_array[i]->ready > value)
1918 value = op_array[i]->ready;
1919 nvalues++;
1922 if (nvalues == 0)
1923 readycost = make_numeric_value (1);
1924 else
1926 /* Construct the ready cost expression as a COND of each value from
1927 the largest to the smallest. */
1928 readycost = rtx_alloc (COND);
1929 XVEC (readycost, 0) = rtvec_alloc (nvalues * 2);
1930 XEXP (readycost, 1) = make_numeric_value (1);
1932 nvalues = 0; orexp = false_rtx; value = op_array[0]->ready;
1933 for (i = 0; i < num; i++)
1935 op = op_array[i];
1936 if (op->ready <= 1)
1937 break;
1938 else if (op->ready == value)
1939 orexp = insert_right_side (IOR, orexp, op->condexp, -2, -2);
1940 else
1942 XVECEXP (readycost, 0, nvalues * 2) = orexp;
1943 XVECEXP (readycost, 0, nvalues * 2 + 1)
1944 = make_numeric_value (value);
1945 nvalues++;
1946 value = op->ready;
1947 orexp = op->condexp;
1950 XVECEXP (readycost, 0, nvalues * 2) = orexp;
1951 XVECEXP (readycost, 0, nvalues * 2 + 1) = make_numeric_value (value);
1954 if (u < num_units)
1956 rtx max_blockage = 0, min_blockage = 0;
1958 /* Simplify the readycost expression by only considering insns
1959 that use the unit. */
1960 readycost = simplify_knowing (readycost, unit->condexp);
1962 /* Determine the blockage cost the executing insn (E) given
1963 the candidate insn (C). This is the maximum of the issue
1964 delay, the pipeline delay, and the simultaneity constraint.
1965 Each function_unit_op represents the characteristics of the
1966 candidate insn, so in the expressions below, C is a known
1967 term and E is an unknown term.
1969 We compute the blockage cost for each E for every possible C.
1970 Thus OP represents E, and READYCOST is a list of values for
1971 every possible C.
1973 The issue delay function for C is op->issue_exp and is used to
1974 write the `<name>_unit_conflict_cost' function. Symbolicly
1975 this is "ISSUE-DELAY (E,C)".
1977 The pipeline delay results form the FIFO constraint on the
1978 function unit and is "READY-COST (E) + 1 - READY-COST (C)".
1980 The simultaneity constraint is based on how long it takes to
1981 fill the unit given the minimum issue delay. FILL-TIME is the
1982 constant "MIN (ISSUE-DELAY (*,*)) * (SIMULTANEITY - 1)", and
1983 the simultaneity constraint is "READY-COST (E) - FILL-TIME"
1984 if SIMULTANEITY is non-zero and zero otherwise.
1986 Thus, BLOCKAGE (E,C) when SIMULTANEITY is zero is
1988 MAX (ISSUE-DELAY (E,C),
1989 READY-COST (E) - (READY-COST (C) - 1))
1991 and otherwise
1993 MAX (ISSUE-DELAY (E,C),
1994 READY-COST (E) - (READY-COST (C) - 1),
1995 READY-COST (E) - FILL-TIME)
1997 The `<name>_unit_blockage' function is computed by determining
1998 this value for each candidate insn. As these values are
1999 computed, we also compute the upper and lower bounds for
2000 BLOCKAGE (E,*). These are combined to form the function
2001 `<name>_unit_blockage_range'. Finally, the maximum blockage
2002 cost, MAX (BLOCKAGE (*,*)), is computed. */
2004 for (op = unit->ops; op; op = op->next)
2006 #ifdef HAIFA
2007 rtx blockage = op->issue_exp;
2008 #else
2009 rtx blockage = operate_exp (POS_MINUS_OP, readycost,
2010 make_numeric_value (1));
2012 if (unit->simultaneity != 0)
2014 rtx filltime = make_numeric_value ((unit->simultaneity - 1)
2015 * unit->issue_delay.min);
2016 blockage = operate_exp (MIN_OP, blockage, filltime);
2019 blockage = operate_exp (POS_MINUS_OP,
2020 make_numeric_value (op->ready),
2021 blockage);
2023 blockage = operate_exp (MAX_OP, blockage, op->issue_exp);
2024 #endif
2025 blockage = simplify_knowing (blockage, unit->condexp);
2027 /* Add this op's contribution to MAX (BLOCKAGE (E,*)) and
2028 MIN (BLOCKAGE (E,*)). */
2029 if (max_blockage == 0)
2030 max_blockage = min_blockage = blockage;
2031 else
2033 max_blockage
2034 = simplify_knowing (operate_exp (MAX_OP, max_blockage,
2035 blockage),
2036 unit->condexp);
2037 min_blockage
2038 = simplify_knowing (operate_exp (MIN_OP, min_blockage,
2039 blockage),
2040 unit->condexp);
2043 /* Make an attribute for use in the blockage function. */
2044 str = attr_printf (strlen (unit->name) + sizeof ("*_block_") + MAX_DIGITS,
2045 "*%s_block_%d", unit->name, op->num);
2046 make_internal_attr (str, blockage, 1);
2049 /* Record MAX (BLOCKAGE (*,*)). */
2050 unit->max_blockage = max_attr_value (max_blockage);
2052 /* See if the upper and lower bounds of BLOCKAGE (E,*) are the
2053 same. If so, the blockage function carries no additional
2054 information and is not written. */
2055 newexp = operate_exp (EQ_OP, max_blockage, min_blockage);
2056 newexp = simplify_knowing (newexp, unit->condexp);
2057 unit->needs_blockage_function
2058 = (GET_CODE (newexp) != CONST_STRING
2059 || atoi (XSTR (newexp, 0)) != 1);
2061 /* If the all values of BLOCKAGE (E,C) have the same value,
2062 neither blockage function is written. */
2063 unit->needs_range_function
2064 = (unit->needs_blockage_function
2065 || GET_CODE (max_blockage) != CONST_STRING);
2067 if (unit->needs_range_function)
2069 /* Compute the blockage range function and make an attribute
2070 for writing it's value. */
2071 newexp = operate_exp (RANGE_OP, min_blockage, max_blockage);
2072 newexp = simplify_knowing (newexp, unit->condexp);
2074 str = attr_printf (strlen (unit->name) + sizeof ("*_unit_blockage_range"),
2075 "*%s_unit_blockage_range", unit->name);
2076 make_internal_attr (str, newexp, 4);
2079 str = attr_printf (strlen (unit->name) + sizeof ("*_unit_ready_cost"),
2080 "*%s_unit_ready_cost", unit->name);
2082 else
2083 str = "*result_ready_cost";
2085 /* Make an attribute for the ready_cost function. Simplifying
2086 further with simplify_by_exploding doesn't win. */
2087 make_internal_attr (str, readycost, 0);
2090 /* For each unit that requires a conflict cost function, make an attribute
2091 that maps insns to the operation number. */
2092 for (unit = units; unit; unit = unit->next)
2094 rtx caseexp;
2096 if (! unit->needs_conflict_function
2097 && ! unit->needs_blockage_function)
2098 continue;
2100 caseexp = rtx_alloc (COND);
2101 XVEC (caseexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
2103 for (op = unit->ops; op; op = op->next)
2105 /* Make our adjustment to the COND being computed. If we are the
2106 last operation class, place our values into the default of the
2107 COND. */
2108 if (op->num == unit->num_opclasses - 1)
2110 XEXP (caseexp, 1) = make_numeric_value (op->num);
2112 else
2114 XVECEXP (caseexp, 0, op->num * 2) = op->condexp;
2115 XVECEXP (caseexp, 0, op->num * 2 + 1)
2116 = make_numeric_value (op->num);
2120 /* Simplifying caseexp with simplify_by_exploding doesn't win. */
2121 str = attr_printf (strlen (unit->name) + sizeof ("*_cases"),
2122 "*%s_cases", unit->name);
2123 make_internal_attr (str, caseexp, 1);
2127 /* Simplify EXP given KNOWN_TRUE. */
2129 static rtx
2130 simplify_knowing (exp, known_true)
2131 rtx exp, known_true;
2133 if (GET_CODE (exp) != CONST_STRING)
2135 exp = attr_rtx (IF_THEN_ELSE, known_true, exp,
2136 make_numeric_value (max_attr_value (exp)));
2137 exp = simplify_by_exploding (exp);
2139 return exp;
2142 /* Translate the CONST_STRING expressions in X to change the encoding of
2143 value. On input, the value is a bitmask with a one bit for each unit
2144 used; on output, the value is the unit number (zero based) if one
2145 and only one unit is used or the one's compliment of the bitmask. */
2147 static rtx
2148 encode_units_mask (x)
2149 rtx x;
2151 register int i;
2152 register int j;
2153 register enum rtx_code code;
2154 register char *fmt;
2156 code = GET_CODE (x);
2158 switch (code)
2160 case CONST_STRING:
2161 i = atoi (XSTR (x, 0));
2162 if (i < 0)
2163 abort (); /* The sign bit encodes a one's compliment mask. */
2164 else if (i != 0 && i == (i & -i))
2165 /* Only one bit is set, so yield that unit number. */
2166 for (j = 0; (i >>= 1) != 0; j++)
2168 else
2169 j = ~i;
2170 return attr_rtx (CONST_STRING, attr_printf (MAX_DIGITS, "%d", j));
2172 case REG:
2173 case QUEUED:
2174 case CONST_INT:
2175 case CONST_DOUBLE:
2176 case SYMBOL_REF:
2177 case CODE_LABEL:
2178 case PC:
2179 case CC0:
2180 case EQ_ATTR:
2181 return x;
2184 /* Compare the elements. If any pair of corresponding elements
2185 fail to match, return 0 for the whole things. */
2187 fmt = GET_RTX_FORMAT (code);
2188 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2190 switch (fmt[i])
2192 case 'V':
2193 case 'E':
2194 for (j = 0; j < XVECLEN (x, i); j++)
2195 XVECEXP (x, i, j) = encode_units_mask (XVECEXP (x, i, j));
2196 break;
2198 case 'e':
2199 XEXP (x, i) = encode_units_mask (XEXP (x, i));
2200 break;
2203 return x;
2206 /* Once all attributes and insns have been read and checked, we construct for
2207 each attribute value a list of all the insns that have that value for
2208 the attribute. */
2210 static void
2211 fill_attr (attr)
2212 struct attr_desc *attr;
2214 struct attr_value *av;
2215 struct insn_ent *ie;
2216 struct insn_def *id;
2217 int i;
2218 rtx value;
2220 /* Don't fill constant attributes. The value is independent of
2221 any particular insn. */
2222 if (attr->is_const)
2223 return;
2225 for (id = defs; id; id = id->next)
2227 /* If no value is specified for this insn for this attribute, use the
2228 default. */
2229 value = NULL;
2230 if (XVEC (id->def, id->vec_idx))
2231 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
2232 if (! strcmp (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
2233 attr->name))
2234 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
2236 if (value == NULL)
2237 av = attr->default_val;
2238 else
2239 av = get_attr_value (value, attr, id->insn_code);
2241 ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
2242 ie->insn_code = id->insn_code;
2243 ie->insn_index = id->insn_code;
2244 insert_insn_ent (av, ie);
2248 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
2249 test that checks relative positions of insns (uses MATCH_DUP or PC).
2250 If so, replace it with what is obtained by passing the expression to
2251 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
2252 recursively on each value (including the default value). Otherwise,
2253 return the value returned by NO_ADDRESS_FN applied to EXP. */
2255 static rtx
2256 substitute_address (exp, no_address_fn, address_fn)
2257 rtx exp;
2258 rtx (*no_address_fn) ();
2259 rtx (*address_fn) ();
2261 int i;
2262 rtx newexp;
2264 if (GET_CODE (exp) == COND)
2266 /* See if any tests use addresses. */
2267 address_used = 0;
2268 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2269 walk_attr_value (XVECEXP (exp, 0, i));
2271 if (address_used)
2272 return (*address_fn) (exp);
2274 /* Make a new copy of this COND, replacing each element. */
2275 newexp = rtx_alloc (COND);
2276 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
2277 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2279 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
2280 XVECEXP (newexp, 0, i + 1)
2281 = substitute_address (XVECEXP (exp, 0, i + 1),
2282 no_address_fn, address_fn);
2285 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
2286 no_address_fn, address_fn);
2288 return newexp;
2291 else if (GET_CODE (exp) == IF_THEN_ELSE)
2293 address_used = 0;
2294 walk_attr_value (XEXP (exp, 0));
2295 if (address_used)
2296 return (*address_fn) (exp);
2298 return attr_rtx (IF_THEN_ELSE,
2299 substitute_address (XEXP (exp, 0),
2300 no_address_fn, address_fn),
2301 substitute_address (XEXP (exp, 1),
2302 no_address_fn, address_fn),
2303 substitute_address (XEXP (exp, 2),
2304 no_address_fn, address_fn));
2307 return (*no_address_fn) (exp);
2310 /* Make new attributes from the `length' attribute. The following are made,
2311 each corresponding to a function called from `shorten_branches' or
2312 `get_attr_length':
2314 *insn_default_length This is the length of the insn to be returned
2315 by `get_attr_length' before `shorten_branches'
2316 has been called. In each case where the length
2317 depends on relative addresses, the largest
2318 possible is used. This routine is also used
2319 to compute the initial size of the insn.
2321 *insn_variable_length_p This returns 1 if the insn's length depends
2322 on relative addresses, zero otherwise.
2324 *insn_current_length This is only called when it is known that the
2325 insn has a variable length and returns the
2326 current length, based on relative addresses.
2329 static void
2330 make_length_attrs ()
2332 static char *new_names[] = {"*insn_default_length",
2333 "*insn_variable_length_p",
2334 "*insn_current_length"};
2335 static rtx (*no_address_fn[]) PROTO((rtx)) = {identity_fn, zero_fn, zero_fn};
2336 static rtx (*address_fn[]) PROTO((rtx)) = {max_fn, one_fn, identity_fn};
2337 int i;
2338 struct attr_desc *length_attr, *new_attr;
2339 struct attr_value *av, *new_av;
2340 struct insn_ent *ie, *new_ie;
2342 /* See if length attribute is defined. If so, it must be numeric. Make
2343 it special so we don't output anything for it. */
2344 length_attr = find_attr ("length", 0);
2345 if (length_attr == 0)
2346 return;
2348 if (! length_attr->is_numeric)
2349 fatal ("length attribute must be numeric.");
2351 length_attr->is_const = 0;
2352 length_attr->is_special = 1;
2354 /* Make each new attribute, in turn. */
2355 for (i = 0; i < sizeof new_names / sizeof new_names[0]; i++)
2357 make_internal_attr (new_names[i],
2358 substitute_address (length_attr->default_val->value,
2359 no_address_fn[i], address_fn[i]),
2361 new_attr = find_attr (new_names[i], 0);
2362 for (av = length_attr->first_value; av; av = av->next)
2363 for (ie = av->first_insn; ie; ie = ie->next)
2365 new_av = get_attr_value (substitute_address (av->value,
2366 no_address_fn[i],
2367 address_fn[i]),
2368 new_attr, ie->insn_code);
2369 new_ie = (struct insn_ent *) oballoc (sizeof (struct insn_ent));
2370 new_ie->insn_code = ie->insn_code;
2371 new_ie->insn_index = ie->insn_index;
2372 insert_insn_ent (new_av, new_ie);
2377 /* Utility functions called from above routine. */
2379 static rtx
2380 identity_fn (exp)
2381 rtx exp;
2383 return exp;
2386 static rtx
2387 zero_fn (exp)
2388 rtx exp;
2390 return make_numeric_value (0);
2393 static rtx
2394 one_fn (exp)
2395 rtx exp;
2397 return make_numeric_value (1);
2400 static rtx
2401 max_fn (exp)
2402 rtx exp;
2404 return make_numeric_value (max_attr_value (exp));
2407 /* Take a COND expression and see if any of the conditions in it can be
2408 simplified. If any are known true or known false for the particular insn
2409 code, the COND can be further simplified.
2411 Also call ourselves on any COND operations that are values of this COND.
2413 We do not modify EXP; rather, we make and return a new rtx. */
2415 static rtx
2416 simplify_cond (exp, insn_code, insn_index)
2417 rtx exp;
2418 int insn_code, insn_index;
2420 int i, j;
2421 /* We store the desired contents here,
2422 then build a new expression if they don't match EXP. */
2423 rtx defval = XEXP (exp, 1);
2424 rtx new_defval = XEXP (exp, 1);
2425 int len = XVECLEN (exp, 0);
2426 rtunion *tests = (rtunion *) alloca (len * sizeof (rtunion));
2427 int allsame = 1;
2428 char *first_spacer;
2430 /* This lets us free all storage allocated below, if appropriate. */
2431 first_spacer = (char *) obstack_finish (rtl_obstack);
2433 bcopy ((char *) XVEC (exp, 0)->elem, (char *) tests, len * sizeof (rtunion));
2435 /* See if default value needs simplification. */
2436 if (GET_CODE (defval) == COND)
2437 new_defval = simplify_cond (defval, insn_code, insn_index);
2439 /* Simplify the subexpressions, and see what tests we can get rid of. */
2441 for (i = 0; i < len; i += 2)
2443 rtx newtest, newval;
2445 /* Simplify this test. */
2446 newtest = SIMPLIFY_TEST_EXP (tests[i].rtx, insn_code, insn_index);
2447 tests[i].rtx = newtest;
2449 newval = tests[i + 1].rtx;
2450 /* See if this value may need simplification. */
2451 if (GET_CODE (newval) == COND)
2452 newval = simplify_cond (newval, insn_code, insn_index);
2454 /* Look for ways to delete or combine this test. */
2455 if (newtest == true_rtx)
2457 /* If test is true, make this value the default
2458 and discard this + any following tests. */
2459 len = i;
2460 defval = tests[i + 1].rtx;
2461 new_defval = newval;
2464 else if (newtest == false_rtx)
2466 /* If test is false, discard it and its value. */
2467 for (j = i; j < len - 2; j++)
2468 tests[j].rtx = tests[j + 2].rtx;
2469 len -= 2;
2472 else if (i > 0 && attr_equal_p (newval, tests[i - 1].rtx))
2474 /* If this value and the value for the prev test are the same,
2475 merge the tests. */
2477 tests[i - 2].rtx
2478 = insert_right_side (IOR, tests[i - 2].rtx, newtest,
2479 insn_code, insn_index);
2481 /* Delete this test/value. */
2482 for (j = i; j < len - 2; j++)
2483 tests[j].rtx = tests[j + 2].rtx;
2484 len -= 2;
2487 else
2488 tests[i + 1].rtx = newval;
2491 /* If the last test in a COND has the same value
2492 as the default value, that test isn't needed. */
2494 while (len > 0 && attr_equal_p (tests[len - 1].rtx, new_defval))
2495 len -= 2;
2497 /* See if we changed anything. */
2498 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
2499 allsame = 0;
2500 else
2501 for (i = 0; i < len; i++)
2502 if (! attr_equal_p (tests[i].rtx, XVECEXP (exp, 0, i)))
2504 allsame = 0;
2505 break;
2508 if (len == 0)
2510 obstack_free (rtl_obstack, first_spacer);
2511 if (GET_CODE (defval) == COND)
2512 return simplify_cond (defval, insn_code, insn_index);
2513 return defval;
2515 else if (allsame)
2517 obstack_free (rtl_obstack, first_spacer);
2518 return exp;
2520 else
2522 rtx newexp = rtx_alloc (COND);
2524 XVEC (newexp, 0) = rtvec_alloc (len);
2525 bcopy ((char *) tests, (char *) XVEC (newexp, 0)->elem,
2526 len * sizeof (rtunion));
2527 XEXP (newexp, 1) = new_defval;
2528 return newexp;
2532 /* Remove an insn entry from an attribute value. */
2534 static void
2535 remove_insn_ent (av, ie)
2536 struct attr_value *av;
2537 struct insn_ent *ie;
2539 struct insn_ent *previe;
2541 if (av->first_insn == ie)
2542 av->first_insn = ie->next;
2543 else
2545 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
2547 previe->next = ie->next;
2550 av->num_insns--;
2551 if (ie->insn_code == -1)
2552 av->has_asm_insn = 0;
2554 num_insn_ents--;
2557 /* Insert an insn entry in an attribute value list. */
2559 static void
2560 insert_insn_ent (av, ie)
2561 struct attr_value *av;
2562 struct insn_ent *ie;
2564 ie->next = av->first_insn;
2565 av->first_insn = ie;
2566 av->num_insns++;
2567 if (ie->insn_code == -1)
2568 av->has_asm_insn = 1;
2570 num_insn_ents++;
2573 /* This is a utility routine to take an expression that is a tree of either
2574 AND or IOR expressions and insert a new term. The new term will be
2575 inserted at the right side of the first node whose code does not match
2576 the root. A new node will be created with the root's code. Its left
2577 side will be the old right side and its right side will be the new
2578 term.
2580 If the `term' is itself a tree, all its leaves will be inserted. */
2582 static rtx
2583 insert_right_side (code, exp, term, insn_code, insn_index)
2584 enum rtx_code code;
2585 rtx exp;
2586 rtx term;
2587 int insn_code, insn_index;
2589 rtx newexp;
2591 /* Avoid consing in some special cases. */
2592 if (code == AND && term == true_rtx)
2593 return exp;
2594 if (code == AND && term == false_rtx)
2595 return false_rtx;
2596 if (code == AND && exp == true_rtx)
2597 return term;
2598 if (code == AND && exp == false_rtx)
2599 return false_rtx;
2600 if (code == IOR && term == true_rtx)
2601 return true_rtx;
2602 if (code == IOR && term == false_rtx)
2603 return exp;
2604 if (code == IOR && exp == true_rtx)
2605 return true_rtx;
2606 if (code == IOR && exp == false_rtx)
2607 return term;
2608 if (attr_equal_p (exp, term))
2609 return exp;
2611 if (GET_CODE (term) == code)
2613 exp = insert_right_side (code, exp, XEXP (term, 0),
2614 insn_code, insn_index);
2615 exp = insert_right_side (code, exp, XEXP (term, 1),
2616 insn_code, insn_index);
2618 return exp;
2621 if (GET_CODE (exp) == code)
2623 rtx new = insert_right_side (code, XEXP (exp, 1),
2624 term, insn_code, insn_index);
2625 if (new != XEXP (exp, 1))
2626 /* Make a copy of this expression and call recursively. */
2627 newexp = attr_rtx (code, XEXP (exp, 0), new);
2628 else
2629 newexp = exp;
2631 else
2633 /* Insert the new term. */
2634 newexp = attr_rtx (code, exp, term);
2637 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2640 /* If we have an expression which AND's a bunch of
2641 (not (eq_attrq "alternative" "n"))
2642 terms, we may have covered all or all but one of the possible alternatives.
2643 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
2645 This routine is passed an expression and either AND or IOR. It returns a
2646 bitmask indicating which alternatives are mentioned within EXP. */
2648 static int
2649 compute_alternative_mask (exp, code)
2650 rtx exp;
2651 enum rtx_code code;
2653 char *string;
2654 if (GET_CODE (exp) == code)
2655 return compute_alternative_mask (XEXP (exp, 0), code)
2656 | compute_alternative_mask (XEXP (exp, 1), code);
2658 else if (code == AND && GET_CODE (exp) == NOT
2659 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2660 && XSTR (XEXP (exp, 0), 0) == alternative_name)
2661 string = XSTR (XEXP (exp, 0), 1);
2663 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
2664 && XSTR (exp, 0) == alternative_name)
2665 string = XSTR (exp, 1);
2667 else
2668 return 0;
2670 if (string[1] == 0)
2671 return 1 << (string[0] - '0');
2672 return 1 << atoi (string);
2675 /* Given I, a single-bit mask, return RTX to compare the `alternative'
2676 attribute with the value represented by that bit. */
2678 static rtx
2679 make_alternative_compare (mask)
2680 int mask;
2682 rtx newexp;
2683 int i;
2685 /* Find the bit. */
2686 for (i = 0; (mask & (1 << i)) == 0; i++)
2689 newexp = attr_rtx (EQ_ATTR, alternative_name, attr_numeral (i));
2690 RTX_UNCHANGING_P (newexp) = 1;
2692 return newexp;
2695 /* If we are processing an (eq_attr "attr" "value") test, we find the value
2696 of "attr" for this insn code. From that value, we can compute a test
2697 showing when the EQ_ATTR will be true. This routine performs that
2698 computation. If a test condition involves an address, we leave the EQ_ATTR
2699 intact because addresses are only valid for the `length' attribute.
2701 EXP is the EQ_ATTR expression and VALUE is the value of that attribute
2702 for the insn corresponding to INSN_CODE and INSN_INDEX. */
2704 static rtx
2705 evaluate_eq_attr (exp, value, insn_code, insn_index)
2706 rtx exp;
2707 rtx value;
2708 int insn_code, insn_index;
2710 rtx orexp, andexp;
2711 rtx right;
2712 rtx newexp;
2713 int i;
2715 if (GET_CODE (value) == CONST_STRING)
2717 if (! strcmp (XSTR (value, 0), XSTR (exp, 1)))
2718 newexp = true_rtx;
2719 else
2720 newexp = false_rtx;
2722 else if (GET_CODE (value) == COND)
2724 /* We construct an IOR of all the cases for which the requested attribute
2725 value is present. Since we start with FALSE, if it is not present,
2726 FALSE will be returned.
2728 Each case is the AND of the NOT's of the previous conditions with the
2729 current condition; in the default case the current condition is TRUE.
2731 For each possible COND value, call ourselves recursively.
2733 The extra TRUE and FALSE expressions will be eliminated by another
2734 call to the simplification routine. */
2736 orexp = false_rtx;
2737 andexp = true_rtx;
2739 if (current_alternative_string)
2740 clear_struct_flag (value);
2742 for (i = 0; i < XVECLEN (value, 0); i += 2)
2744 rtx this = SIMPLIFY_TEST_EXP (XVECEXP (value, 0, i),
2745 insn_code, insn_index);
2747 SIMPLIFY_ALTERNATIVE (this);
2749 right = insert_right_side (AND, andexp, this,
2750 insn_code, insn_index);
2751 right = insert_right_side (AND, right,
2752 evaluate_eq_attr (exp,
2753 XVECEXP (value, 0,
2754 i + 1),
2755 insn_code, insn_index),
2756 insn_code, insn_index);
2757 orexp = insert_right_side (IOR, orexp, right,
2758 insn_code, insn_index);
2760 /* Add this condition into the AND expression. */
2761 newexp = attr_rtx (NOT, this);
2762 andexp = insert_right_side (AND, andexp, newexp,
2763 insn_code, insn_index);
2766 /* Handle the default case. */
2767 right = insert_right_side (AND, andexp,
2768 evaluate_eq_attr (exp, XEXP (value, 1),
2769 insn_code, insn_index),
2770 insn_code, insn_index);
2771 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2773 else
2774 abort ();
2776 /* If uses an address, must return original expression. But set the
2777 RTX_UNCHANGING_P bit so we don't try to simplify it again. */
2779 address_used = 0;
2780 walk_attr_value (newexp);
2782 if (address_used)
2784 /* This had `&& current_alternative_string', which seems to be wrong. */
2785 if (! RTX_UNCHANGING_P (exp))
2786 return copy_rtx_unchanging (exp);
2787 return exp;
2789 else
2790 return newexp;
2793 /* This routine is called when an AND of a term with a tree of AND's is
2794 encountered. If the term or its complement is present in the tree, it
2795 can be replaced with TRUE or FALSE, respectively.
2797 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2798 be true and hence are complementary.
2800 There is one special case: If we see
2801 (and (not (eq_attr "att" "v1"))
2802 (eq_attr "att" "v2"))
2803 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2804 replace the term, not anything in the AND tree. So we pass a pointer to
2805 the term. */
2807 static rtx
2808 simplify_and_tree (exp, pterm, insn_code, insn_index)
2809 rtx exp;
2810 rtx *pterm;
2811 int insn_code, insn_index;
2813 rtx left, right;
2814 rtx newexp;
2815 rtx temp;
2816 int left_eliminates_term, right_eliminates_term;
2818 if (GET_CODE (exp) == AND)
2820 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2821 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2822 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2824 newexp = attr_rtx (GET_CODE (exp), left, right);
2826 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2830 else if (GET_CODE (exp) == IOR)
2832 /* For the IOR case, we do the same as above, except that we can
2833 only eliminate `term' if both sides of the IOR would do so. */
2834 temp = *pterm;
2835 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2836 left_eliminates_term = (temp == true_rtx);
2838 temp = *pterm;
2839 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2840 right_eliminates_term = (temp == true_rtx);
2842 if (left_eliminates_term && right_eliminates_term)
2843 *pterm = true_rtx;
2845 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2847 newexp = attr_rtx (GET_CODE (exp), left, right);
2849 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2853 /* Check for simplifications. Do some extra checking here since this
2854 routine is called so many times. */
2856 if (exp == *pterm)
2857 return true_rtx;
2859 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2860 return false_rtx;
2862 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2863 return false_rtx;
2865 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2867 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2868 return exp;
2870 if (! strcmp (XSTR (exp, 1), XSTR (*pterm, 1)))
2871 return true_rtx;
2872 else
2873 return false_rtx;
2876 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2877 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2879 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2880 return exp;
2882 if (! strcmp (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2883 return false_rtx;
2884 else
2885 return true_rtx;
2888 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2889 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2891 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2892 return exp;
2894 if (! strcmp (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2895 return false_rtx;
2896 else
2897 *pterm = true_rtx;
2900 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2902 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2903 return true_rtx;
2906 else if (GET_CODE (exp) == NOT)
2908 if (attr_equal_p (XEXP (exp, 0), *pterm))
2909 return false_rtx;
2912 else if (GET_CODE (*pterm) == NOT)
2914 if (attr_equal_p (XEXP (*pterm, 0), exp))
2915 return false_rtx;
2918 else if (attr_equal_p (exp, *pterm))
2919 return true_rtx;
2921 return exp;
2924 /* Similar to `simplify_and_tree', but for IOR trees. */
2926 static rtx
2927 simplify_or_tree (exp, pterm, insn_code, insn_index)
2928 rtx exp;
2929 rtx *pterm;
2930 int insn_code, insn_index;
2932 rtx left, right;
2933 rtx newexp;
2934 rtx temp;
2935 int left_eliminates_term, right_eliminates_term;
2937 if (GET_CODE (exp) == IOR)
2939 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2940 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2941 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2943 newexp = attr_rtx (GET_CODE (exp), left, right);
2945 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2949 else if (GET_CODE (exp) == AND)
2951 /* For the AND case, we do the same as above, except that we can
2952 only eliminate `term' if both sides of the AND would do so. */
2953 temp = *pterm;
2954 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2955 left_eliminates_term = (temp == false_rtx);
2957 temp = *pterm;
2958 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2959 right_eliminates_term = (temp == false_rtx);
2961 if (left_eliminates_term && right_eliminates_term)
2962 *pterm = false_rtx;
2964 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2966 newexp = attr_rtx (GET_CODE (exp), left, right);
2968 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2972 if (attr_equal_p (exp, *pterm))
2973 return false_rtx;
2975 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
2976 return true_rtx;
2978 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
2979 return true_rtx;
2981 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2982 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2983 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
2984 *pterm = false_rtx;
2986 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2987 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
2988 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
2989 return false_rtx;
2991 return exp;
2994 /* Given an expression, see if it can be simplified for a particular insn
2995 code based on the values of other attributes being tested. This can
2996 eliminate nested get_attr_... calls.
2998 Note that if an endless recursion is specified in the patterns, the
2999 optimization will loop. However, it will do so in precisely the cases where
3000 an infinite recursion loop could occur during compilation. It's better that
3001 it occurs here! */
3003 static rtx
3004 simplify_test_exp (exp, insn_code, insn_index)
3005 rtx exp;
3006 int insn_code, insn_index;
3008 rtx left, right;
3009 struct attr_desc *attr;
3010 struct attr_value *av;
3011 struct insn_ent *ie;
3012 int i;
3013 rtx newexp = exp;
3014 char *spacer = (char *) obstack_finish (rtl_obstack);
3016 /* Don't re-simplify something we already simplified. */
3017 if (RTX_UNCHANGING_P (exp) || MEM_IN_STRUCT_P (exp))
3018 return exp;
3020 switch (GET_CODE (exp))
3022 case AND:
3023 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3024 SIMPLIFY_ALTERNATIVE (left);
3025 if (left == false_rtx)
3027 obstack_free (rtl_obstack, spacer);
3028 return false_rtx;
3030 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3031 SIMPLIFY_ALTERNATIVE (right);
3032 if (left == false_rtx)
3034 obstack_free (rtl_obstack, spacer);
3035 return false_rtx;
3038 /* If either side is an IOR and we have (eq_attr "alternative" ..")
3039 present on both sides, apply the distributive law since this will
3040 yield simplifications. */
3041 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
3042 && compute_alternative_mask (left, IOR)
3043 && compute_alternative_mask (right, IOR))
3045 if (GET_CODE (left) == IOR)
3047 rtx tem = left;
3048 left = right;
3049 right = tem;
3052 newexp = attr_rtx (IOR,
3053 attr_rtx (AND, left, XEXP (right, 0)),
3054 attr_rtx (AND, left, XEXP (right, 1)));
3056 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3059 /* Try with the term on both sides. */
3060 right = simplify_and_tree (right, &left, insn_code, insn_index);
3061 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3062 left = simplify_and_tree (left, &right, insn_code, insn_index);
3064 if (left == false_rtx || right == false_rtx)
3066 obstack_free (rtl_obstack, spacer);
3067 return false_rtx;
3069 else if (left == true_rtx)
3071 return right;
3073 else if (right == true_rtx)
3075 return left;
3077 /* See if all or all but one of the insn's alternatives are specified
3078 in this tree. Optimize if so. */
3080 else if (insn_code >= 0
3081 && (GET_CODE (left) == AND
3082 || (GET_CODE (left) == NOT
3083 && GET_CODE (XEXP (left, 0)) == EQ_ATTR
3084 && XSTR (XEXP (left, 0), 0) == alternative_name)
3085 || GET_CODE (right) == AND
3086 || (GET_CODE (right) == NOT
3087 && GET_CODE (XEXP (right, 0)) == EQ_ATTR
3088 && XSTR (XEXP (right, 0), 0) == alternative_name)))
3090 i = compute_alternative_mask (exp, AND);
3091 if (i & ~insn_alternatives[insn_code])
3092 fatal ("Invalid alternative specified for pattern number %d",
3093 insn_index);
3095 /* If all alternatives are excluded, this is false. */
3096 i ^= insn_alternatives[insn_code];
3097 if (i == 0)
3098 return false_rtx;
3099 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3101 /* If just one excluded, AND a comparison with that one to the
3102 front of the tree. The others will be eliminated by
3103 optimization. We do not want to do this if the insn has one
3104 alternative and we have tested none of them! */
3105 left = make_alternative_compare (i);
3106 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3107 newexp = attr_rtx (AND, left, right);
3109 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3113 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3115 newexp = attr_rtx (AND, left, right);
3116 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3118 break;
3120 case IOR:
3121 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3122 SIMPLIFY_ALTERNATIVE (left);
3123 if (left == true_rtx)
3125 obstack_free (rtl_obstack, spacer);
3126 return true_rtx;
3128 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
3129 SIMPLIFY_ALTERNATIVE (right);
3130 if (right == true_rtx)
3132 obstack_free (rtl_obstack, spacer);
3133 return true_rtx;
3136 right = simplify_or_tree (right, &left, insn_code, insn_index);
3137 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
3138 left = simplify_or_tree (left, &right, insn_code, insn_index);
3140 if (right == true_rtx || left == true_rtx)
3142 obstack_free (rtl_obstack, spacer);
3143 return true_rtx;
3145 else if (left == false_rtx)
3147 return right;
3149 else if (right == false_rtx)
3151 return left;
3154 /* Test for simple cases where the distributive law is useful. I.e.,
3155 convert (ior (and (x) (y))
3156 (and (x) (z)))
3157 to (and (x)
3158 (ior (y) (z)))
3161 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
3162 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
3164 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
3166 left = XEXP (left, 0);
3167 right = newexp;
3168 newexp = attr_rtx (AND, left, right);
3169 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3172 /* See if all or all but one of the insn's alternatives are specified
3173 in this tree. Optimize if so. */
3175 else if (insn_code >= 0
3176 && (GET_CODE (left) == IOR
3177 || (GET_CODE (left) == EQ_ATTR
3178 && XSTR (left, 0) == alternative_name)
3179 || GET_CODE (right) == IOR
3180 || (GET_CODE (right) == EQ_ATTR
3181 && XSTR (right, 0) == alternative_name)))
3183 i = compute_alternative_mask (exp, IOR);
3184 if (i & ~insn_alternatives[insn_code])
3185 fatal ("Invalid alternative specified for pattern number %d",
3186 insn_index);
3188 /* If all alternatives are included, this is true. */
3189 i ^= insn_alternatives[insn_code];
3190 if (i == 0)
3191 return true_rtx;
3192 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
3194 /* If just one excluded, IOR a comparison with that one to the
3195 front of the tree. The others will be eliminated by
3196 optimization. We do not want to do this if the insn has one
3197 alternative and we have tested none of them! */
3198 left = make_alternative_compare (i);
3199 right = simplify_and_tree (exp, &left, insn_code, insn_index);
3200 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
3202 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3206 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
3208 newexp = attr_rtx (IOR, left, right);
3209 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3211 break;
3213 case NOT:
3214 if (GET_CODE (XEXP (exp, 0)) == NOT)
3216 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
3217 insn_code, insn_index);
3218 SIMPLIFY_ALTERNATIVE (left);
3219 return left;
3222 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
3223 SIMPLIFY_ALTERNATIVE (left);
3224 if (GET_CODE (left) == NOT)
3225 return XEXP (left, 0);
3227 if (left == false_rtx)
3229 obstack_free (rtl_obstack, spacer);
3230 return true_rtx;
3232 else if (left == true_rtx)
3234 obstack_free (rtl_obstack, spacer);
3235 return false_rtx;
3238 /* Try to apply De`Morgan's laws. */
3239 else if (GET_CODE (left) == IOR)
3241 newexp = attr_rtx (AND,
3242 attr_rtx (NOT, XEXP (left, 0)),
3243 attr_rtx (NOT, XEXP (left, 1)));
3245 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3247 else if (GET_CODE (left) == AND)
3249 newexp = attr_rtx (IOR,
3250 attr_rtx (NOT, XEXP (left, 0)),
3251 attr_rtx (NOT, XEXP (left, 1)));
3253 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
3255 else if (left != XEXP (exp, 0))
3257 newexp = attr_rtx (NOT, left);
3259 break;
3261 case EQ_ATTR:
3262 if (current_alternative_string && XSTR (exp, 0) == alternative_name)
3263 return (XSTR (exp, 1) == current_alternative_string
3264 ? true_rtx : false_rtx);
3266 /* Look at the value for this insn code in the specified attribute.
3267 We normally can replace this comparison with the condition that
3268 would give this insn the values being tested for. */
3269 if (XSTR (exp, 0) != alternative_name
3270 && (attr = find_attr (XSTR (exp, 0), 0)) != NULL)
3271 for (av = attr->first_value; av; av = av->next)
3272 for (ie = av->first_insn; ie; ie = ie->next)
3273 if (ie->insn_code == insn_code)
3274 return evaluate_eq_attr (exp, av->value, insn_code, insn_index);
3277 /* We have already simplified this expression. Simplifying it again
3278 won't buy anything unless we weren't given a valid insn code
3279 to process (i.e., we are canonicalizing something.). */
3280 if (insn_code != -2 /* Seems wrong: && current_alternative_string. */
3281 && ! RTX_UNCHANGING_P (newexp))
3282 return copy_rtx_unchanging (newexp);
3284 return newexp;
3287 /* Optimize the attribute lists by seeing if we can determine conditional
3288 values from the known values of other attributes. This will save subroutine
3289 calls during the compilation. */
3291 static void
3292 optimize_attrs ()
3294 struct attr_desc *attr;
3295 struct attr_value *av;
3296 struct insn_ent *ie;
3297 rtx newexp;
3298 int something_changed = 1;
3299 int i;
3300 struct attr_value_list { struct attr_value *av;
3301 struct insn_ent *ie;
3302 struct attr_desc * attr;
3303 struct attr_value_list *next; };
3304 struct attr_value_list **insn_code_values;
3305 struct attr_value_list *ivbuf;
3306 struct attr_value_list *iv;
3308 /* For each insn code, make a list of all the insn_ent's for it,
3309 for all values for all attributes. */
3311 if (num_insn_ents == 0)
3312 return;
3314 /* Make 2 extra elements, for "code" values -2 and -1. */
3315 insn_code_values
3316 = (struct attr_value_list **) alloca ((insn_code_number + 2)
3317 * sizeof (struct attr_value_list *));
3318 bzero ((char *) insn_code_values,
3319 (insn_code_number + 2) * sizeof (struct attr_value_list *));
3321 /* Offset the table address so we can index by -2 or -1. */
3322 insn_code_values += 2;
3324 /* Allocate the attr_value_list structures using xmalloc rather than
3325 alloca, because using alloca can overflow the maximum permitted
3326 stack limit on SPARC Lynx. */
3327 iv = ivbuf = ((struct attr_value_list *)
3328 xmalloc (num_insn_ents * sizeof (struct attr_value_list)));
3330 for (i = 0; i < MAX_ATTRS_INDEX; i++)
3331 for (attr = attrs[i]; attr; attr = attr->next)
3332 for (av = attr->first_value; av; av = av->next)
3333 for (ie = av->first_insn; ie; ie = ie->next)
3335 iv->attr = attr;
3336 iv->av = av;
3337 iv->ie = ie;
3338 iv->next = insn_code_values[ie->insn_code];
3339 insn_code_values[ie->insn_code] = iv;
3340 iv++;
3343 /* Sanity check on num_insn_ents. */
3344 if (iv != ivbuf + num_insn_ents)
3345 abort ();
3347 /* Process one insn code at a time. */
3348 for (i = -2; i < insn_code_number; i++)
3350 /* Clear the MEM_IN_STRUCT_P flag everywhere relevant.
3351 We use it to mean "already simplified for this insn". */
3352 for (iv = insn_code_values[i]; iv; iv = iv->next)
3353 clear_struct_flag (iv->av->value);
3355 /* Loop until nothing changes for one iteration. */
3356 something_changed = 1;
3357 while (something_changed)
3359 something_changed = 0;
3360 for (iv = insn_code_values[i]; iv; iv = iv->next)
3362 struct obstack *old = rtl_obstack;
3363 char *spacer = (char *) obstack_finish (temp_obstack);
3365 attr = iv->attr;
3366 av = iv->av;
3367 ie = iv->ie;
3368 if (GET_CODE (av->value) != COND)
3369 continue;
3371 rtl_obstack = temp_obstack;
3372 #if 0 /* This was intended as a speed up, but it was slower. */
3373 if (insn_n_alternatives[ie->insn_code] > 6
3374 && count_sub_rtxs (av->value, 200) >= 200)
3375 newexp = simplify_by_alternatives (av->value, ie->insn_code,
3376 ie->insn_index);
3377 else
3378 #endif
3379 newexp = simplify_cond (av->value, ie->insn_code,
3380 ie->insn_index);
3382 rtl_obstack = old;
3383 if (newexp != av->value)
3385 newexp = attr_copy_rtx (newexp);
3386 remove_insn_ent (av, ie);
3387 av = get_attr_value (newexp, attr, ie->insn_code);
3388 iv->av = av;
3389 insert_insn_ent (av, ie);
3390 something_changed = 1;
3392 obstack_free (temp_obstack, spacer);
3397 free (ivbuf);
3400 #if 0
3401 static rtx
3402 simplify_by_alternatives (exp, insn_code, insn_index)
3403 rtx exp;
3404 int insn_code, insn_index;
3406 int i;
3407 int len = insn_n_alternatives[insn_code];
3408 rtx newexp = rtx_alloc (COND);
3409 rtx ultimate;
3412 XVEC (newexp, 0) = rtvec_alloc (len * 2);
3414 /* It will not matter what value we use as the default value
3415 of the new COND, since that default will never be used.
3416 Choose something of the right type. */
3417 for (ultimate = exp; GET_CODE (ultimate) == COND;)
3418 ultimate = XEXP (ultimate, 1);
3419 XEXP (newexp, 1) = ultimate;
3421 for (i = 0; i < insn_n_alternatives[insn_code]; i++)
3423 current_alternative_string = attr_numeral (i);
3424 XVECEXP (newexp, 0, i * 2) = make_alternative_compare (1 << i);
3425 XVECEXP (newexp, 0, i * 2 + 1)
3426 = simplify_cond (exp, insn_code, insn_index);
3429 current_alternative_string = 0;
3430 return simplify_cond (newexp, insn_code, insn_index);
3432 #endif
3434 /* If EXP is a suitable expression, reorganize it by constructing an
3435 equivalent expression that is a COND with the tests being all combinations
3436 of attribute values and the values being simple constants. */
3438 static rtx
3439 simplify_by_exploding (exp)
3440 rtx exp;
3442 rtx list = 0, link, condexp, defval;
3443 struct dimension *space;
3444 rtx *condtest, *condval;
3445 int i, j, total, ndim = 0;
3446 int most_tests, num_marks, new_marks;
3448 /* Locate all the EQ_ATTR expressions. */
3449 if (! find_and_mark_used_attributes (exp, &list, &ndim) || ndim == 0)
3451 unmark_used_attributes (list, 0, 0);
3452 return exp;
3455 /* Create an attribute space from the list of used attributes. For each
3456 dimension in the attribute space, record the attribute, list of values
3457 used, and number of values used. Add members to the list of values to
3458 cover the domain of the attribute. This makes the expanded COND form
3459 order independent. */
3461 space = (struct dimension *) alloca (ndim * sizeof (struct dimension));
3463 total = 1;
3464 for (ndim = 0; list; ndim++)
3466 /* Pull the first attribute value from the list and record that
3467 attribute as another dimension in the attribute space. */
3468 char *name = XSTR (XEXP (list, 0), 0);
3469 rtx *prev;
3471 if ((space[ndim].attr = find_attr (name, 0)) == 0
3472 || space[ndim].attr->is_numeric)
3474 unmark_used_attributes (list, space, ndim);
3475 return exp;
3478 /* Add all remaining attribute values that refer to this attribute. */
3479 space[ndim].num_values = 0;
3480 space[ndim].values = 0;
3481 prev = &list;
3482 for (link = list; link; link = *prev)
3483 if (! strcmp (XSTR (XEXP (link, 0), 0), name))
3485 space[ndim].num_values++;
3486 *prev = XEXP (link, 1);
3487 XEXP (link, 1) = space[ndim].values;
3488 space[ndim].values = link;
3490 else
3491 prev = &XEXP (link, 1);
3493 /* Add sufficient members to the list of values to make the list
3494 mutually exclusive and record the total size of the attribute
3495 space. */
3496 total *= add_values_to_cover (&space[ndim]);
3499 /* Sort the attribute space so that the attributes go from non-constant
3500 to constant and from most values to least values. */
3501 for (i = 0; i < ndim; i++)
3502 for (j = ndim - 1; j > i; j--)
3503 if ((space[j-1].attr->is_const && !space[j].attr->is_const)
3504 || space[j-1].num_values < space[j].num_values)
3506 struct dimension tmp;
3507 tmp = space[j];
3508 space[j] = space[j-1];
3509 space[j-1] = tmp;
3512 /* Establish the initial current value. */
3513 for (i = 0; i < ndim; i++)
3514 space[i].current_value = space[i].values;
3516 condtest = (rtx *) alloca (total * sizeof (rtx));
3517 condval = (rtx *) alloca (total * sizeof (rtx));
3519 /* Expand the tests and values by iterating over all values in the
3520 attribute space. */
3521 for (i = 0;; i++)
3523 condtest[i] = test_for_current_value (space, ndim);
3524 condval[i] = simplify_with_current_value (exp, space, ndim);
3525 if (! increment_current_value (space, ndim))
3526 break;
3528 if (i != total - 1)
3529 abort ();
3531 /* We are now finished with the original expression. */
3532 unmark_used_attributes (0, space, ndim);
3534 /* Find the most used constant value and make that the default. */
3535 most_tests = -1;
3536 for (i = num_marks = 0; i < total; i++)
3537 if (GET_CODE (condval[i]) == CONST_STRING
3538 && ! MEM_VOLATILE_P (condval[i]))
3540 /* Mark the unmarked constant value and count how many are marked. */
3541 MEM_VOLATILE_P (condval[i]) = 1;
3542 for (j = new_marks = 0; j < total; j++)
3543 if (GET_CODE (condval[j]) == CONST_STRING
3544 && MEM_VOLATILE_P (condval[j]))
3545 new_marks++;
3546 if (new_marks - num_marks > most_tests)
3548 most_tests = new_marks - num_marks;
3549 defval = condval[i];
3551 num_marks = new_marks;
3553 /* Clear all the marks. */
3554 for (i = 0; i < total; i++)
3555 MEM_VOLATILE_P (condval[i]) = 0;
3557 /* Give up if nothing is constant. */
3558 if (num_marks == 0)
3559 return exp;
3561 /* If all values are the default, use that. */
3562 if (total == most_tests)
3563 return defval;
3565 /* Make a COND with the most common constant value the default. (A more
3566 complex method where tests with the same value were combined didn't
3567 seem to improve things.) */
3568 condexp = rtx_alloc (COND);
3569 XVEC (condexp, 0) = rtvec_alloc ((total - most_tests) * 2);
3570 XEXP (condexp, 1) = defval;
3571 for (i = j = 0; i < total; i++)
3572 if (condval[i] != defval)
3574 XVECEXP (condexp, 0, 2 * j) = condtest[i];
3575 XVECEXP (condexp, 0, 2 * j + 1) = condval[i];
3576 j++;
3579 return condexp;
3582 /* Set the MEM_VOLATILE_P flag for all EQ_ATTR expressions in EXP and
3583 verify that EXP can be simplified to a constant term if all the EQ_ATTR
3584 tests have known value. */
3586 static int
3587 find_and_mark_used_attributes (exp, terms, nterms)
3588 rtx exp, *terms;
3589 int *nterms;
3591 int i;
3593 switch (GET_CODE (exp))
3595 case EQ_ATTR:
3596 if (! MEM_VOLATILE_P (exp))
3598 rtx link = rtx_alloc (EXPR_LIST);
3599 XEXP (link, 0) = exp;
3600 XEXP (link, 1) = *terms;
3601 *terms = link;
3602 *nterms += 1;
3603 MEM_VOLATILE_P (exp) = 1;
3605 case CONST_STRING:
3606 return 1;
3608 case IF_THEN_ELSE:
3609 if (! find_and_mark_used_attributes (XEXP (exp, 2), terms, nterms))
3610 return 0;
3611 case IOR:
3612 case AND:
3613 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3614 return 0;
3615 case NOT:
3616 if (! find_and_mark_used_attributes (XEXP (exp, 0), terms, nterms))
3617 return 0;
3618 return 1;
3620 case COND:
3621 for (i = 0; i < XVECLEN (exp, 0); i++)
3622 if (! find_and_mark_used_attributes (XVECEXP (exp, 0, i), terms, nterms))
3623 return 0;
3624 if (! find_and_mark_used_attributes (XEXP (exp, 1), terms, nterms))
3625 return 0;
3626 return 1;
3629 return 0;
3632 /* Clear the MEM_VOLATILE_P flag in all EQ_ATTR expressions on LIST and
3633 in the values of the NDIM-dimensional attribute space SPACE. */
3635 static void
3636 unmark_used_attributes (list, space, ndim)
3637 rtx list;
3638 struct dimension *space;
3639 int ndim;
3641 rtx link, exp;
3642 int i;
3644 for (i = 0; i < ndim; i++)
3645 unmark_used_attributes (space[i].values, 0, 0);
3647 for (link = list; link; link = XEXP (link, 1))
3649 exp = XEXP (link, 0);
3650 if (GET_CODE (exp) == EQ_ATTR)
3651 MEM_VOLATILE_P (exp) = 0;
3655 /* Update the attribute dimension DIM so that all values of the attribute
3656 are tested. Return the updated number of values. */
3658 static int
3659 add_values_to_cover (dim)
3660 struct dimension *dim;
3662 struct attr_value *av;
3663 rtx exp, link, *prev;
3664 int nalt = 0;
3666 for (av = dim->attr->first_value; av; av = av->next)
3667 if (GET_CODE (av->value) == CONST_STRING)
3668 nalt++;
3670 if (nalt < dim->num_values)
3671 abort ();
3672 else if (nalt == dim->num_values)
3673 ; /* Ok. */
3674 else if (nalt * 2 < dim->num_values * 3)
3676 /* Most all the values of the attribute are used, so add all the unused
3677 values. */
3678 prev = &dim->values;
3679 for (link = dim->values; link; link = *prev)
3680 prev = &XEXP (link, 1);
3682 for (av = dim->attr->first_value; av; av = av->next)
3683 if (GET_CODE (av->value) == CONST_STRING)
3685 exp = attr_eq (dim->attr->name, XSTR (av->value, 0));
3686 if (MEM_VOLATILE_P (exp))
3687 continue;
3689 link = rtx_alloc (EXPR_LIST);
3690 XEXP (link, 0) = exp;
3691 XEXP (link, 1) = 0;
3692 *prev = link;
3693 prev = &XEXP (link, 1);
3695 dim->num_values = nalt;
3697 else
3699 rtx orexp = false_rtx;
3701 /* Very few values are used, so compute a mutually exclusive
3702 expression. (We could do this for numeric values if that becomes
3703 important.) */
3704 prev = &dim->values;
3705 for (link = dim->values; link; link = *prev)
3707 orexp = insert_right_side (IOR, orexp, XEXP (link, 0), -2, -2);
3708 prev = &XEXP (link, 1);
3710 link = rtx_alloc (EXPR_LIST);
3711 XEXP (link, 0) = attr_rtx (NOT, orexp);
3712 XEXP (link, 1) = 0;
3713 *prev = link;
3714 dim->num_values++;
3716 return dim->num_values;
3719 /* Increment the current value for the NDIM-dimensional attribute space SPACE
3720 and return FALSE if the increment overflowed. */
3722 static int
3723 increment_current_value (space, ndim)
3724 struct dimension *space;
3725 int ndim;
3727 int i;
3729 for (i = ndim - 1; i >= 0; i--)
3731 if ((space[i].current_value = XEXP (space[i].current_value, 1)) == 0)
3732 space[i].current_value = space[i].values;
3733 else
3734 return 1;
3736 return 0;
3739 /* Construct an expression corresponding to the current value for the
3740 NDIM-dimensional attribute space SPACE. */
3742 static rtx
3743 test_for_current_value (space, ndim)
3744 struct dimension *space;
3745 int ndim;
3747 int i;
3748 rtx exp = true_rtx;
3750 for (i = 0; i < ndim; i++)
3751 exp = insert_right_side (AND, exp, XEXP (space[i].current_value, 0),
3752 -2, -2);
3754 return exp;
3757 /* Given the current value of the NDIM-dimensional attribute space SPACE,
3758 set the corresponding EQ_ATTR expressions to that value and reduce
3759 the expression EXP as much as possible. On input [and output], all
3760 known EQ_ATTR expressions are set to FALSE. */
3762 static rtx
3763 simplify_with_current_value (exp, space, ndim)
3764 rtx exp;
3765 struct dimension *space;
3766 int ndim;
3768 int i;
3769 rtx x;
3771 /* Mark each current value as TRUE. */
3772 for (i = 0; i < ndim; i++)
3774 x = XEXP (space[i].current_value, 0);
3775 if (GET_CODE (x) == EQ_ATTR)
3776 MEM_VOLATILE_P (x) = 0;
3779 exp = simplify_with_current_value_aux (exp);
3781 /* Change each current value back to FALSE. */
3782 for (i = 0; i < ndim; i++)
3784 x = XEXP (space[i].current_value, 0);
3785 if (GET_CODE (x) == EQ_ATTR)
3786 MEM_VOLATILE_P (x) = 1;
3789 return exp;
3792 /* Reduce the expression EXP based on the MEM_VOLATILE_P settings of
3793 all EQ_ATTR expressions. */
3795 static rtx
3796 simplify_with_current_value_aux (exp)
3797 rtx exp;
3799 register int i;
3800 rtx cond;
3802 switch (GET_CODE (exp))
3804 case EQ_ATTR:
3805 if (MEM_VOLATILE_P (exp))
3806 return false_rtx;
3807 else
3808 return true_rtx;
3809 case CONST_STRING:
3810 return exp;
3812 case IF_THEN_ELSE:
3813 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3814 if (cond == true_rtx)
3815 return simplify_with_current_value_aux (XEXP (exp, 1));
3816 else if (cond == false_rtx)
3817 return simplify_with_current_value_aux (XEXP (exp, 2));
3818 else
3819 return attr_rtx (IF_THEN_ELSE, cond,
3820 simplify_with_current_value_aux (XEXP (exp, 1)),
3821 simplify_with_current_value_aux (XEXP (exp, 2)));
3823 case IOR:
3824 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3825 if (cond == true_rtx)
3826 return cond;
3827 else if (cond == false_rtx)
3828 return simplify_with_current_value_aux (XEXP (exp, 0));
3829 else
3830 return attr_rtx (IOR, cond,
3831 simplify_with_current_value_aux (XEXP (exp, 0)));
3833 case AND:
3834 cond = simplify_with_current_value_aux (XEXP (exp, 1));
3835 if (cond == true_rtx)
3836 return simplify_with_current_value_aux (XEXP (exp, 0));
3837 else if (cond == false_rtx)
3838 return cond;
3839 else
3840 return attr_rtx (AND, cond,
3841 simplify_with_current_value_aux (XEXP (exp, 0)));
3843 case NOT:
3844 cond = simplify_with_current_value_aux (XEXP (exp, 0));
3845 if (cond == true_rtx)
3846 return false_rtx;
3847 else if (cond == false_rtx)
3848 return true_rtx;
3849 else
3850 return attr_rtx (NOT, cond);
3852 case COND:
3853 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3855 cond = simplify_with_current_value_aux (XVECEXP (exp, 0, i));
3856 if (cond == true_rtx)
3857 return simplify_with_current_value_aux (XVECEXP (exp, 0, i + 1));
3858 else if (cond == false_rtx)
3859 continue;
3860 else
3861 abort (); /* With all EQ_ATTR's of known value, a case should
3862 have been selected. */
3864 return simplify_with_current_value_aux (XEXP (exp, 1));
3866 abort ();
3869 /* Clear the MEM_IN_STRUCT_P flag in EXP and its subexpressions. */
3871 static void
3872 clear_struct_flag (x)
3873 rtx x;
3875 register int i;
3876 register int j;
3877 register enum rtx_code code;
3878 register char *fmt;
3880 MEM_IN_STRUCT_P (x) = 0;
3881 if (RTX_UNCHANGING_P (x))
3882 return;
3884 code = GET_CODE (x);
3886 switch (code)
3888 case REG:
3889 case QUEUED:
3890 case CONST_INT:
3891 case CONST_DOUBLE:
3892 case SYMBOL_REF:
3893 case CODE_LABEL:
3894 case PC:
3895 case CC0:
3896 case EQ_ATTR:
3897 case ATTR_FLAG:
3898 return;
3901 /* Compare the elements. If any pair of corresponding elements
3902 fail to match, return 0 for the whole things. */
3904 fmt = GET_RTX_FORMAT (code);
3905 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3907 switch (fmt[i])
3909 case 'V':
3910 case 'E':
3911 for (j = 0; j < XVECLEN (x, i); j++)
3912 clear_struct_flag (XVECEXP (x, i, j));
3913 break;
3915 case 'e':
3916 clear_struct_flag (XEXP (x, i));
3917 break;
3922 /* Return the number of RTX objects making up the expression X.
3923 But if we count more more than MAX objects, stop counting. */
3925 static int
3926 count_sub_rtxs (x, max)
3927 rtx x;
3928 int max;
3930 register int i;
3931 register int j;
3932 register enum rtx_code code;
3933 register char *fmt;
3934 int total = 0;
3936 code = GET_CODE (x);
3938 switch (code)
3940 case REG:
3941 case QUEUED:
3942 case CONST_INT:
3943 case CONST_DOUBLE:
3944 case SYMBOL_REF:
3945 case CODE_LABEL:
3946 case PC:
3947 case CC0:
3948 case EQ_ATTR:
3949 case ATTR_FLAG:
3950 return 1;
3953 /* Compare the elements. If any pair of corresponding elements
3954 fail to match, return 0 for the whole things. */
3956 fmt = GET_RTX_FORMAT (code);
3957 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3959 if (total >= max)
3960 return total;
3962 switch (fmt[i])
3964 case 'V':
3965 case 'E':
3966 for (j = 0; j < XVECLEN (x, i); j++)
3967 total += count_sub_rtxs (XVECEXP (x, i, j), max);
3968 break;
3970 case 'e':
3971 total += count_sub_rtxs (XEXP (x, i), max);
3972 break;
3975 return total;
3979 /* Create table entries for DEFINE_ATTR. */
3981 static void
3982 gen_attr (exp)
3983 rtx exp;
3985 struct attr_desc *attr;
3986 struct attr_value *av;
3987 char *name_ptr;
3988 char *p;
3990 /* Make a new attribute structure. Check for duplicate by looking at
3991 attr->default_val, since it is initialized by this routine. */
3992 attr = find_attr (XSTR (exp, 0), 1);
3993 if (attr->default_val)
3994 fatal ("Duplicate definition for `%s' attribute", attr->name);
3996 if (*XSTR (exp, 1) == '\0')
3997 attr->is_numeric = 1;
3998 else
4000 name_ptr = XSTR (exp, 1);
4001 while ((p = next_comma_elt (&name_ptr)) != NULL)
4003 av = (struct attr_value *) oballoc (sizeof (struct attr_value));
4004 av->value = attr_rtx (CONST_STRING, p);
4005 av->next = attr->first_value;
4006 attr->first_value = av;
4007 av->first_insn = NULL;
4008 av->num_insns = 0;
4009 av->has_asm_insn = 0;
4013 if (GET_CODE (XEXP (exp, 2)) == CONST)
4015 attr->is_const = 1;
4016 if (attr->is_numeric)
4017 fatal ("Constant attributes may not take numeric values");
4018 /* Get rid of the CONST node. It is allowed only at top-level. */
4019 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
4022 if (! strcmp (attr->name, "length") && ! attr->is_numeric)
4023 fatal ("`length' attribute must take numeric values");
4025 /* Set up the default value. */
4026 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
4027 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
4030 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
4031 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
4032 number of alternatives as this should be checked elsewhere. */
4034 static int
4035 count_alternatives (exp)
4036 rtx exp;
4038 int i, j, n;
4039 char *fmt;
4041 if (GET_CODE (exp) == MATCH_OPERAND)
4042 return n_comma_elts (XSTR (exp, 2));
4044 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4045 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4046 switch (*fmt++)
4048 case 'e':
4049 case 'u':
4050 n = count_alternatives (XEXP (exp, i));
4051 if (n)
4052 return n;
4053 break;
4055 case 'E':
4056 case 'V':
4057 if (XVEC (exp, i) != NULL)
4058 for (j = 0; j < XVECLEN (exp, i); j++)
4060 n = count_alternatives (XVECEXP (exp, i, j));
4061 if (n)
4062 return n;
4066 return 0;
4069 /* Returns non-zero if the given expression contains an EQ_ATTR with the
4070 `alternative' attribute. */
4072 static int
4073 compares_alternatives_p (exp)
4074 rtx exp;
4076 int i, j;
4077 char *fmt;
4079 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
4080 return 1;
4082 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4083 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4084 switch (*fmt++)
4086 case 'e':
4087 case 'u':
4088 if (compares_alternatives_p (XEXP (exp, i)))
4089 return 1;
4090 break;
4092 case 'E':
4093 for (j = 0; j < XVECLEN (exp, i); j++)
4094 if (compares_alternatives_p (XVECEXP (exp, i, j)))
4095 return 1;
4096 break;
4099 return 0;
4102 /* Returns non-zero is INNER is contained in EXP. */
4104 static int
4105 contained_in_p (inner, exp)
4106 rtx inner;
4107 rtx exp;
4109 int i, j;
4110 char *fmt;
4112 if (rtx_equal_p (inner, exp))
4113 return 1;
4115 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
4116 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
4117 switch (*fmt++)
4119 case 'e':
4120 case 'u':
4121 if (contained_in_p (inner, XEXP (exp, i)))
4122 return 1;
4123 break;
4125 case 'E':
4126 for (j = 0; j < XVECLEN (exp, i); j++)
4127 if (contained_in_p (inner, XVECEXP (exp, i, j)))
4128 return 1;
4129 break;
4132 return 0;
4135 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
4137 static void
4138 gen_insn (exp)
4139 rtx exp;
4141 struct insn_def *id;
4143 id = (struct insn_def *) oballoc (sizeof (struct insn_def));
4144 id->next = defs;
4145 defs = id;
4146 id->def = exp;
4148 switch (GET_CODE (exp))
4150 case DEFINE_INSN:
4151 id->insn_code = insn_code_number++;
4152 id->insn_index = insn_index_number++;
4153 id->num_alternatives = count_alternatives (exp);
4154 if (id->num_alternatives == 0)
4155 id->num_alternatives = 1;
4156 id->vec_idx = 4;
4157 break;
4159 case DEFINE_PEEPHOLE:
4160 id->insn_code = insn_code_number++;
4161 id->insn_index = insn_index_number++;
4162 id->num_alternatives = count_alternatives (exp);
4163 if (id->num_alternatives == 0)
4164 id->num_alternatives = 1;
4165 id->vec_idx = 3;
4166 break;
4168 case DEFINE_ASM_ATTRIBUTES:
4169 id->insn_code = -1;
4170 id->insn_index = -1;
4171 id->num_alternatives = 1;
4172 id->vec_idx = 0;
4173 got_define_asm_attributes = 1;
4174 break;
4178 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
4179 true or annul false is specified, and make a `struct delay_desc'. */
4181 static void
4182 gen_delay (def)
4183 rtx def;
4185 struct delay_desc *delay;
4186 int i;
4188 if (XVECLEN (def, 1) % 3 != 0)
4189 fatal ("Number of elements in DEFINE_DELAY must be multiple of three.");
4191 for (i = 0; i < XVECLEN (def, 1); i += 3)
4193 if (XVECEXP (def, 1, i + 1))
4194 have_annul_true = 1;
4195 if (XVECEXP (def, 1, i + 2))
4196 have_annul_false = 1;
4199 delay = (struct delay_desc *) oballoc (sizeof (struct delay_desc));
4200 delay->def = def;
4201 delay->num = ++num_delays;
4202 delay->next = delays;
4203 delays = delay;
4206 /* Process a DEFINE_FUNCTION_UNIT.
4208 This gives information about a function unit contained in the CPU.
4209 We fill in a `struct function_unit_op' and a `struct function_unit'
4210 with information used later by `expand_unit'. */
4212 static void
4213 gen_unit (def)
4214 rtx def;
4216 struct function_unit *unit;
4217 struct function_unit_op *op;
4218 char *name = XSTR (def, 0);
4219 int multiplicity = XINT (def, 1);
4220 int simultaneity = XINT (def, 2);
4221 rtx condexp = XEXP (def, 3);
4222 int ready_cost = MAX (XINT (def, 4), 1);
4223 int issue_delay = MAX (XINT (def, 5), 1);
4225 /* See if we have already seen this function unit. If so, check that
4226 the multiplicity and simultaneity values are the same. If not, make
4227 a structure for this function unit. */
4228 for (unit = units; unit; unit = unit->next)
4229 if (! strcmp (unit->name, name))
4231 if (unit->multiplicity != multiplicity
4232 || unit->simultaneity != simultaneity)
4233 fatal ("Differing specifications given for `%s' function unit.",
4234 unit->name);
4235 break;
4238 if (unit == 0)
4240 unit = (struct function_unit *) oballoc (sizeof (struct function_unit));
4241 unit->name = name;
4242 unit->multiplicity = multiplicity;
4243 unit->simultaneity = simultaneity;
4244 unit->issue_delay.min = unit->issue_delay.max = issue_delay;
4245 unit->num = num_units++;
4246 unit->num_opclasses = 0;
4247 unit->condexp = false_rtx;
4248 unit->ops = 0;
4249 unit->next = units;
4250 units = unit;
4253 /* Make a new operation class structure entry and initialize it. */
4254 op = (struct function_unit_op *) oballoc (sizeof (struct function_unit_op));
4255 op->condexp = condexp;
4256 op->num = unit->num_opclasses++;
4257 op->ready = ready_cost;
4258 op->issue_delay = issue_delay;
4259 op->next = unit->ops;
4260 unit->ops = op;
4262 /* Set our issue expression based on whether or not an optional conflict
4263 vector was specified. */
4264 if (XVEC (def, 6))
4266 /* Compute the IOR of all the specified expressions. */
4267 rtx orexp = false_rtx;
4268 int i;
4270 for (i = 0; i < XVECLEN (def, 6); i++)
4271 orexp = insert_right_side (IOR, orexp, XVECEXP (def, 6, i), -2, -2);
4273 op->conflict_exp = orexp;
4274 extend_range (&unit->issue_delay, 1, issue_delay);
4276 else
4278 op->conflict_exp = true_rtx;
4279 extend_range (&unit->issue_delay, issue_delay, issue_delay);
4282 /* Merge our conditional into that of the function unit so we can determine
4283 which insns are used by the function unit. */
4284 unit->condexp = insert_right_side (IOR, unit->condexp, op->condexp, -2, -2);
4287 /* Given a piece of RTX, print a C expression to test it's truth value.
4288 We use AND and IOR both for logical and bit-wise operations, so
4289 interpret them as logical unless they are inside a comparison expression.
4290 The second operand of this function will be non-zero in that case. */
4292 static void
4293 write_test_expr (exp, in_comparison)
4294 rtx exp;
4295 int in_comparison;
4297 int comparison_operator = 0;
4298 RTX_CODE code;
4299 struct attr_desc *attr;
4301 /* In order not to worry about operator precedence, surround our part of
4302 the expression with parentheses. */
4304 printf ("(");
4305 code = GET_CODE (exp);
4306 switch (code)
4308 /* Binary operators. */
4309 case EQ: case NE:
4310 case GE: case GT: case GEU: case GTU:
4311 case LE: case LT: case LEU: case LTU:
4312 comparison_operator = 1;
4314 case PLUS: case MINUS: case MULT: case DIV: case MOD:
4315 case AND: case IOR: case XOR:
4316 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4317 write_test_expr (XEXP (exp, 0), in_comparison || comparison_operator);
4318 switch (code)
4320 case EQ:
4321 printf (" == ");
4322 break;
4323 case NE:
4324 printf (" != ");
4325 break;
4326 case GE:
4327 printf (" >= ");
4328 break;
4329 case GT:
4330 printf (" > ");
4331 break;
4332 case GEU:
4333 printf (" >= (unsigned) ");
4334 break;
4335 case GTU:
4336 printf (" > (unsigned) ");
4337 break;
4338 case LE:
4339 printf (" <= ");
4340 break;
4341 case LT:
4342 printf (" < ");
4343 break;
4344 case LEU:
4345 printf (" <= (unsigned) ");
4346 break;
4347 case LTU:
4348 printf (" < (unsigned) ");
4349 break;
4350 case PLUS:
4351 printf (" + ");
4352 break;
4353 case MINUS:
4354 printf (" - ");
4355 break;
4356 case MULT:
4357 printf (" * ");
4358 break;
4359 case DIV:
4360 printf (" / ");
4361 break;
4362 case MOD:
4363 printf (" %% ");
4364 break;
4365 case AND:
4366 if (in_comparison)
4367 printf (" & ");
4368 else
4369 printf (" && ");
4370 break;
4371 case IOR:
4372 if (in_comparison)
4373 printf (" | ");
4374 else
4375 printf (" || ");
4376 break;
4377 case XOR:
4378 printf (" ^ ");
4379 break;
4380 case ASHIFT:
4381 printf (" << ");
4382 break;
4383 case LSHIFTRT:
4384 case ASHIFTRT:
4385 printf (" >> ");
4386 break;
4389 write_test_expr (XEXP (exp, 1), in_comparison || comparison_operator);
4390 break;
4392 case NOT:
4393 /* Special-case (not (eq_attrq "alternative" "x")) */
4394 if (! in_comparison && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
4395 && XSTR (XEXP (exp, 0), 0) == alternative_name)
4397 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
4398 break;
4401 /* Otherwise, fall through to normal unary operator. */
4403 /* Unary operators. */
4404 case ABS: case NEG:
4405 switch (code)
4407 case NOT:
4408 if (in_comparison)
4409 printf ("~ ");
4410 else
4411 printf ("! ");
4412 break;
4413 case ABS:
4414 printf ("abs ");
4415 break;
4416 case NEG:
4417 printf ("-");
4418 break;
4421 write_test_expr (XEXP (exp, 0), in_comparison);
4422 break;
4424 /* Comparison test of an attribute with a value. Most of these will
4425 have been removed by optimization. Handle "alternative"
4426 specially and give error if EQ_ATTR present inside a comparison. */
4427 case EQ_ATTR:
4428 if (in_comparison)
4429 fatal ("EQ_ATTR not valid inside comparison");
4431 if (XSTR (exp, 0) == alternative_name)
4433 printf ("which_alternative == %s", XSTR (exp, 1));
4434 break;
4437 attr = find_attr (XSTR (exp, 0), 0);
4438 if (! attr) abort ();
4440 /* Now is the time to expand the value of a constant attribute. */
4441 if (attr->is_const)
4443 write_test_expr (evaluate_eq_attr (exp, attr->default_val->value,
4444 -2, -2),
4445 in_comparison);
4447 else
4449 printf ("get_attr_%s (insn) == ", attr->name);
4450 write_attr_valueq (attr, XSTR (exp, 1));
4452 break;
4454 /* Comparison test of flags for define_delays. */
4455 case ATTR_FLAG:
4456 if (in_comparison)
4457 fatal ("ATTR_FLAG not valid inside comparison");
4458 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
4459 break;
4461 /* See if an operand matches a predicate. */
4462 case MATCH_OPERAND:
4463 /* If only a mode is given, just ensure the mode matches the operand.
4464 If neither a mode nor predicate is given, error. */
4465 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
4467 if (GET_MODE (exp) == VOIDmode)
4468 fatal ("Null MATCH_OPERAND specified as test");
4469 else
4470 printf ("GET_MODE (operands[%d]) == %smode",
4471 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4473 else
4474 printf ("%s (operands[%d], %smode)",
4475 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
4476 break;
4478 /* Constant integer. */
4479 case CONST_INT:
4480 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
4481 printf ("%d", XWINT (exp, 0));
4482 #else
4483 printf ("%ld", XWINT (exp, 0));
4484 #endif
4485 break;
4487 /* A random C expression. */
4488 case SYMBOL_REF:
4489 printf ("%s", XSTR (exp, 0));
4490 break;
4492 /* The address of the branch target. */
4493 case MATCH_DUP:
4494 printf ("insn_addresses[INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])]",
4495 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
4496 break;
4498 /* The address of the current insn. It would be more consistent with
4499 other usage to make this the address of the NEXT insn, but this gets
4500 too confusing because of the ambiguity regarding the length of the
4501 current insn. */
4502 case PC:
4503 printf ("insn_current_address");
4504 break;
4506 default:
4507 fatal ("bad RTX code `%s' in attribute calculation\n",
4508 GET_RTX_NAME (code));
4511 printf (")");
4514 /* Given an attribute value, return the maximum CONST_STRING argument
4515 encountered. It is assumed that they are all numeric. */
4517 static int
4518 max_attr_value (exp)
4519 rtx exp;
4521 int current_max = 0;
4522 int n;
4523 int i;
4525 if (GET_CODE (exp) == CONST_STRING)
4526 return atoi (XSTR (exp, 0));
4528 else if (GET_CODE (exp) == COND)
4530 for (i = 0; i < XVECLEN (exp, 0); i += 2)
4532 n = max_attr_value (XVECEXP (exp, 0, i + 1));
4533 if (n > current_max)
4534 current_max = n;
4537 n = max_attr_value (XEXP (exp, 1));
4538 if (n > current_max)
4539 current_max = n;
4542 else if (GET_CODE (exp) == IF_THEN_ELSE)
4544 current_max = max_attr_value (XEXP (exp, 1));
4545 n = max_attr_value (XEXP (exp, 2));
4546 if (n > current_max)
4547 current_max = n;
4550 else
4551 abort ();
4553 return current_max;
4556 /* Scan an attribute value, possibly a conditional, and record what actions
4557 will be required to do any conditional tests in it.
4559 Specifically, set
4560 `must_extract' if we need to extract the insn operands
4561 `must_constrain' if we must compute `which_alternative'
4562 `address_used' if an address expression was used
4563 `length_used' if an (eq_attr "length" ...) was used
4566 static void
4567 walk_attr_value (exp)
4568 rtx exp;
4570 register int i, j;
4571 register char *fmt;
4572 RTX_CODE code;
4574 if (exp == NULL)
4575 return;
4577 code = GET_CODE (exp);
4578 switch (code)
4580 case SYMBOL_REF:
4581 if (! RTX_UNCHANGING_P (exp))
4582 /* Since this is an arbitrary expression, it can look at anything.
4583 However, constant expressions do not depend on any particular
4584 insn. */
4585 must_extract = must_constrain = 1;
4586 return;
4588 case MATCH_OPERAND:
4589 must_extract = 1;
4590 return;
4592 case EQ_ATTR:
4593 if (XSTR (exp, 0) == alternative_name)
4594 must_extract = must_constrain = 1;
4595 else if (strcmp (XSTR (exp, 0), "length") == 0)
4596 length_used = 1;
4597 return;
4599 case MATCH_DUP:
4600 must_extract = 1;
4601 address_used = 1;
4602 return;
4604 case PC:
4605 address_used = 1;
4606 return;
4608 case ATTR_FLAG:
4609 return;
4612 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
4613 switch (*fmt++)
4615 case 'e':
4616 case 'u':
4617 walk_attr_value (XEXP (exp, i));
4618 break;
4620 case 'E':
4621 if (XVEC (exp, i) != NULL)
4622 for (j = 0; j < XVECLEN (exp, i); j++)
4623 walk_attr_value (XVECEXP (exp, i, j));
4624 break;
4628 /* Write out a function to obtain the attribute for a given INSN. */
4630 static void
4631 write_attr_get (attr)
4632 struct attr_desc *attr;
4634 struct attr_value *av, *common_av;
4636 /* Find the most used attribute value. Handle that as the `default' of the
4637 switch we will generate. */
4638 common_av = find_most_used (attr);
4640 /* Write out start of function, then all values with explicit `case' lines,
4641 then a `default', then the value with the most uses. */
4642 if (!attr->is_numeric)
4643 printf ("enum attr_%s\n", attr->name);
4644 else if (attr->unsigned_p)
4645 printf ("unsigned int\n");
4646 else
4647 printf ("int\n");
4649 /* If the attribute name starts with a star, the remainder is the name of
4650 the subroutine to use, instead of `get_attr_...'. */
4651 if (attr->name[0] == '*')
4652 printf ("%s (insn)\n", &attr->name[1]);
4653 else if (attr->is_const == 0)
4654 printf ("get_attr_%s (insn)\n", attr->name);
4655 else
4657 printf ("get_attr_%s ()\n", attr->name);
4658 printf ("{\n");
4660 for (av = attr->first_value; av; av = av->next)
4661 if (av->num_insns != 0)
4662 write_attr_set (attr, 2, av->value, "return", ";",
4663 true_rtx, av->first_insn->insn_code,
4664 av->first_insn->insn_index);
4666 printf ("}\n\n");
4667 return;
4669 printf (" rtx insn;\n");
4670 printf ("{\n");
4671 printf (" switch (recog_memoized (insn))\n");
4672 printf (" {\n");
4674 for (av = attr->first_value; av; av = av->next)
4675 if (av != common_av)
4676 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4678 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4679 printf (" }\n}\n\n");
4682 /* Given an AND tree of known true terms (because we are inside an `if' with
4683 that as the condition or are in an `else' clause) and an expression,
4684 replace any known true terms with TRUE. Use `simplify_and_tree' to do
4685 the bulk of the work. */
4687 static rtx
4688 eliminate_known_true (known_true, exp, insn_code, insn_index)
4689 rtx known_true;
4690 rtx exp;
4691 int insn_code, insn_index;
4693 rtx term;
4695 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
4697 if (GET_CODE (known_true) == AND)
4699 exp = eliminate_known_true (XEXP (known_true, 0), exp,
4700 insn_code, insn_index);
4701 exp = eliminate_known_true (XEXP (known_true, 1), exp,
4702 insn_code, insn_index);
4704 else
4706 term = known_true;
4707 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
4710 return exp;
4713 /* Write out a series of tests and assignment statements to perform tests and
4714 sets of an attribute value. We are passed an indentation amount and prefix
4715 and suffix strings to write around each attribute value (e.g., "return"
4716 and ";"). */
4718 static void
4719 write_attr_set (attr, indent, value, prefix, suffix, known_true,
4720 insn_code, insn_index)
4721 struct attr_desc *attr;
4722 int indent;
4723 rtx value;
4724 char *prefix;
4725 char *suffix;
4726 rtx known_true;
4727 int insn_code, insn_index;
4729 if (GET_CODE (value) == CONST_STRING)
4731 write_indent (indent);
4732 printf ("%s ", prefix);
4733 write_attr_value (attr, value);
4734 printf ("%s\n", suffix);
4736 else if (GET_CODE (value) == COND)
4738 /* Assume the default value will be the default of the COND unless we
4739 find an always true expression. */
4740 rtx default_val = XEXP (value, 1);
4741 rtx our_known_true = known_true;
4742 rtx newexp;
4743 int first_if = 1;
4744 int i;
4746 for (i = 0; i < XVECLEN (value, 0); i += 2)
4748 rtx testexp;
4749 rtx inner_true;
4751 testexp = eliminate_known_true (our_known_true,
4752 XVECEXP (value, 0, i),
4753 insn_code, insn_index);
4754 newexp = attr_rtx (NOT, testexp);
4755 newexp = insert_right_side (AND, our_known_true, newexp,
4756 insn_code, insn_index);
4758 /* If the test expression is always true or if the next `known_true'
4759 expression is always false, this is the last case, so break
4760 out and let this value be the `else' case. */
4761 if (testexp == true_rtx || newexp == false_rtx)
4763 default_val = XVECEXP (value, 0, i + 1);
4764 break;
4767 /* Compute the expression to pass to our recursive call as being
4768 known true. */
4769 inner_true = insert_right_side (AND, our_known_true,
4770 testexp, insn_code, insn_index);
4772 /* If this is always false, skip it. */
4773 if (inner_true == false_rtx)
4774 continue;
4776 write_indent (indent);
4777 printf ("%sif ", first_if ? "" : "else ");
4778 first_if = 0;
4779 write_test_expr (testexp, 0);
4780 printf ("\n");
4781 write_indent (indent + 2);
4782 printf ("{\n");
4784 write_attr_set (attr, indent + 4,
4785 XVECEXP (value, 0, i + 1), prefix, suffix,
4786 inner_true, insn_code, insn_index);
4787 write_indent (indent + 2);
4788 printf ("}\n");
4789 our_known_true = newexp;
4792 if (! first_if)
4794 write_indent (indent);
4795 printf ("else\n");
4796 write_indent (indent + 2);
4797 printf ("{\n");
4800 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
4801 prefix, suffix, our_known_true, insn_code, insn_index);
4803 if (! first_if)
4805 write_indent (indent + 2);
4806 printf ("}\n");
4809 else
4810 abort ();
4813 /* Write out the computation for one attribute value. */
4815 static void
4816 write_attr_case (attr, av, write_case_lines, prefix, suffix, indent,
4817 known_true)
4818 struct attr_desc *attr;
4819 struct attr_value *av;
4820 int write_case_lines;
4821 char *prefix, *suffix;
4822 int indent;
4823 rtx known_true;
4825 struct insn_ent *ie;
4827 if (av->num_insns == 0)
4828 return;
4830 if (av->has_asm_insn)
4832 write_indent (indent);
4833 printf ("case -1:\n");
4834 write_indent (indent + 2);
4835 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
4836 write_indent (indent + 2);
4837 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
4838 write_indent (indent + 2);
4839 printf (" fatal_insn_not_found (insn);\n");
4842 if (write_case_lines)
4844 for (ie = av->first_insn; ie; ie = ie->next)
4845 if (ie->insn_code != -1)
4847 write_indent (indent);
4848 printf ("case %d:\n", ie->insn_code);
4851 else
4853 write_indent (indent);
4854 printf ("default:\n");
4857 /* See what we have to do to output this value. */
4858 must_extract = must_constrain = address_used = 0;
4859 walk_attr_value (av->value);
4861 if (must_extract)
4863 write_indent (indent + 2);
4864 printf ("insn_extract (insn);\n");
4867 if (must_constrain)
4869 #ifdef REGISTER_CONSTRAINTS
4870 write_indent (indent + 2);
4871 printf ("if (! constrain_operands (INSN_CODE (insn), reload_completed))\n");
4872 write_indent (indent + 2);
4873 printf (" fatal_insn_not_found (insn);\n");
4874 #endif
4877 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
4878 known_true, av->first_insn->insn_code,
4879 av->first_insn->insn_index);
4881 if (strncmp (prefix, "return", 6))
4883 write_indent (indent + 2);
4884 printf ("break;\n");
4886 printf ("\n");
4889 /* Utilities to write names in various forms. */
4891 static void
4892 write_attr_valueq (attr, s)
4893 struct attr_desc *attr;
4894 char *s;
4896 if (attr->is_numeric)
4898 printf ("%s", s);
4899 /* Make the blockage range values easier to read. */
4900 if (strlen (s) > 1)
4901 printf (" /* 0x%x */", atoi (s));
4903 else
4905 write_upcase (attr->name);
4906 printf ("_");
4907 write_upcase (s);
4911 static void
4912 write_attr_value (attr, value)
4913 struct attr_desc *attr;
4914 rtx value;
4916 if (GET_CODE (value) != CONST_STRING)
4917 abort ();
4919 write_attr_valueq (attr, XSTR (value, 0));
4922 static void
4923 write_upcase (str)
4924 char *str;
4926 while (*str)
4927 if (*str < 'a' || *str > 'z')
4928 printf ("%c", *str++);
4929 else
4930 printf ("%c", *str++ - 'a' + 'A');
4933 static void
4934 write_indent (indent)
4935 int indent;
4937 for (; indent > 8; indent -= 8)
4938 printf ("\t");
4940 for (; indent; indent--)
4941 printf (" ");
4944 /* Write a subroutine that is given an insn that requires a delay slot, a
4945 delay slot ordinal, and a candidate insn. It returns non-zero if the
4946 candidate can be placed in the specified delay slot of the insn.
4948 We can write as many as three subroutines. `eligible_for_delay'
4949 handles normal delay slots, `eligible_for_annul_true' indicates that
4950 the specified insn can be annulled if the branch is true, and likewise
4951 for `eligible_for_annul_false'.
4953 KIND is a string distinguishing these three cases ("delay", "annul_true",
4954 or "annul_false"). */
4956 static void
4957 write_eligible_delay (kind)
4958 char *kind;
4960 struct delay_desc *delay;
4961 int max_slots;
4962 char str[50];
4963 struct attr_desc *attr;
4964 struct attr_value *av, *common_av;
4965 int i;
4967 /* Compute the maximum number of delay slots required. We use the delay
4968 ordinal times this number plus one, plus the slot number as an index into
4969 the appropriate predicate to test. */
4971 for (delay = delays, max_slots = 0; delay; delay = delay->next)
4972 if (XVECLEN (delay->def, 1) / 3 > max_slots)
4973 max_slots = XVECLEN (delay->def, 1) / 3;
4975 /* Write function prelude. */
4977 printf ("int\n");
4978 printf ("eligible_for_%s (delay_insn, slot, candidate_insn, flags)\n",
4979 kind);
4980 printf (" rtx delay_insn;\n");
4981 printf (" int slot;\n");
4982 printf (" rtx candidate_insn;\n");
4983 printf (" int flags;\n");
4984 printf ("{\n");
4985 printf (" rtx insn;\n");
4986 printf ("\n");
4987 printf (" if (slot >= %d)\n", max_slots);
4988 printf (" abort ();\n");
4989 printf ("\n");
4991 /* If more than one delay type, find out which type the delay insn is. */
4993 if (num_delays > 1)
4995 attr = find_attr ("*delay_type", 0);
4996 if (! attr) abort ();
4997 common_av = find_most_used (attr);
4999 printf (" insn = delay_insn;\n");
5000 printf (" switch (recog_memoized (insn))\n");
5001 printf (" {\n");
5003 sprintf (str, " * %d;\n break;", max_slots);
5004 for (av = attr->first_value; av; av = av->next)
5005 if (av != common_av)
5006 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
5008 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
5009 printf (" }\n\n");
5011 /* Ensure matched. Otherwise, shouldn't have been called. */
5012 printf (" if (slot < %d)\n", max_slots);
5013 printf (" abort ();\n\n");
5016 /* If just one type of delay slot, write simple switch. */
5017 if (num_delays == 1 && max_slots == 1)
5019 printf (" insn = candidate_insn;\n");
5020 printf (" switch (recog_memoized (insn))\n");
5021 printf (" {\n");
5023 attr = find_attr ("*delay_1_0", 0);
5024 if (! attr) abort ();
5025 common_av = find_most_used (attr);
5027 for (av = attr->first_value; av; av = av->next)
5028 if (av != common_av)
5029 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
5031 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
5032 printf (" }\n");
5035 else
5037 /* Write a nested CASE. The first indicates which condition we need to
5038 test, and the inner CASE tests the condition. */
5039 printf (" insn = candidate_insn;\n");
5040 printf (" switch (slot)\n");
5041 printf (" {\n");
5043 for (delay = delays; delay; delay = delay->next)
5044 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
5046 printf (" case %d:\n",
5047 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
5048 printf (" switch (recog_memoized (insn))\n");
5049 printf ("\t{\n");
5051 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
5052 attr = find_attr (str, 0);
5053 if (! attr) abort ();
5054 common_av = find_most_used (attr);
5056 for (av = attr->first_value; av; av = av->next)
5057 if (av != common_av)
5058 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
5060 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
5061 printf (" }\n");
5064 printf (" default:\n");
5065 printf (" abort ();\n");
5066 printf (" }\n");
5069 printf ("}\n\n");
5072 /* Write routines to compute conflict cost for function units. Then write a
5073 table describing the available function units. */
5075 static void
5076 write_function_unit_info ()
5078 struct function_unit *unit;
5079 int i;
5081 /* Write out conflict routines for function units. Don't bother writing
5082 one if there is only one issue delay value. */
5084 for (unit = units; unit; unit = unit->next)
5086 if (unit->needs_blockage_function)
5087 write_complex_function (unit, "blockage", "block");
5089 /* If the minimum and maximum conflict costs are the same, there
5090 is only one value, so we don't need a function. */
5091 if (! unit->needs_conflict_function)
5093 unit->default_cost = make_numeric_value (unit->issue_delay.max);
5094 continue;
5097 /* The function first computes the case from the candidate insn. */
5098 unit->default_cost = make_numeric_value (0);
5099 write_complex_function (unit, "conflict_cost", "cost");
5102 /* Now that all functions have been written, write the table describing
5103 the function units. The name is included for documentation purposes
5104 only. */
5106 printf ("struct function_unit_desc function_units[] = {\n");
5108 /* Write out the descriptions in numeric order, but don't force that order
5109 on the list. Doing so increases the runtime of genattrtab.c. */
5110 for (i = 0; i < num_units; i++)
5112 for (unit = units; unit; unit = unit->next)
5113 if (unit->num == i)
5114 break;
5116 printf (" {\"%s\", %d, %d, %d, %s, %d, %s_unit_ready_cost, ",
5117 unit->name, 1 << unit->num, unit->multiplicity,
5118 unit->simultaneity, XSTR (unit->default_cost, 0),
5119 unit->issue_delay.max, unit->name);
5121 if (unit->needs_conflict_function)
5122 printf ("%s_unit_conflict_cost, ", unit->name);
5123 else
5124 printf ("0, ");
5126 printf ("%d, ", unit->max_blockage);
5128 if (unit->needs_range_function)
5129 printf ("%s_unit_blockage_range, ", unit->name);
5130 else
5131 printf ("0, ");
5133 if (unit->needs_blockage_function)
5134 printf ("%s_unit_blockage", unit->name);
5135 else
5136 printf ("0");
5138 printf ("}, \n");
5141 printf ("};\n\n");
5144 static void
5145 write_complex_function (unit, name, connection)
5146 struct function_unit *unit;
5147 char *name, *connection;
5149 struct attr_desc *case_attr, *attr;
5150 struct attr_value *av, *common_av;
5151 rtx value;
5152 char *str;
5153 int using_case;
5154 int i;
5156 printf ("static int\n");
5157 printf ("%s_unit_%s (executing_insn, candidate_insn)\n",
5158 unit->name, name);
5159 printf (" rtx executing_insn;\n");
5160 printf (" rtx candidate_insn;\n");
5161 printf ("{\n");
5162 printf (" rtx insn;\n");
5163 printf (" int casenum;\n\n");
5164 printf (" insn = executing_insn;\n");
5165 printf (" switch (recog_memoized (insn))\n");
5166 printf (" {\n");
5168 /* Write the `switch' statement to get the case value. */
5169 str = (char *) alloca (strlen (unit->name) + strlen (name) + strlen (connection) + 10);
5170 sprintf (str, "*%s_cases", unit->name);
5171 case_attr = find_attr (str, 0);
5172 if (! case_attr) abort ();
5173 common_av = find_most_used (case_attr);
5175 for (av = case_attr->first_value; av; av = av->next)
5176 if (av != common_av)
5177 write_attr_case (case_attr, av, 1,
5178 "casenum =", ";", 4, unit->condexp);
5180 write_attr_case (case_attr, common_av, 0,
5181 "casenum =", ";", 4, unit->condexp);
5182 printf (" }\n\n");
5184 /* Now write an outer switch statement on each case. Then write
5185 the tests on the executing function within each. */
5186 printf (" insn = candidate_insn;\n");
5187 printf (" switch (casenum)\n");
5188 printf (" {\n");
5190 for (i = 0; i < unit->num_opclasses; i++)
5192 /* Ensure using this case. */
5193 using_case = 0;
5194 for (av = case_attr->first_value; av; av = av->next)
5195 if (av->num_insns
5196 && contained_in_p (make_numeric_value (i), av->value))
5197 using_case = 1;
5199 if (! using_case)
5200 continue;
5202 printf (" case %d:\n", i);
5203 sprintf (str, "*%s_%s_%d", unit->name, connection, i);
5204 attr = find_attr (str, 0);
5205 if (! attr) abort ();
5207 /* If single value, just write it. */
5208 value = find_single_value (attr);
5209 if (value)
5210 write_attr_set (attr, 6, value, "return", ";\n", true_rtx, -2, -2);
5211 else
5213 common_av = find_most_used (attr);
5214 printf (" switch (recog_memoized (insn))\n");
5215 printf ("\t{\n");
5217 for (av = attr->first_value; av; av = av->next)
5218 if (av != common_av)
5219 write_attr_case (attr, av, 1,
5220 "return", ";", 8, unit->condexp);
5222 write_attr_case (attr, common_av, 0,
5223 "return", ";", 8, unit->condexp);
5224 printf (" }\n\n");
5228 printf (" }\n}\n\n");
5231 /* This page contains miscellaneous utility routines. */
5233 /* Given a string, return the number of comma-separated elements in it.
5234 Return 0 for the null string. */
5236 static int
5237 n_comma_elts (s)
5238 char *s;
5240 int n;
5242 if (*s == '\0')
5243 return 0;
5245 for (n = 1; *s; s++)
5246 if (*s == ',')
5247 n++;
5249 return n;
5252 /* Given a pointer to a (char *), return a malloc'ed string containing the
5253 next comma-separated element. Advance the pointer to after the string
5254 scanned, or the end-of-string. Return NULL if at end of string. */
5256 static char *
5257 next_comma_elt (pstr)
5258 char **pstr;
5260 char *out_str;
5261 char *p;
5263 if (**pstr == '\0')
5264 return NULL;
5266 /* Find end of string to compute length. */
5267 for (p = *pstr; *p != ',' && *p != '\0'; p++)
5270 out_str = attr_string (*pstr, p - *pstr);
5271 *pstr = p;
5273 if (**pstr == ',')
5274 (*pstr)++;
5276 return out_str;
5279 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
5280 is non-zero, build a new attribute, if one does not exist. */
5282 static struct attr_desc *
5283 find_attr (name, create)
5284 char *name;
5285 int create;
5287 struct attr_desc *attr;
5288 int index;
5290 /* Before we resort to using `strcmp', see if the string address matches
5291 anywhere. In most cases, it should have been canonicalized to do so. */
5292 if (name == alternative_name)
5293 return NULL;
5295 index = name[0] & (MAX_ATTRS_INDEX - 1);
5296 for (attr = attrs[index]; attr; attr = attr->next)
5297 if (name == attr->name)
5298 return attr;
5300 /* Otherwise, do it the slow way. */
5301 for (attr = attrs[index]; attr; attr = attr->next)
5302 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
5303 return attr;
5305 if (! create)
5306 return NULL;
5308 attr = (struct attr_desc *) oballoc (sizeof (struct attr_desc));
5309 attr->name = attr_string (name, strlen (name));
5310 attr->first_value = attr->default_val = NULL;
5311 attr->is_numeric = attr->negative_ok = attr->is_const = attr->is_special = 0;
5312 attr->next = attrs[index];
5313 attrs[index] = attr;
5315 return attr;
5318 /* Create internal attribute with the given default value. */
5320 static void
5321 make_internal_attr (name, value, special)
5322 char *name;
5323 rtx value;
5324 int special;
5326 struct attr_desc *attr;
5328 attr = find_attr (name, 1);
5329 if (attr->default_val)
5330 abort ();
5332 attr->is_numeric = 1;
5333 attr->is_const = 0;
5334 attr->is_special = (special & 1) != 0;
5335 attr->negative_ok = (special & 2) != 0;
5336 attr->unsigned_p = (special & 4) != 0;
5337 attr->default_val = get_attr_value (value, attr, -2);
5340 /* Find the most used value of an attribute. */
5342 static struct attr_value *
5343 find_most_used (attr)
5344 struct attr_desc *attr;
5346 struct attr_value *av;
5347 struct attr_value *most_used;
5348 int nuses;
5350 most_used = NULL;
5351 nuses = -1;
5353 for (av = attr->first_value; av; av = av->next)
5354 if (av->num_insns > nuses)
5355 nuses = av->num_insns, most_used = av;
5357 return most_used;
5360 /* If an attribute only has a single value used, return it. Otherwise
5361 return NULL. */
5363 static rtx
5364 find_single_value (attr)
5365 struct attr_desc *attr;
5367 struct attr_value *av;
5368 rtx unique_value;
5370 unique_value = NULL;
5371 for (av = attr->first_value; av; av = av->next)
5372 if (av->num_insns)
5374 if (unique_value)
5375 return NULL;
5376 else
5377 unique_value = av->value;
5380 return unique_value;
5383 /* Return (attr_value "n") */
5385 static rtx
5386 make_numeric_value (n)
5387 int n;
5389 static rtx int_values[20];
5390 rtx exp;
5391 char *p;
5393 if (n < 0)
5394 abort ();
5396 if (n < 20 && int_values[n])
5397 return int_values[n];
5399 p = attr_printf (MAX_DIGITS, "%d", n);
5400 exp = attr_rtx (CONST_STRING, p);
5402 if (n < 20)
5403 int_values[n] = exp;
5405 return exp;
5408 static void
5409 extend_range (range, min, max)
5410 struct range *range;
5411 int min;
5412 int max;
5414 if (range->min > min) range->min = min;
5415 if (range->max < max) range->max = max;
5418 char *
5419 xrealloc (ptr, size)
5420 char *ptr;
5421 unsigned size;
5423 char *result = (char *) realloc (ptr, size);
5424 if (!result)
5425 fatal ("virtual memory exhausted");
5426 return result;
5429 char *
5430 xmalloc (size)
5431 unsigned size;
5433 register char *val = (char *) malloc (size);
5435 if (val == 0)
5436 fatal ("virtual memory exhausted");
5437 return val;
5440 static rtx
5441 copy_rtx_unchanging (orig)
5442 register rtx orig;
5444 #if 0
5445 register rtx copy;
5446 register RTX_CODE code;
5447 #endif
5449 if (RTX_UNCHANGING_P (orig) || MEM_IN_STRUCT_P (orig))
5450 return orig;
5452 MEM_IN_STRUCT_P (orig) = 1;
5453 return orig;
5455 #if 0
5456 code = GET_CODE (orig);
5457 switch (code)
5459 case CONST_INT:
5460 case CONST_DOUBLE:
5461 case SYMBOL_REF:
5462 case CODE_LABEL:
5463 return orig;
5466 copy = rtx_alloc (code);
5467 PUT_MODE (copy, GET_MODE (orig));
5468 RTX_UNCHANGING_P (copy) = 1;
5470 bcopy ((char *) &XEXP (orig, 0), (char *) &XEXP (copy, 0),
5471 GET_RTX_LENGTH (GET_CODE (copy)) * sizeof (rtx));
5472 return copy;
5473 #endif
5476 static void
5477 fatal (s, a1, a2)
5478 char *s;
5479 char *a1, *a2;
5481 fprintf (stderr, "genattrtab: ");
5482 fprintf (stderr, s, a1, a2);
5483 fprintf (stderr, "\n");
5484 exit (FATAL_EXIT_CODE);
5487 /* More 'friendly' abort that prints the line and file.
5488 config.h can #define abort fancy_abort if you like that sort of thing. */
5490 void
5491 fancy_abort ()
5493 fatal ("Internal gcc abort.");
5496 /* Determine if an insn has a constant number of delay slots, i.e., the
5497 number of delay slots is not a function of the length of the insn. */
5499 void
5500 write_const_num_delay_slots ()
5502 struct attr_desc *attr = find_attr ("*num_delay_slots", 0);
5503 struct attr_value *av;
5504 struct insn_ent *ie;
5505 int i;
5507 if (attr)
5509 printf ("int\nconst_num_delay_slots (insn)\n");
5510 printf (" rtx insn;\n");
5511 printf ("{\n");
5512 printf (" switch (recog_memoized (insn))\n");
5513 printf (" {\n");
5515 for (av = attr->first_value; av; av = av->next)
5517 length_used = 0;
5518 walk_attr_value (av->value);
5519 if (length_used)
5521 for (ie = av->first_insn; ie; ie = ie->next)
5522 if (ie->insn_code != -1)
5523 printf (" case %d:\n", ie->insn_code);
5524 printf (" return 0;\n");
5528 printf (" default:\n");
5529 printf (" return 1;\n");
5530 printf (" }\n}\n");
5536 main (argc, argv)
5537 int argc;
5538 char **argv;
5540 rtx desc;
5541 FILE *infile;
5542 register int c;
5543 struct attr_desc *attr;
5544 struct insn_def *id;
5545 rtx tem;
5546 int i;
5548 #ifdef RLIMIT_STACK
5549 /* Get rid of any avoidable limit on stack size. */
5551 struct rlimit rlim;
5553 /* Set the stack limit huge so that alloca does not fail. */
5554 getrlimit (RLIMIT_STACK, &rlim);
5555 rlim.rlim_cur = rlim.rlim_max;
5556 setrlimit (RLIMIT_STACK, &rlim);
5558 #endif /* RLIMIT_STACK defined */
5560 obstack_init (rtl_obstack);
5561 obstack_init (hash_obstack);
5562 obstack_init (temp_obstack);
5564 if (argc <= 1)
5565 fatal ("No input file name.");
5567 infile = fopen (argv[1], "r");
5568 if (infile == 0)
5570 perror (argv[1]);
5571 exit (FATAL_EXIT_CODE);
5574 init_rtl ();
5576 /* Set up true and false rtx's */
5577 true_rtx = rtx_alloc (CONST_INT);
5578 XWINT (true_rtx, 0) = 1;
5579 false_rtx = rtx_alloc (CONST_INT);
5580 XWINT (false_rtx, 0) = 0;
5581 RTX_UNCHANGING_P (true_rtx) = RTX_UNCHANGING_P (false_rtx) = 1;
5582 RTX_INTEGRATED_P (true_rtx) = RTX_INTEGRATED_P (false_rtx) = 1;
5584 alternative_name = attr_string ("alternative", strlen ("alternative"));
5586 printf ("/* Generated automatically by the program `genattrtab'\n\
5587 from the machine description file `md'. */\n\n");
5589 /* Read the machine description. */
5591 while (1)
5593 c = read_skip_spaces (infile);
5594 if (c == EOF)
5595 break;
5596 ungetc (c, infile);
5598 desc = read_rtx (infile);
5599 if (GET_CODE (desc) == DEFINE_INSN
5600 || GET_CODE (desc) == DEFINE_PEEPHOLE
5601 || GET_CODE (desc) == DEFINE_ASM_ATTRIBUTES)
5602 gen_insn (desc);
5604 else if (GET_CODE (desc) == DEFINE_EXPAND)
5605 insn_code_number++, insn_index_number++;
5607 else if (GET_CODE (desc) == DEFINE_SPLIT)
5608 insn_code_number++, insn_index_number++;
5610 else if (GET_CODE (desc) == DEFINE_ATTR)
5612 gen_attr (desc);
5613 insn_index_number++;
5616 else if (GET_CODE (desc) == DEFINE_DELAY)
5618 gen_delay (desc);
5619 insn_index_number++;
5622 else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
5624 gen_unit (desc);
5625 insn_index_number++;
5629 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
5630 if (! got_define_asm_attributes)
5632 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
5633 XVEC (tem, 0) = rtvec_alloc (0);
5634 gen_insn (tem);
5637 /* Expand DEFINE_DELAY information into new attribute. */
5638 if (num_delays)
5639 expand_delays ();
5641 /* Expand DEFINE_FUNCTION_UNIT information into new attributes. */
5642 if (num_units)
5643 expand_units ();
5645 printf ("#include \"config.h\"\n");
5646 printf ("#include \"rtl.h\"\n");
5647 printf ("#include \"insn-config.h\"\n");
5648 printf ("#include \"recog.h\"\n");
5649 printf ("#include \"regs.h\"\n");
5650 printf ("#include \"real.h\"\n");
5651 printf ("#include \"output.h\"\n");
5652 printf ("#include \"insn-attr.h\"\n");
5653 printf ("\n");
5654 printf ("#define operands recog_operand\n\n");
5656 /* Make `insn_alternatives'. */
5657 insn_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
5658 for (id = defs; id; id = id->next)
5659 if (id->insn_code >= 0)
5660 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
5662 /* Make `insn_n_alternatives'. */
5663 insn_n_alternatives = (int *) oballoc (insn_code_number * sizeof (int));
5664 for (id = defs; id; id = id->next)
5665 if (id->insn_code >= 0)
5666 insn_n_alternatives[id->insn_code] = id->num_alternatives;
5668 /* Prepare to write out attribute subroutines by checking everything stored
5669 away and building the attribute cases. */
5671 check_defs ();
5672 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5673 for (attr = attrs[i]; attr; attr = attr->next)
5675 attr->default_val->value
5676 = check_attr_value (attr->default_val->value, attr);
5677 fill_attr (attr);
5680 /* Construct extra attributes for `length'. */
5681 make_length_attrs ();
5683 /* Perform any possible optimizations to speed up compilation. */
5684 optimize_attrs ();
5686 /* Now write out all the `gen_attr_...' routines. Do these before the
5687 special routines (specifically before write_function_unit_info), so
5688 that they get defined before they are used. */
5690 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5691 for (attr = attrs[i]; attr; attr = attr->next)
5693 if (! attr->is_special)
5694 write_attr_get (attr);
5697 /* Write out delay eligibility information, if DEFINE_DELAY present.
5698 (The function to compute the number of delay slots will be written
5699 below.) */
5700 if (num_delays)
5702 write_eligible_delay ("delay");
5703 if (have_annul_true)
5704 write_eligible_delay ("annul_true");
5705 if (have_annul_false)
5706 write_eligible_delay ("annul_false");
5709 /* Write out information about function units. */
5710 if (num_units)
5711 write_function_unit_info ();
5713 /* Write out constant delay slot info */
5714 write_const_num_delay_slots ();
5716 fflush (stdout);
5717 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
5718 /* NOTREACHED */
5719 return 0;