1 /* Generate code from machine description to compute values of attributes.
2 Copyright (C) 1991 Free Software Foundation, Inc.
3 Contributed by Richard Kenner (kenner@nyu.edu)
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)
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This program handles insn attribues and the DEFINE_DELAY and
22 DEFINE_FUNCTION_UNIT definitions.
24 It produces a series of functions named `get_attr_...', one for each insn
25 attribute. Each of these is given the rtx for an insn and returns a member
26 of the enum for the attribute.
28 These subroutines have the form of a `switch' on the INSN_CODE (via
29 `recog_memoized'). Each case either returns a constant attribute value
30 or a value that depends on tests on other attributes, the form of
31 operands, or some random C expression (encoded with a SYMBOL_REF
34 If the attribute `alternative', or a random C expression is present,
35 `constrain_operands' is called. If either of these cases of a reference to
36 an operand is found, `insn_extract' is called.
38 The special attribute `length' is also recognized. For this operand,
39 expressions involving the address of an operand or the current insn,
40 (address (pc)), are valid. In this case, an initial pass is made to
41 set all lengths that do not depend on address. Those that do are set to
42 the maximum length. Then each insn that depends on an address is checked
43 and possibly has its length changed. The process repeats until no further
44 changed are made. The resulting lengths are saved for use by
47 A special form of DEFINE_ATTR, where the expression for default value is a
48 CONST expression, indicates an attribute that is constant for a given run
49 of the compiler. The subroutine generated for these attributes has no
50 parameters as it does not depend on any particular insn. Constant
51 attributes are typically used to specify which variety of processor is
54 Internal attributes are defined to handle DEFINE_DELAY and
55 DEFINE_FUNCTION_UNIT. Special routines are output for these cases.
57 This program works by keeping a list of possible values for each attribute.
58 These include the basic attribute choices, default values for attribute, and
59 all derived quantities.
61 As the description file is read, the definition for each insn is saved in a
62 `struct insn_def'. When the file reading is complete, a `struct insn_ent'
63 is created for each insn and chained to the corresponding attribute value,
64 either that specified, or the default.
66 An optimization phase is then run. This simplifies expressions for each
67 insn. EQ_ATTR tests are resolved, whenever possible, to a test that
68 indicates when the attribute has the specified value for the insn. This
69 avoids recursive calls during compilation.
71 The strategy used when processing DEFINE_DELAY and DEFINE_FUNCTION_UNIT
72 definitions is to create arbitrarily complex expressions and have the
73 optimization simplify them.
75 Once optimization is complete, any required routines and definitions
78 An optimization that is not yet implemented is to hoist the constant
79 expressions entirely out of the routines and definitions that are written.
80 A way to do this is to iterate over all possible combinations of values
81 for constant attributes and generate a set of functions for that given
82 combination. An initialization function would be written that evaluates
83 the attributes and installs the corresponding set of routines and
84 definitions (each would be accessed through a pointer). */
91 #include "insn-config.h" /* For REGISTER_CONSTRAINTS */
93 static struct obstack obstack;
94 struct obstack *rtl_obstack = &obstack;
96 #define obstack_chunk_alloc xmalloc
97 #define obstack_chunk_free free
101 static void fatal ();
104 /* Define structures used to record attributes and values. */
106 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
107 encountered, we store all the relevant information into a
108 `struct insn_def'. This is done to allow attribute definitions to occur
109 anywhere in the file. */
113 int insn_code; /* Instruction number. */
114 int insn_index; /* Expression numer in file, for errors. */
115 struct insn_def *next; /* Next insn in chain. */
116 rtx def; /* The DEFINE_... */
117 int num_alternatives; /* Number of alternatives. */
118 int vec_idx; /* Index of attribute vector in `def'. */
121 /* Once everything has been read in, we store in each attribute value a list
122 of insn codes that have that value. Here is the structure used for the
127 int insn_code; /* Instruction number. */
128 int insn_index; /* Index of definition in file */
129 struct insn_ent *next; /* Next in chain. */
132 /* Each value of an attribute (either constant or computed) is assigned a
133 structure which is used as the listhead of the insns that have that
138 rtx value; /* Value of attribute. */
139 struct attr_value *next; /* Next attribute value in chain. */
140 struct insn_ent *first_insn; /* First insn with this value. */
141 int num_insns; /* Number of insns with this value. */
142 int has_asm_insn; /* True if this value used for `asm' insns */
145 /* Structure for each attribute. */
149 char *name; /* Name of attribute. */
150 struct attr_desc *next; /* Next attribute. */
151 int is_numeric; /* Values of this attribute are numeric. */
152 int is_const; /* Attribute value constant for each run. */
153 int is_special; /* Don't call `write_attr_set'. */
154 struct attr_value *first_value; /* First value of this attribute. */
155 struct attr_value *default_val; /* Default value for this attribute. */
158 /* Structure for each DEFINE_DELAY. */
162 rtx def; /* DEFINE_DELAY expression. */
163 struct delay_desc *next; /* Next DEFINE_DELAY. */
164 int num; /* Number of DEFINE_DELAY, starting at 1. */
167 /* Record information about each DEFINE_FUNCTION_UNIT. */
169 struct function_unit_op
171 rtx condexp; /* Expression TRUE for applicable insn. */
172 struct function_unit_op *next; /* Next operation for this function unit. */
173 int num; /* Ordinal for this operation type in unit. */
174 int ready; /* Cost until data is ready. */
175 rtx busyexp; /* Expression computing conflict cost. */
178 /* Record information about each function unit mentioned in a
179 DEFINE_FUNCTION_UNIT. */
183 char *name; /* Function unit name. */
184 struct function_unit *next; /* Next function unit. */
185 int num; /* Ordinal of this unit type. */
186 int multiplicity; /* Number of units of this type. */
187 int simultaneity; /* Maximum number of simultaneous insns
188 on this function unit or 0 if unlimited. */
189 rtx condexp; /* Expression TRUE for insn needing unit. */
190 rtx costexp; /* Worst-case cost as function of insn. */
191 int num_opclasses; /* Number of different operation types. */
192 struct function_unit_op *ops; /* Pointer to first operation type. */
193 int needs_conflict_function; /* Nonzero if a conflict function required. */
194 rtx default_cost; /* Conflict cost, if constant. */
197 /* Listheads of above structures. */
199 static struct attr_desc *attrs;
200 static struct insn_def *defs;
201 static struct delay_desc *delays;
202 static struct function_unit *units;
204 /* Other variables. */
206 static int insn_code_number;
207 static int insn_index_number;
208 static int got_define_asm_attributes;
209 static int must_extract;
210 static int must_constrain;
211 static int address_used;
212 static int num_delays;
213 static int have_annul_true, have_annul_false;
214 static int num_units;
216 /* Used as operand to `operate_exp': */
218 enum operator {PLUS_OP, MINUS_OP, OR_OP, MAX_OP};
220 /* Stores, for each insn code, a bitmap that has bits on for each possible
223 static int *insn_alternatives;
225 /* Used to simplify expressions. */
227 static rtx true_rtx, false_rtx;
229 /* Used to reduce calls to `strcmp' */
231 static char *alternative_name = "alternative";
233 /* Simplify an expression. Only call the routine if there is something to
235 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
236 (RTX_UNCHANGING_P (EXP) ? (EXP) \
237 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
239 /* These are referenced by rtlanal.c and hence need to be defined somewhere.
240 They won't actually be used. */
242 rtx frame_pointer_rtx, stack_pointer_rtx, arg_pointer_rtx;
244 static rtx attr_rtx ();
245 static char *attr_printf ();
246 static char *attr_string ();
247 static rtx check_attr_test ();
248 static void check_attr_value ();
249 static rtx convert_set_attr_alternative ();
250 static rtx convert_set_attr ();
251 static void check_defs ();
252 static rtx convert_const_symbol_ref ();
253 static rtx make_canonical ();
254 static struct attr_value *get_attr_value ();
255 static void expand_delays ();
256 static rtx operate_exp ();
257 static void expand_units ();
258 static void fill_attr ();
259 static rtx substitute_address ();
260 static void make_length_attrs ();
261 static rtx identity_fn ();
262 static rtx zero_fn ();
263 static rtx one_fn ();
264 static rtx max_fn ();
265 static rtx simplify_cond ();
266 static void remove_insn_ent ();
267 static void insert_insn_ent ();
268 static rtx insert_right_side ();
269 static rtx make_alternative_compare ();
270 static int compute_alternative_mask ();
271 static rtx evaluate_eq_attr ();
272 static rtx simplify_and_tree ();
273 static rtx simplify_or_tree ();
274 static rtx simplify_test_exp ();
275 static void optimize_attrs ();
276 static void gen_attr ();
277 static int count_alternatives ();
278 static int compares_alternatives_p ();
279 static int contained_in_p ();
280 static void gen_insn ();
281 static void gen_delay ();
282 static void gen_unit ();
283 static void write_test_expr ();
284 static int max_attr_value ();
285 static void walk_attr_value ();
286 static void write_attr_get ();
287 static rtx eliminate_known_true ();
288 static void write_attr_set ();
289 static void write_attr_case ();
290 static void write_attr_value ();
291 static void write_attr_valueq ();
292 static void write_upcase ();
293 static void write_indent ();
294 static void write_eligible_delay ();
295 static void write_function_unit_info ();
296 static int n_comma_elts ();
297 static char *next_comma_elt ();
298 static struct attr_desc *find_attr ();
299 static void make_internal_attr ();
300 static struct attr_value *find_most_used ();
301 static rtx find_single_value ();
302 static rtx make_numeric_value ();
305 static void fatal ();
307 /* Hash table for sharing RTL and strings. */
309 /* Each hash table slot is a bucket containing a chain of these structures.
310 Strings are given negative hash codes; RTL expressions are given positive
315 struct attr_hash *next; /* Next structure in the bucket. */
316 int hashcode; /* Hash code of this rtx or string. */
319 char *str; /* The string (negative hash codes) */
320 rtx rtl; /* or the RTL recorded here. */
324 /* Now here is the hash table. When recording an RTL, it is added to
325 the slot whose index is the hash code mod the table size. Note
326 that the hash table is used for several kinds of RTL (see attr_rtx)
327 and for strings. While all these live in the same table, they are
328 completely independent, and the hash code is computed differently
331 #define RTL_HASH_SIZE 4093
332 struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
334 /* Here is how primitive or already-shared RTL's hash
336 #define RTL_HASH(RTL) ((int) (RTL) & 0777777)
338 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
341 attr_hash_add_rtx (hashcode, rtl)
345 register struct attr_hash *h;
347 h = (struct attr_hash *) xmalloc (sizeof (struct attr_hash));
348 h->hashcode = hashcode;
350 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
351 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
354 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
357 attr_hash_add_string (hashcode, str)
361 register struct attr_hash *h;
363 h = (struct attr_hash *) xmalloc (sizeof (struct attr_hash));
364 h->hashcode = -hashcode;
366 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
367 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
370 /* Generate an RTL expression, but allow sharing. Like gen_rtx, but the
373 rtx attr_rtx (code, [element1, ..., elementn]) */
382 register int i; /* Array indices... */
383 register char *fmt; /* Current rtx's format... */
384 register rtx rt_val; /* RTX to return to caller... */
386 register struct attr_hash *h;
389 code = va_arg (p, enum rtx_code);
391 /* For each of several cases, search the hash table for an existing entry.
392 Use that entry if one is found; otherwise create a new RTL and add it
395 if (GET_RTX_CLASS (code) == '1')
397 rtx arg0 = va_arg (p, rtx);
399 hashcode = (code + RTL_HASH (arg0));
400 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
401 if (h->hashcode == hashcode
402 && GET_CODE (h->u.rtl) == code
403 && XEXP (h->u.rtl, 0) == arg0)
408 rt_val = rtx_alloc (code);
409 XEXP (rt_val, 0) = arg0;
412 else if (GET_RTX_CLASS (code) == 'c'
413 || GET_RTX_CLASS (code) == '2'
414 || GET_RTX_CLASS (code) == '<')
416 rtx arg0 = va_arg (p, rtx);
417 rtx arg1 = va_arg (p, rtx);
419 hashcode = (code + RTL_HASH (arg0) + RTL_HASH (arg1));
420 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
421 if (h->hashcode == hashcode
422 && GET_CODE (h->u.rtl) == code
423 && XEXP (h->u.rtl, 0) == arg0
424 && XEXP (h->u.rtl, 1) == arg1)
429 rt_val = rtx_alloc (code);
430 XEXP (rt_val, 0) = arg0;
431 XEXP (rt_val, 1) = arg1;
434 else if (GET_RTX_LENGTH (code) == 1
435 && GET_RTX_FORMAT (code)[0] == 's')
437 char * arg0 = va_arg (p, char *);
439 hashcode = (code + RTL_HASH (arg0));
440 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
441 if (h->hashcode == hashcode
442 && GET_CODE (h->u.rtl) == code
443 && XSTR (h->u.rtl, 0) == arg0)
448 rt_val = rtx_alloc (code);
449 XSTR (rt_val, 0) = arg0;
452 else if (GET_RTX_LENGTH (code) == 2
453 && GET_RTX_FORMAT (code)[0] == 's'
454 && GET_RTX_FORMAT (code)[1] == 's')
456 char * arg0 = va_arg (p, char *);
457 char * arg1 = va_arg (p, char *);
459 hashcode = (code + RTL_HASH (arg0) + RTL_HASH (arg1));
460 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
461 if (h->hashcode == hashcode
462 && GET_CODE (h->u.rtl) == code
463 && XSTR (h->u.rtl, 0) == arg0
464 && XSTR (h->u.rtl, 1) == arg1)
469 rt_val = rtx_alloc (code);
470 XSTR (rt_val, 0) = arg0;
471 XSTR (rt_val, 1) = arg1;
476 rt_val = rtx_alloc (code); /* Allocate the storage space. */
478 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
479 for (i = 0; i < GET_RTX_LENGTH (code); i++)
483 case '0': /* Unused field. */
486 case 'i': /* An integer? */
487 XINT (rt_val, i) = va_arg (p, int);
490 case 's': /* A string? */
491 XSTR (rt_val, i) = va_arg (p, char *);
494 case 'e': /* An expression? */
495 case 'u': /* An insn? Same except when printing. */
496 XEXP (rt_val, i) = va_arg (p, rtx);
499 case 'E': /* An RTX vector? */
500 XVEC (rt_val, i) = va_arg (p, rtvec);
512 attr_hash_add_rtx (hashcode, rt_val);
520 /* Create a new string printed with the printf line arguments into a space
521 of at most LEN bytes:
523 rtx attr_printf (len, format, [arg1, ..., argn]) */
527 attr_printf (va_alist)
535 /* Print the string into a temporary location. */
537 len = va_arg (p, int);
538 str = (char *) alloca (len);
539 fmt = va_arg (p, char *);
540 vsprintf (str, fmt, p);
543 return attr_string (str, strlen (str));
546 /* Return a permanent (possibly shared) copy of a string STR (not assumed
547 to be null terminated) with LEN bytes. */
550 attr_string (str, len)
554 register struct attr_hash *h;
557 register char *new_str;
559 /* Compute the hash code. */
560 hashcode = (len + 1) * 613 + (unsigned)str[0];
561 for (i = 1; i <= len; i += 2)
562 hashcode = ((hashcode * 613) + (unsigned)str[i]);
564 hashcode = -hashcode;
566 /* Search the table for the string. */
567 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
568 if (h->hashcode == -hashcode
569 && !strcmp (h->u.str, str))
570 return h->u.str; /* <-- return if found. */
572 /* Not found; create a permanent copy and add it to the hash table. */
573 new_str = (char *) xmalloc (len + 1);
574 bcopy (str, new_str, len);
576 attr_hash_add_string (hashcode, new_str);
578 return new_str; /* Return the new string. */
581 /* Given a test expression for an attribute, ensure it is validly formed.
582 IS_CONST indicates whether the expression is constant for each compiler
583 run (a constant expression may not test any particular insn).
585 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
586 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
587 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
589 Update the string address in EQ_ATTR expression to be the same used
590 in the attribute (or `alternative_name') to speed up subsequent
591 `find_attr' calls and eliminate most `strcmp' calls.
593 Return the new expression, if any. */
596 check_attr_test (exp, is_const)
600 struct attr_desc *attr;
601 struct attr_value *av;
605 switch (GET_CODE (exp))
608 /* Handle negation test. */
609 if (XSTR (exp, 1)[0] == '!')
610 return check_attr_test (attr_rtx (NOT,
616 else if (n_comma_elts (XSTR (exp, 1)) == 1)
618 attr = find_attr (XEXP (exp, 0), 0);
621 if (! strcmp (XSTR (exp, 0), "alternative"))
623 XSTR (exp, 0) = alternative_name;
624 /* This can't be simplified any further. */
625 RTX_UNCHANGING_P (exp) = 1;
629 fatal ("Unknown attribute `%s' in EQ_ATTR", XEXP (exp, 0));
632 if (is_const && ! attr->is_const)
633 fatal ("Constant expression uses insn attribute `%s' in EQ_ATTR",
636 XSTR (exp, 0) = attr->name;
638 if (attr->is_numeric)
640 for (p = XSTR (exp, 1); *p; p++)
641 if (*p < '0' || *p > '9')
642 fatal ("Attribute `%s' takes only numeric values",
647 for (av = attr->first_value; av; av = av->next)
648 if (GET_CODE (av->value) == CONST_STRING
649 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
653 fatal ("Unknown value `%s' for `%s' attribute",
654 XEXP (exp, 1), XEXP (exp, 0));
659 /* Make an IOR tree of the possible values. */
661 name_ptr = XSTR (exp, 1);
662 while ((p = next_comma_elt (&name_ptr)) != NULL)
664 newexp = attr_rtx (EQ_ATTR, XSTR (exp, 0), p);
665 orexp = insert_right_side (IOR, orexp, newexp, -2);
668 return check_attr_test (orexp, is_const);
673 /* Either TRUE or FALSE. */
681 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const);
682 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const);
686 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const);
691 fatal ("RTL operator \"%s\" not valid in constant attribute test",
692 GET_RTX_NAME (MATCH_OPERAND));
694 case LE: case LT: case GT: case GE:
695 case LEU: case LTU: case GTU: case GEU:
697 /* These cases can't be simplified. */
698 RTX_UNCHANGING_P (exp) = 1;
704 /* These cases are valid for constant attributes, but can't be
706 RTX_UNCHANGING_P (exp) = 1;
710 fatal ("RTL operator \"%s\" not valid in attribute test",
711 GET_RTX_NAME (GET_CODE (exp)));
717 /* Given an expression, ensure that it is validly formed and that all named
718 attribute values are valid for the given attribute. Issue a fatal error
719 if not. If no attribute is specified, assume a numeric attribute. */
722 check_attr_value (exp, attr)
724 struct attr_desc *attr;
726 struct attr_value *av;
730 switch (GET_CODE (exp))
733 if (attr && ! attr->is_numeric)
734 fatal ("CONST_INT not valid for non-numeric `%s' attribute",
737 if (INTVAL (exp) < 0)
738 fatal ("Negative numeric value specified for `%s' attribute",
744 if (! strcmp (XSTR (exp, 0), "*"))
747 if (attr == 0 || attr->is_numeric)
749 for (p = XSTR (exp, 0); *p; p++)
750 if (*p > '9' || *p < '0')
751 fatal ("Non-numeric value for numeric `%s' attribute",
752 attr ? "internal" : attr->name);
756 for (av = attr->first_value; av; av = av->next)
757 if (GET_CODE (av->value) == CONST_STRING
758 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
762 fatal ("Unknown value `%s' for `%s' attribute",
763 XSTR (exp, 0), attr ? "internal" : attr->name);
768 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
769 attr ? attr->is_const : 0);
770 check_attr_value (XEXP (exp, 1), attr);
771 check_attr_value (XEXP (exp, 2), attr);
775 if (XVECLEN (exp, 0) % 2 != 0)
776 fatal ("First operand of COND must have even length");
778 for (i = 0; i < XVECLEN (exp, 0); i += 2)
780 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
781 attr ? attr->is_const : 0);
782 check_attr_value (XVECEXP (exp, 0, i + 1), attr);
785 check_attr_value (XEXP (exp, 1), attr);
789 if (attr && attr->is_const)
790 /* A constant SYMBOL_REF is valid as a constant attribute test and
791 is expanded later by make_canonical into a COND. */
793 /* Otherwise, fall through... */
796 fatal ("Illegal operation `%s' for attribute value",
797 GET_RTX_NAME (GET_CODE (exp)));
801 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
802 It becomes a COND with each test being (eq_attr "alternative "n") */
805 convert_set_attr_alternative (exp, num_alt, insn_code, insn_index)
808 int insn_code, insn_index;
814 if (XVECLEN (exp, 1) != num_alt)
815 fatal ("Bad number of entries in SET_ATTR_ALTERNATIVE for insn %d",
818 /* Make a COND with all tests but the last. Select the last value via the
820 condexp = rtx_alloc (COND);
821 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
823 for (i = 0; i < num_alt - 1; i++)
826 p = attr_printf (3, "%d", i);
828 /* Sharing this EQ_ATTR rtl causes trouble. */
829 XVECEXP (condexp, 0, 2 * i) = rtx_alloc (EQ_ATTR);
830 XSTR (XVECEXP (condexp, 0, 2 * i), 0) = alternative_name;
831 XSTR (XVECEXP (condexp, 0, 2 * i), 1) = p;
832 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
835 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
837 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
840 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
841 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
844 convert_set_attr (exp, num_alt, insn_code, insn_index)
847 int insn_code, insn_index;
854 /* See how many alternative specified. */
855 n = n_comma_elts (XSTR (exp, 1));
857 return attr_rtx (SET,
858 attr_rtx (ATTR, XSTR (exp, 0)),
859 attr_rtx (CONST_STRING, XSTR (exp, 1)));
861 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
862 XSTR (newexp, 0) = XSTR (exp, 0);
863 XVEC (newexp, 1) = rtvec_alloc (n);
865 /* Process each comma-separated name. */
866 name_ptr = XSTR (exp, 1);
868 while ((p = next_comma_elt (&name_ptr)) != NULL)
869 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
871 return convert_set_attr_alternative (newexp, num_alt, insn_code, insn_index);
874 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
875 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
882 struct attr_desc *attr;
886 for (id = defs; id; id = id->next)
888 if (XVEC (id->def, id->vec_idx) == NULL)
891 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
893 value = XVECEXP (id->def, id->vec_idx, i);
894 switch (GET_CODE (value))
897 if (GET_CODE (XEXP (value, 0)) != ATTR)
898 fatal ("Bad attribute set in pattern %d", id->insn_index);
901 case SET_ATTR_ALTERNATIVE:
902 value = convert_set_attr_alternative (value,
903 id->num_alternatives,
909 value = convert_set_attr (value, id->num_alternatives,
910 id->insn_code, id->insn_index);
914 fatal ("Invalid attribute code `%s' for pattern %d",
915 GET_RTX_NAME (GET_CODE (value)), id->insn_index);
918 if ((attr = find_attr (XSTR (XEXP (value, 0), 0), 0)) == NULL)
919 fatal ("Unknown attribute `%s' for pattern number %d",
920 XSTR (XEXP (value, 0), 0), id->insn_index);
922 XVECEXP (id->def, id->vec_idx, i) = value;
923 check_attr_value (XEXP (value, 1), attr);
928 /* Given a constant SYMBOL_REF expression, convert to a COND that
929 explicitly tests each enumerated value. */
932 convert_const_symbol_ref (exp, attr)
934 struct attr_desc *attr;
937 struct attr_value *av;
941 for (av = attr->first_value; av; av = av->next)
944 /* Make a COND with all tests but the last, and in the original order.
945 Select the last value via the default. Note that the attr values
946 are constructed in reverse order. */
948 condexp = rtx_alloc (COND);
949 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
950 av = attr->first_value;
951 XEXP (condexp, 1) = av->value;
953 for (i = num_alt - 2; av = av->next, i >= 0; i--)
958 XVECEXP (condexp, 0, 2 * i) = rtx_alloc (EQ);
959 XEXP (XVECEXP (condexp, 0, 2 * i), 0) = exp;
960 XEXP (XVECEXP (condexp, 0, 2 * i), 1) = value = rtx_alloc (SYMBOL_REF);
961 RTX_UNCHANGING_P (value) = 1;
962 XSTR (value, 0) = p = (char *) xmalloc (2
963 + strlen (attr->name)
964 + strlen (XSTR (av->value, 0)));
965 strcpy (p, attr->name);
967 strcat (p, XSTR (av->value, 0));
968 for (; *p != '\0'; p++)
969 if (*p >= 'a' && *p <= 'z')
972 XVECEXP (condexp, 0, 2 * i + 1) = av->value;
978 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
979 expressions by converting them into a COND. This removes cases from this
980 program. Also, replace an attribute value of "*" with the default attribute
984 make_canonical (attr, exp)
985 struct attr_desc *attr;
991 switch (GET_CODE (exp))
994 exp = make_numeric_value (INTVAL (exp));
998 if (! strcmp (XSTR (exp, 0), "*"))
1000 if (attr == 0 || attr->default_val == 0)
1001 fatal ("(attr_value \"*\") used in invalid context.");
1002 exp = attr->default_val->value;
1008 if (!attr->is_const || RTX_UNCHANGING_P (exp))
1010 RTX_UNCHANGING_P (exp) = 1;
1011 exp = convert_const_symbol_ref (exp, attr);
1012 check_attr_value (exp, attr);
1013 /* Goto COND case since this is now a COND. Note that while the
1014 new expression is rescanned, all symbol_ref notes are mared as
1019 newexp = rtx_alloc (COND);
1020 XVEC (newexp, 0) = rtvec_alloc (2);
1021 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1022 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1024 XEXP (newexp, 1) = XEXP (exp, 2);
1027 /* Fall through to COND case since this is now a COND. */
1031 /* First, check for degenerate COND. */
1032 if (XVECLEN (exp, 0) == 0)
1033 return make_canonical (attr, XEXP (exp, 1));
1035 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1036 XVECEXP (exp, 0, i + 1)
1037 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1039 XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1046 /* Given a value and an attribute description, return a `struct attr_value *'
1047 that represents that value. This is either an existing structure, if the
1048 value has been previously encountered, or a newly-created structure.
1050 `insn_code' is the code of an insn whose attribute has the specified
1051 value (-2 if not processing an insn). We ensure that all insns for
1052 a given value have the same number of alternatives if the value checks
1055 static struct attr_value *
1056 get_attr_value (value, attr, insn_code)
1058 struct attr_desc *attr;
1061 struct attr_value *av;
1064 value = make_canonical (attr, value);
1065 if (compares_alternatives_p (value))
1067 if (insn_code < 0 || insn_alternatives == NULL)
1068 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1070 num_alt = insn_alternatives[insn_code];
1073 for (av = attr->first_value; av; av = av->next)
1074 if (rtx_equal_p (value, av->value)
1075 && (num_alt == 0 || av->first_insn == NULL
1076 || insn_alternatives[av->first_insn->insn_code]))
1079 av = (struct attr_value *) xmalloc (sizeof (struct attr_value));
1081 av->next = attr->first_value;
1082 attr->first_value = av;
1083 av->first_insn = NULL;
1085 av->has_asm_insn = 0;
1090 /* After all DEFINE_DELAYs have been read in, create internal attributes
1091 to generate the required routines.
1093 First, we compute the number of delay slots for each insn (as a COND of
1094 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1095 delay type is specified, we compute a similar function giving the
1096 DEFINE_DELAY ordinal for each insn.
1098 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1099 tells whether a given insn can be in that delay slot.
1101 Normal attrbute filling and optimization expands these to contain the
1102 information needed to handle delay slots. */
1107 struct delay_desc *delay;
1113 /* First, generate data for `num_delay_slots' function. */
1115 condexp = rtx_alloc (COND);
1116 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1117 XEXP (condexp, 1) = make_numeric_value (0);
1119 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1121 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1122 XVECEXP (condexp, 0, i + 1)
1123 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1126 make_internal_attr ("*num_delay_slots", condexp, 0);
1128 /* If more than one delay type, do the same for computing the delay type. */
1131 condexp = rtx_alloc (COND);
1132 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1133 XEXP (condexp, 1) = make_numeric_value (0);
1135 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1137 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1138 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1141 make_internal_attr ("*delay_type", condexp, 1);
1144 /* For each delay possibility and delay slot, compute an eligability
1145 attribute for non-anulled insns and for each type of annulled (annul
1146 if true and annul if false). */
1147 for (delay = delays; delay; delay = delay->next)
1149 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1151 condexp = XVECEXP (delay->def, 1, i);
1152 if (condexp == 0) condexp = false_rtx;
1153 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1154 make_numeric_value (1), make_numeric_value (0));
1156 p = attr_printf (13, "*delay_%d_%d", delay->num, i / 3);
1157 make_internal_attr (p, newexp, 1);
1159 if (have_annul_true)
1161 condexp = XVECEXP (delay->def, 1, i + 1);
1162 if (condexp == 0) condexp = false_rtx;
1163 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1164 make_numeric_value (1),
1165 make_numeric_value (0));
1166 p = attr_printf (18, "*annul_true_%d_%d", delay->num, i / 3);
1167 make_internal_attr (p, newexp, 1);
1170 if (have_annul_false)
1172 condexp = XVECEXP (delay->def, 1, i + 2);
1173 if (condexp == 0) condexp = false_rtx;
1174 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1175 make_numeric_value (1),
1176 make_numeric_value (0));
1177 p = attr_printf (18, "*annul_false_%d_%d", delay->num, i / 3);
1178 make_internal_attr (p, newexp, 1);
1184 /* This function is given a left and right side expression and an operator.
1185 Each side is a conditional expression, each alternative of which has a
1186 numerical value. The function returns another conditional expression
1187 which, for every possible set of condition values, returns a value that is
1188 the operator applied to the values of the two sides.
1190 Since this is called early, it must also support IF_THEN_ELSE. */
1193 operate_exp (op, left, right)
1197 int left_value, right_value;
1201 /* If left is a string, apply operator to it and the right side. */
1202 if (GET_CODE (left) == CONST_STRING)
1204 /* If right is also a string, just perform the operation. */
1205 if (GET_CODE (right) == CONST_STRING)
1207 left_value = atoi (XSTR (left, 0));
1208 right_value = atoi (XSTR (right, 0));
1212 i = left_value + right_value;
1216 i = left_value - right_value;
1220 i = left_value | right_value;
1224 if (left_value > right_value)
1234 return make_numeric_value (i);
1236 else if (GET_CODE (right) == IF_THEN_ELSE)
1238 /* Apply recursively to all values within. */
1239 return attr_rtx (IF_THEN_ELSE, XEXP (right, 0),
1240 operate_exp (op, left, XEXP (right, 1)),
1241 operate_exp (op, left, XEXP (right, 2)));
1243 else if (GET_CODE (right) == COND)
1245 newexp = rtx_alloc (COND);
1246 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (right, 0));
1247 for (i = 0; i < XVECLEN (right, 0); i += 2)
1249 XVECEXP (newexp, 0, i) = XVECEXP (right, 0, i);
1250 XVECEXP (newexp, 0, i + 1)
1251 = operate_exp (op, left, XVECEXP (right, 0, i + 1));
1254 XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
1259 fatal ("Badly formed attribute value");
1262 /* Otherwise, do recursion the other way. */
1263 else if (GET_CODE (left) == IF_THEN_ELSE)
1265 return attr_rtx (IF_THEN_ELSE, XEXP (left, 0),
1266 operate_exp (op, XEXP (left, 1), right),
1267 operate_exp (op, XEXP (left, 2), right));
1270 else if (GET_CODE (left) == COND)
1272 newexp = rtx_alloc (COND);
1273 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (left, 0));
1274 for (i = 0; i < XVECLEN (left, 0); i += 2)
1276 XVECEXP (newexp, 0, i) = XVECEXP (left, 0, i);
1277 XVECEXP (newexp, 0, i + 1)
1278 = operate_exp (op, XVECEXP (left, 0, i + 1), right);
1281 XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
1287 fatal ("Badly formed attribute value.");
1292 /* Once all attributes and DEFINE_FUNCTION_UNITs have been read, we
1293 construct a number of attributes.
1295 The first produces a function `function_units_used' which is given an
1296 insn and produces a mask showing which function units are required for
1297 the execution of that insn.
1299 The second produces a function `result_ready_cost' which is used to
1300 determine the time that the result of an insn will be ready and hence
1301 a worst-case schedule.
1303 Both of these produce quite complex expressions which are then set as the
1304 default value of internal attributes. Normal attribute simplification
1305 should produce reasonable expressions.
1307 For each unit, a `<name>_unit_ready_cost' function will take an
1308 insn and give the delay until that unit will be ready with the result
1309 and a `<name>_unit_busy_delay' function is given an insn already
1310 executing on the unit and a candidate to execute and will give the
1311 cost from the time the executing insn started until the candidate
1312 can start (ignore limitations on the number of simultaneous insns). */
1317 struct function_unit *unit;
1318 struct function_unit_op *op;
1324 /* Initially, cost and masks are zero. */
1325 unitsmask = readycost = make_numeric_value (0);
1327 /* Set up a conditional for costs and unit mask. */
1328 newexp = rtx_alloc (IF_THEN_ELSE);
1329 XEXP (newexp, 2) = make_numeric_value (0);
1331 /* For each unit, insert its contribution to the above three values. */
1332 for (unit = units; unit; unit = unit->next)
1334 /* An expression that computes the ready cost for this unit. */
1335 rtx readyexp = rtx_alloc (COND);
1336 /* An expression that maps insns to operation number for conflicts. */
1337 rtx caseexp = rtx_alloc (COND);
1339 XVEC (readyexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
1340 XVEC (caseexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
1342 for (op = unit->ops; op; op = op->next)
1344 /* Validate the expressions we were given for the conditions
1345 and busy cost. Then make an attribute for use in the conflict
1347 op->condexp = check_attr_test (op->condexp, 0);
1348 check_attr_value (op->busyexp, 0);
1349 str = attr_printf (strlen (unit->name) + 11, "*%s_case_%d",
1350 unit->name, op->num);
1351 make_internal_attr (str, make_canonical (0, op->busyexp));
1353 /* Make our adjustment to the two COND's being computed. If we are
1354 the last operation class, place our values into the default of
1356 if (op->num == unit->num_opclasses - 1)
1358 XEXP (readyexp, 1) = make_numeric_value (op->ready);
1359 XEXP (caseexp, 1) = make_numeric_value (op->num);
1363 XVECEXP (readyexp, 0, op->num * 2) = op->condexp;
1364 XVECEXP (readyexp, 0, op->num * 2 + 1)
1365 = make_numeric_value (op->ready);
1366 XVECEXP (caseexp, 0, op->num * 2) = op->condexp;
1367 XVECEXP (caseexp, 0, op->num * 2 + 1)
1368 = make_numeric_value (op->num);
1372 /* Make an attribute for the case number and ready delay. */
1373 str = attr_printf (strlen (unit->name) + 8, "*%s_cases", unit->name);
1374 make_internal_attr (str, caseexp, 1);
1376 str = attr_printf (strlen (unit->name) + 20, "*%s_unit_ready_cost",
1378 make_internal_attr (str, readyexp, 0);
1380 /* Merge this function unit into the ready cost and unit mask
1382 XEXP (newexp, 0) = check_attr_test (unit->condexp, 0);
1383 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1384 unitsmask = operate_exp (OR_OP, unitsmask, newexp);
1386 XEXP (newexp, 1) = readyexp;
1387 readycost = operate_exp (MAX_OP, readycost, newexp);
1390 make_internal_attr ("*function_units_used", unitsmask, 0);
1391 make_internal_attr ("*result_ready_cost", readycost, 0);
1394 /* Once all attributes and insns have been read and checked, we construct for
1395 each attribute value a list of all the insns that have that value for
1400 struct attr_desc *attr;
1402 struct attr_value *av;
1403 struct insn_ent *ie;
1404 struct insn_def *id;
1408 for (id = defs; id; id = id->next)
1410 /* If no value is specified for this insn for this attribute, use the
1413 if (XVEC (id->def, id->vec_idx))
1414 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1415 if (! strcmp (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
1417 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
1420 av = attr->default_val;
1422 av = get_attr_value (value, attr, id->insn_code);
1424 ie = (struct insn_ent *) xmalloc (sizeof (struct insn_ent));
1425 ie->insn_code = id->insn_code;
1426 ie->insn_index = id->insn_code;
1427 insert_insn_ent (av, ie);
1431 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
1432 test that checks relative positions of insns (uses MATCH_DUP or PC).
1433 If so, replace it with what is obtained by passing the expression to
1434 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
1435 recursively on each value (including the default value). Otherwise,
1436 return the value returned by NO_ADDRESS_FN applied to EXP. */
1439 substitute_address (exp, no_address_fn, address_fn)
1441 rtx (*no_address_fn) ();
1442 rtx (*address_fn) ();
1447 if (GET_CODE (exp) == COND)
1449 /* See if any tests use addresses. */
1451 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1452 walk_attr_value (XVECEXP (exp, 0, i));
1455 return (*address_fn) (exp);
1457 /* Make a new copy of this COND, replacing each element. */
1458 newexp = rtx_alloc (COND);
1459 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1460 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1462 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
1463 XVECEXP (newexp, 0, i + 1)
1464 = substitute_address (XVECEXP (exp, 0, i + 1),
1465 no_address_fn, address_fn);
1468 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
1469 no_address_fn, address_fn);
1474 else if (GET_CODE (exp) == IF_THEN_ELSE)
1477 walk_attr_value (XEXP (exp, 0));
1479 return (*address_fn) (exp);
1481 return attr_rtx (IF_THEN_ELSE,
1482 substitute_address (XEXP (exp, 0),
1483 no_address_fn, address_fn),
1484 substitute_address (XEXP (exp, 1),
1485 no_address_fn, address_fn),
1486 substitute_address (XEXP (exp, 2),
1487 no_address_fn, address_fn));
1490 return (*no_address_fn) (exp);
1493 /* Make new attributes from the `length' attribute. The following are made,
1494 each corresponding to a function called from `shorten_branches' or
1497 *insn_default_length This is the length of the insn to be returned
1498 by `get_attr_length' before `shorten_branches'
1499 has been called. In each case where the length
1500 depends on relative addresses, the largest
1501 possible is used. This routine is also used
1502 to compute the initial size of the insn.
1504 *insn_variable_length_p This returns 1 if the insn's length depends
1505 on relative addresses, zero otherwise.
1507 *insn_current_length This is only called when it is known that the
1508 insn has a variable length and returns the
1509 current length, based on relative addresses.
1513 make_length_attrs ()
1515 static char *new_names[] = {"*insn_default_length",
1516 "*insn_variable_length_p",
1517 "*insn_current_length"};
1518 static rtx (*no_address_fn[]) () = {identity_fn, zero_fn, zero_fn};
1519 static rtx (*address_fn[]) () = {max_fn, one_fn, identity_fn};
1521 struct attr_desc *length_attr, *new_attr;
1522 struct attr_value *av, *new_av;
1523 struct insn_ent *ie, *new_ie;
1525 /* See if length attribute is defined. If so, it must be numeric. Make
1526 it special so we don't output anything for it. */
1527 length_attr = find_attr ("length", 0);
1528 if (length_attr == 0)
1531 if (! length_attr->is_numeric)
1532 fatal ("length attribute must be numeric.");
1534 length_attr->is_const = 0;
1535 length_attr->is_special = 1;
1537 /* Make each new attribute, in turn. */
1538 for (i = 0; i < sizeof new_names / sizeof new_names[0]; i++)
1540 make_internal_attr (new_names[i],
1541 substitute_address (length_attr->default_val->value,
1542 no_address_fn[i], address_fn[i]),
1544 new_attr = find_attr (new_names[i], 0);
1545 for (av = length_attr->first_value; av; av = av->next)
1546 for (ie = av->first_insn; ie; ie = ie->next)
1548 new_av = get_attr_value (substitute_address (av->value,
1551 new_attr, ie->insn_code);
1552 new_ie = (struct insn_ent *) xmalloc (sizeof (struct insn_ent));
1553 new_ie->insn_code = ie->insn_code;
1554 new_ie->insn_index = ie->insn_index;
1555 insert_insn_ent (new_av, new_ie);
1560 /* Utility functions called from above routine. */
1573 return make_numeric_value (0);
1580 return make_numeric_value (1);
1587 return make_numeric_value (max_attr_value (exp));
1590 /* Take a COND expression and see if any of the conditions in it can be
1591 simplified. If any are known true or known false for the particular insn
1592 code, the COND can be further simplified.
1594 Also call ourselves on any COND operations that are values of this COND.
1596 We only do the first replacement found directly and call ourselves
1597 recursively for subsequent replacements. */
1600 simplify_cond (exp, insn_code, insn_index)
1602 int insn_code, insn_index;
1609 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1611 newtest = SIMPLIFY_TEST_EXP (XVECEXP (exp, 0, i), insn_code, insn_index);
1612 if (newtest == true_rtx)
1614 /* Make a new COND with any previous conditions and the value for
1615 this pair as the default value. */
1616 newexp = rtx_alloc (COND);
1617 XVEC (newexp, 0) = rtvec_alloc (i);
1618 for (j = 0; j < i; j++)
1619 XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
1621 XEXP (newexp, 1) = XVECEXP (exp, 0, i + 1);
1625 else if (newtest == false_rtx)
1627 /* Build a new COND without this test. */
1628 newexp = rtx_alloc (COND);
1629 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0) - 2);
1630 for (j = 0; j < i; j++)
1631 XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
1633 for (j = i; j < XVECLEN (newexp, 0); j++)
1634 XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j + 2);
1636 XEXP (newexp, 1) = XEXP (exp, 1);
1640 else if (newtest != XVECEXP (exp, 0, i))
1642 newexp = rtx_alloc (COND);
1643 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1644 for (j = 0; j < XVECLEN (exp, 0); j++)
1645 XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
1646 XEXP (newexp, 1) = XEXP (exp, 1);
1648 XVECEXP (newexp, 0, i) = newtest;
1652 /* See if this value may need simplification. */
1653 if (GET_CODE (XVECEXP (exp, 0, i + 1)) == COND)
1655 value = simplify_cond (XVECEXP (exp, 0, i + 1),
1656 insn_code, insn_index);
1657 if (value != XVECEXP (exp, 0, i + 1))
1659 newexp = rtx_alloc (COND);
1660 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1661 for (j = 0; j < XVECLEN (exp, 0); j++)
1662 XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
1663 XEXP (newexp, 1) = XEXP (exp, 1);
1665 XVECEXP (newexp, 0, i + 1) = value;
1670 /* If this is the last condition in a COND and our value is the same
1671 as the default value, our test isn't needed. */
1672 if (i == XVECLEN (exp, 0) - 2
1673 && rtx_equal_p (XVECEXP (exp, 0, i + 1), XEXP (exp, 1)))
1675 newexp = rtx_alloc (COND);
1676 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0) - 2);
1677 for (j = 0; j < i; j++)
1678 XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
1679 XEXP (newexp, 1) = XEXP (exp, 1);
1683 /* If this value and the value for the next test are the same, merge the
1685 else if (i != XVECLEN (exp, 0) - 2
1686 && rtx_equal_p (XVECEXP (exp, 0, i + 1),
1687 XVECEXP (exp, 0, i + 3)))
1689 newexp = rtx_alloc (COND);
1690 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0) - 2);
1691 for (j = 0; j < i; j++)
1692 XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
1694 XVECEXP (newexp, 0, j)
1695 = insert_right_side (IOR, XVECEXP (exp, 0, i),
1696 XVECEXP (exp, 0, i + 2),
1697 insn_code, insn_index);
1698 XVECEXP (newexp, 0, j + 1) = XVECEXP (exp, 0, i + 1);
1700 for (j = i + 2; j < XVECLEN (newexp, 0); j++)
1701 XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j + 2);
1703 XEXP (newexp, 1) = XEXP (exp, 1);
1708 /* See if default value needs simplification. */
1709 if (GET_CODE (XEXP (exp, 1)) == COND)
1711 value = simplify_cond (XEXP (exp, 1), insn_code, insn_index);
1712 if (value != XEXP (exp, 1))
1714 newexp = rtx_alloc (COND);
1715 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1716 for (j = 0; j < XVECLEN (exp, 0); j++)
1717 XVECEXP (newexp, 0, j) = XVECEXP (exp, 0, j);
1718 XEXP (newexp, 1) = value;
1724 else if (XVECLEN (newexp, 0) == 1)
1725 return XVECEXP (newexp, 0, 0);
1727 return simplify_cond (newexp, insn_code, insn_index);
1730 /* Remove an insn entry from an attribute value. */
1733 remove_insn_ent (av, ie)
1734 struct attr_value *av;
1735 struct insn_ent *ie;
1737 struct insn_ent *previe;
1739 if (av->first_insn == ie)
1740 av->first_insn = ie->next;
1743 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
1745 previe->next = ie->next;
1749 if (ie->insn_code == -1)
1750 av->has_asm_insn = 0;
1753 /* Insert an insn entry in an attribute value list. */
1756 insert_insn_ent (av, ie)
1757 struct attr_value *av;
1758 struct insn_ent *ie;
1760 ie->next = av->first_insn;
1761 av->first_insn = ie;
1763 if (ie->insn_code == -1)
1764 av->has_asm_insn = 1;
1767 /* This is a utility routine to take an expression that is a tree of either
1768 AND or IOR expressions and insert a new term. The new term will be
1769 inserted at the right side of the first node whose code does not match
1770 the root. A new node will be created with the root's code. Its left
1771 side will be the old right side and its right side will be the new
1774 If the `term' is itself a tree, all its leaves will be inserted. */
1777 insert_right_side (code, exp, term, insn_code, insn_index)
1781 int insn_code, insn_index;
1785 if (GET_CODE (term) == code)
1787 exp = insert_right_side (code, exp, XEXP (term, 0),
1788 insn_code, insn_index);
1789 exp = insert_right_side (code, exp, XEXP (term, 1),
1790 insn_code, insn_index);
1795 if (GET_CODE (exp) == code)
1797 /* Make a copy of this expression and call recursively. */
1798 newexp = attr_rtx (code, XEXP (exp, 0),
1799 insert_right_side (code, XEXP (exp, 1),
1800 term, insn_code, insn_index));
1804 /* Insert the new term. */
1805 newexp = attr_rtx (code, exp, term);
1808 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
1811 /* If we have an expression which AND's a bunch of
1812 (not (eq_attrq "alternative" "n"))
1813 terms, we may have covered all or all but one of the possible alternatives.
1814 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
1816 This routine is passed an expression and either AND or IOR. It returns a
1817 bitmask indicating which alternatives are present. */
1820 compute_alternative_mask (exp, code)
1824 if (GET_CODE (exp) == code)
1825 return compute_alternative_mask (XEXP (exp, 0), code)
1826 | compute_alternative_mask (XEXP (exp, 1), code);
1828 else if (code == AND && GET_CODE (exp) == NOT
1829 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
1830 && XSTR (XEXP (exp, 0), 0) == alternative_name)
1831 return 1 << atoi (XSTR (XEXP (exp, 0), 1));
1833 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
1834 && XSTR (exp, 0) == alternative_name)
1835 return 1 << atoi (XSTR (exp, 1));
1841 /* Given I, a single-bit mask, return RTX to compare the `alternative'
1842 attribute with the value represented by that bit. */
1845 make_alternative_compare (mask)
1853 for (i = 0; (mask & (1 << i)) == 0; i++)
1856 alternative = attr_printf (3, "%d", i);
1858 newexp = attr_rtx (EQ_ATTR, alternative_name, alternative);
1859 RTX_UNCHANGING_P (newexp) = 1;
1864 /* If we are processing an (eq_attr "attr" "value") test, we find the value
1865 of "attr" for this insn code. From that value, we can compute a test
1866 showing when the EQ_ATTR will be true. This routine performs that
1867 computation. If a test condition involves an address, we leave the EQ_ATTR
1868 intact because addresses are only valid for the `length' attribute. */
1871 evaluate_eq_attr (exp, value, insn_code, insn_index)
1874 int insn_code, insn_index;
1881 if (GET_CODE (value) == CONST_STRING)
1883 if (! strcmp (XSTR (value, 0), XSTR (exp, 1)))
1888 else if (GET_CODE (value) == COND)
1890 /* We construct an IOR of all the cases for which the requested attribute
1891 value is present. Since we start with FALSE, if it is not present,
1892 FALSE will be returned.
1894 Each case is the AND of the NOT's of the previous conditions with the
1895 current condition; in the default case the current condition is TRUE.
1897 For each possible COND value, call ourselves recursively.
1899 The extra TRUE and FALSE expressions will be eliminated by another
1900 call to the simplification routine. */
1905 for (i = 0; i < XVECLEN (value, 0); i += 2)
1907 right = insert_right_side (AND, andexp,
1908 XVECEXP (value, 0, i),
1909 insn_code, insn_index);
1910 right = insert_right_side (AND, right,
1911 evaluate_eq_attr (exp, XVECEXP (value, 0, i + 1),
1912 insn_code, insn_index),
1913 insn_code, insn_index);
1914 orexp = insert_right_side (IOR, orexp, right,
1915 insn_code, insn_index);
1917 /* Add this condition into the AND expression. */
1918 newexp = attr_rtx (NOT, XVECEXP (value, 0, i));
1919 andexp = insert_right_side (AND, andexp, newexp,
1920 insn_code, insn_index);
1923 /* Handle the default case. */
1924 right = insert_right_side (AND, andexp,
1925 evaluate_eq_attr (exp, XEXP (value, 1),
1926 insn_code, insn_index),
1927 insn_code, insn_index);
1928 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
1933 /* If uses an address, must return original expression. */
1936 walk_attr_value (newexp);
1944 /* This routine is called when an AND of a term with a tree of AND's is
1945 encountered. If the term or its complement is present in the tree, it
1946 can be replaced with TRUE or FALSE, respectively.
1948 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
1949 be true and hence are complementary.
1951 There is one special case: If we see
1952 (and (not (eq_attr "att" "v1"))
1953 (eq_attr "att" "v2"))
1954 this can be replaced by (eq_attr "att" "v2"). To do this we need to
1955 replace the term, not anything in the AND tree. So we pass a pointer to
1959 simplify_and_tree (exp, pterm, insn_code, insn_index)
1962 int insn_code, insn_index;
1967 int left_eliminates_term, right_eliminates_term;
1969 if (GET_CODE (exp) == AND)
1971 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
1972 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
1973 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
1975 newexp = attr_rtx (GET_CODE (exp), left, right);
1977 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
1981 else if (GET_CODE (exp) == IOR)
1983 /* For the IOR case, we do the same as above, except that we can
1984 only eliminate `term' if both sides of the IOR would do so. */
1986 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
1987 left_eliminates_term = (temp == true_rtx);
1990 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
1991 right_eliminates_term = (temp == true_rtx);
1993 if (left_eliminates_term && right_eliminates_term)
1996 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
1998 newexp = attr_rtx (GET_CODE (exp), left, right);
2000 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2004 /* Check for simplifications. Do some extra checking here since this
2005 routine is called so many times. */
2010 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2013 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2016 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2018 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2021 if (! strcmp (XSTR (exp, 1), XSTR (*pterm, 1)))
2027 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2028 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2030 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2033 if (! strcmp (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2039 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2040 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2042 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2045 if (! strcmp (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2051 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2053 if (rtx_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2057 else if (GET_CODE (exp) == NOT)
2059 if (rtx_equal_p (XEXP (exp, 0), *pterm))
2063 else if (GET_CODE (*pterm) == NOT)
2065 if (rtx_equal_p (XEXP (*pterm, 0), exp))
2069 else if (rtx_equal_p (exp, *pterm))
2075 /* Similiar to `simplify_and_tree', but for IOR trees. */
2078 simplify_or_tree (exp, pterm, insn_code, insn_index)
2081 int insn_code, insn_index;
2086 int left_eliminates_term, right_eliminates_term;
2088 if (GET_CODE (exp) == IOR)
2090 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2091 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2092 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2094 newexp = attr_rtx (GET_CODE (exp), left, right);
2096 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2100 else if (GET_CODE (exp) == AND)
2102 /* For the AND case, we do the same as above, except that we can
2103 only eliminate `term' if both sides of the AND would do so. */
2105 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2106 left_eliminates_term = (temp == false_rtx);
2109 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2110 right_eliminates_term = (temp == false_rtx);
2112 if (left_eliminates_term && right_eliminates_term)
2115 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2117 newexp = attr_rtx (GET_CODE (exp), left, right);
2119 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2123 if (rtx_equal_p (exp, *pterm))
2126 else if (GET_CODE (exp) == NOT && rtx_equal_p (XEXP (exp, 0), *pterm))
2129 else if (GET_CODE (*pterm) == NOT && rtx_equal_p (XEXP (*pterm, 0), exp))
2132 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2133 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2134 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
2137 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2138 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
2139 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
2145 /* Given an expression, see if it can be simplified for a particular insn
2146 code based on the values of other attributes being tested. This can
2147 eliminate nested get_attr_... calls.
2149 Note that if an endless recursion is specified in the patterns, the
2150 optimization will loop. However, it will do so in precisely the cases where
2151 an infinite recursion loop could occur during compilation. It's better that
2155 simplify_test_exp (exp, insn_code, insn_index)
2157 int insn_code, insn_index;
2160 struct attr_desc *attr;
2161 struct attr_value *av;
2162 struct insn_ent *ie;
2166 switch (GET_CODE (exp))
2169 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2170 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2172 /* If either side is an IOR and we have (eq_attr "alternative" ..")
2173 present on both sides, apply the distributive law since this will
2174 yield simplications. */
2175 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
2176 && compute_alternative_mask (left, IOR)
2177 && compute_alternative_mask (right, IOR))
2179 if (GET_CODE (left) == IOR)
2186 newexp = attr_rtx (IOR,
2187 attr_rtx (AND, left, XEXP (right, 0)),
2188 attr_rtx (AND, left, XEXP (right, 1)));
2190 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2193 /* Try with the term on both sides. */
2194 right = simplify_and_tree (right, &left, insn_code, insn_index);
2195 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2196 left = simplify_and_tree (left, &right, insn_code, insn_index);
2198 if (left == false_rtx || right == false_rtx)
2200 else if (left == true_rtx)
2202 else if (right == true_rtx)
2205 /* See if all or all but one of the insn's alternatives are specified
2206 in this tree. Optimize if so. */
2208 else if (insn_code >= 0
2209 && (GET_CODE (left) == AND
2210 || (GET_CODE (left) == NOT
2211 && GET_CODE (XEXP (left, 0)) == EQ_ATTR
2212 && XSTR (XEXP (left, 0), 0) == alternative_name)
2213 || GET_CODE (right) == AND
2214 || (GET_CODE (right) == NOT
2215 && GET_CODE (XEXP (right, 0)) == EQ_ATTR
2216 && XSTR (XEXP (right, 0), 0) == alternative_name)))
2218 i = compute_alternative_mask (exp, AND);
2219 if (i & ~insn_alternatives[insn_code])
2220 fatal ("Illegal alternative specified for pattern number %d",
2223 /* If all alternatives are excluded, this is false. */
2224 i ^= insn_alternatives[insn_code];
2227 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2229 /* If just one excluded, AND a comparison with that one to the
2230 front of the tree. The others will be eliminated by
2231 optimization. We do not want to do this if the insn has one
2232 alternative and we have tested none of them! */
2233 left = make_alternative_compare (i);
2234 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2235 newexp = attr_rtx (AND, left, right);
2237 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2241 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2243 newexp = attr_rtx (AND, left, right);
2244 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2249 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2250 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2252 right = simplify_or_tree (right, &left, insn_code, insn_index);
2253 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2254 left = simplify_or_tree (left, &right, insn_code, insn_index);
2256 if (right == true_rtx || left == true_rtx)
2258 else if (left == false_rtx)
2260 else if (right == false_rtx)
2263 /* Test for simple cases where the distributive law is useful. I.e.,
2264 convert (ior (and (x) (y))
2270 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2271 && rtx_equal_p (XEXP (left, 0), XEXP (right, 0)))
2273 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
2275 left = XEXP (left, 0);
2277 newexp = attr_rtx (AND, left, right);
2278 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2281 /* See if all or all but one of the insn's alternatives are specified
2282 in this tree. Optimize if so. */
2284 else if (insn_code >= 0
2285 && (GET_CODE (left) == IOR
2286 || (GET_CODE (left) == EQ_ATTR
2287 && XSTR (left, 0) == alternative_name)
2288 || GET_CODE (right) == IOR
2289 || (GET_CODE (right) == EQ_ATTR
2290 && XSTR (right, 0) == alternative_name)))
2292 i = compute_alternative_mask (exp, IOR);
2293 if (i & ~insn_alternatives[insn_code])
2294 fatal ("Illegal alternative specified for pattern number %d",
2297 /* If all alternatives are included, this is true. */
2298 i ^= insn_alternatives[insn_code];
2301 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2303 /* If just one excluded, IOR a comparison with that one to the
2304 front of the tree. The others will be eliminated by
2305 optimization. We do not want to do this if the insn has one
2306 alternative and we have tested none of them! */
2307 left = make_alternative_compare (i);
2308 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2309 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
2311 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2315 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2317 newexp = attr_rtx (IOR, left, right);
2318 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2323 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2324 if (GET_CODE (left) == NOT)
2325 return XEXP (left, 0);
2327 if (left == false_rtx)
2329 else if (left == true_rtx)
2332 /* Try to apply De`Morgan's laws. */
2333 else if (GET_CODE (left) == IOR)
2335 newexp = attr_rtx (AND,
2336 attr_rtx (NOT, XEXP (left, 0)),
2337 attr_rtx (NOT, XEXP (left, 1)));
2339 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2341 else if (GET_CODE (left) == AND)
2343 newexp = attr_rtx (IOR,
2344 attr_rtx (NOT, XEXP (left, 0)),
2345 attr_rtx (NOT, XEXP (left, 1)));
2347 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2349 else if (left != XEXP (exp, 0))
2351 newexp = attr_rtx (NOT, left);
2356 /* Look at the value for this insn code in the specified attribute.
2357 We normally can replace this comparison with the condition that
2358 would give this insn the values being tested for. */
2359 if (XSTR (exp, 0) != alternative_name
2360 && (attr = find_attr (XSTR (exp, 0), 0)) != NULL)
2361 for (av = attr->first_value; av; av = av->next)
2362 for (ie = av->first_insn; ie; ie = ie->next)
2363 if (ie->insn_code == insn_code)
2364 return evaluate_eq_attr (exp, av->value, insn_code, insn_index);
2367 /* We have already simplified this expression. Simplifying it again
2368 won't buy anything unless we weren't given a valid insn code
2369 to process (i.e., we are canonicalizing something.). */
2370 if (insn_code != -2)
2371 RTX_UNCHANGING_P (newexp) = 1;
2376 /* Optimize the attribute lists by seeing if we can determine conditional
2377 values from the known values of other attributes. This will save subroutine
2378 calls during the compilation. */
2383 struct attr_desc *attr;
2384 struct attr_value *av;
2385 struct insn_ent *ie, *nextie;
2387 int something_changed = 1;
2389 /* Loop until nothing changes for one iteration. */
2390 while (something_changed)
2392 something_changed = 0;
2393 for (attr = attrs; attr; attr = attr->next)
2394 for (av = attr->first_value; av; av = av->next)
2395 for (ie = av->first_insn; ie; ie = nextie)
2398 if (GET_CODE (av->value) != COND)
2401 newexp = simplify_cond (av->value, ie->insn_code,
2403 if (newexp != av->value)
2405 remove_insn_ent (av, ie);
2406 insert_insn_ent (get_attr_value (newexp, attr,
2407 ie->insn_code), ie);
2408 something_changed = 1;
2414 /* Create table entries for DEFINE_ATTR. */
2420 struct attr_desc *attr;
2421 struct attr_value *av;
2425 /* Make a new attribute structure. Check for duplicate by looking at
2426 attr->default_val, since it is initialized by this routine. */
2427 attr = find_attr (XSTR (exp, 0), 1);
2428 if (attr->default_val)
2429 fatal ("Duplicate definition for `%s' attribute", attr->name);
2431 if (*XSTR (exp, 1) == '\0')
2432 attr->is_numeric = 1;
2435 name_ptr = XSTR (exp, 1);
2436 while ((p = next_comma_elt (&name_ptr)) != NULL)
2438 av = (struct attr_value *) xmalloc (sizeof (struct attr_value));
2439 av->value = attr_rtx (CONST_STRING, p);
2440 av->next = attr->first_value;
2441 attr->first_value = av;
2442 av->first_insn = NULL;
2444 av->has_asm_insn = 0;
2448 if (GET_CODE (XEXP (exp, 2)) == CONST)
2451 if (attr->is_numeric)
2452 fatal ("Constant attributes may not take numeric values");
2453 /* Get rid of the CONST node. It is allowed only at top-level. */
2454 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
2457 if (! strcmp (attr->name, "length") && ! attr->is_numeric)
2458 fatal ("`length' attribute must take numeric values");
2460 /* Set up the default value. */
2461 check_attr_value (XEXP (exp, 2), attr);
2462 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
2465 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
2466 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
2467 number of alternatives as this should be checked elsewhere. */
2470 count_alternatives (exp)
2476 if (GET_CODE (exp) == MATCH_OPERAND)
2477 return n_comma_elts (XSTR (exp, 2));
2479 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
2480 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
2485 n = count_alternatives (XEXP (exp, i));
2492 if (XVEC (exp, i) != NULL)
2493 for (j = 0; j < XVECLEN (exp, i); j++)
2495 n = count_alternatives (XVECEXP (exp, i, j));
2504 /* Returns non-zero if the given expression contains an EQ_ATTR with the
2505 `alternative' attribute. */
2508 compares_alternatives_p (exp)
2514 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
2517 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
2518 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
2523 if (compares_alternatives_p (XEXP (exp, i)))
2528 for (j = 0; j < XVECLEN (exp, i); j++)
2529 if (compares_alternatives_p (XVECEXP (exp, i, j)))
2537 /* Returns non-zero is INNER is contained in EXP. */
2540 contained_in_p (inner, exp)
2547 if (rtx_equal_p (inner, exp))
2550 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
2551 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
2556 if (contained_in_p (inner, XEXP (exp, i)))
2561 for (j = 0; j < XVECLEN (exp, i); j++)
2562 if (contained_in_p (inner, XVECEXP (exp, i, j)))
2570 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
2576 struct insn_def *id;
2578 id = (struct insn_def *) xmalloc (sizeof (struct insn_def));
2583 switch (GET_CODE (exp))
2586 id->insn_code = insn_code_number++;
2587 id->insn_index = insn_index_number++;
2588 id->num_alternatives = count_alternatives (exp);
2589 if (id->num_alternatives == 0)
2590 id->num_alternatives = 1;
2594 case DEFINE_PEEPHOLE:
2595 id->insn_code = insn_code_number++;
2596 id->insn_index = insn_index_number++;
2597 id->num_alternatives = count_alternatives (exp);
2598 if (id->num_alternatives == 0)
2599 id->num_alternatives = 1;
2603 case DEFINE_ASM_ATTRIBUTES:
2605 id->insn_index = -1;
2606 id->num_alternatives = 1;
2608 got_define_asm_attributes = 1;
2613 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
2614 true or annul false is specified, and make a `struct delay_desc'. */
2620 struct delay_desc *delay;
2623 if (XVECLEN (def, 1) % 3 != 0)
2624 fatal ("Number of elements in DEFINE_DELAY must be multiple of three.");
2626 for (i = 0; i < XVECLEN (def, 1); i += 3)
2628 if (XVECEXP (def, 1, i + 1))
2629 have_annul_true = 1;
2630 if (XVECEXP (def, 1, i + 2))
2631 have_annul_false = 1;
2634 delay = (struct delay_desc *) xmalloc (sizeof (struct delay_desc));
2636 delay->num = ++num_delays;
2637 delay->next = delays;
2641 /* Process a DEFINE_FUNCTION_UNIT.
2643 This gives information about a function unit contained in the CPU.
2644 We fill in a `struct function_unit_op' and a `struct function_unit'
2645 with information used later by `expand_unit'. */
2651 struct function_unit *unit;
2652 struct function_unit_op *op;
2654 /* See if we have already seen this function unit. If so, check that
2655 the multipicity and simultaneity values are the same. If not, make
2656 a structure for this function unit. */
2657 for (unit = units; unit; unit = unit->next)
2658 if (! strcmp (unit->name, XSTR (def, 0)))
2660 if (unit->multiplicity != XINT (def, 1)
2661 || unit->simultaneity != XINT (def, 2))
2662 fatal ("Differing specifications given for `%s' function unit.",
2669 unit = (struct function_unit *) xmalloc (sizeof (struct function_unit));
2670 unit->name = XSTR (def, 0);
2671 unit->multiplicity = XINT (def, 1);
2672 unit->simultaneity = XINT (def, 2);
2673 unit->num = num_units++;
2674 unit->num_opclasses = 0;
2675 unit->condexp = false_rtx;
2681 /* Make a new operation class structure entry and initialize it. */
2682 op = (struct function_unit_op *) xmalloc (sizeof (struct function_unit_op));
2683 op->condexp = XEXP (def, 3);
2684 op->num = unit->num_opclasses++;
2685 op->ready = XINT (def, 4);
2686 op->next = unit->ops;
2689 /* Set our busy expression based on whether or not an optional conflict
2690 vector was specified. */
2693 /* Compute the IOR of all the specified expressions. */
2694 rtx orexp = false_rtx;
2697 for (i = 0; i < XVECLEN (def, 6); i++)
2698 orexp = insert_right_side (IOR, orexp, XVECEXP (def, 6, i), -2);
2700 op->busyexp = attr_rtx (IF_THEN_ELSE, orexp,
2701 make_numeric_value (XINT (def, 5)),
2702 make_numeric_value (0));
2705 op->busyexp = make_numeric_value (XINT (def, 5));
2707 /* Merge our conditional into that of the function unit so we can determine
2708 which insns are used by the function unit. */
2709 unit->condexp = insert_right_side (IOR, unit->condexp, op->condexp, -2);
2712 /* Given a piece of RTX, print a C expression to test it's truth value.
2713 We use AND and IOR both for logical and bit-wise operations, so
2714 interpret them as logical unless they are inside a comparison expression.
2715 The second operand of this function will be non-zero in that case. */
2718 write_test_expr (exp, in_comparison)
2722 int comparison_operator = 0;
2724 struct attr_desc *attr;
2726 /* In order not to worry about operator precedence, surround our part of
2727 the expression with parentheses. */
2730 code = GET_CODE (exp);
2733 /* Binary operators. */
2735 case GE: case GT: case GEU: case GTU:
2736 case LE: case LT: case LEU: case LTU:
2737 comparison_operator = 1;
2739 case PLUS: case MINUS: case MULT: case DIV: case MOD:
2740 case AND: case IOR: case XOR:
2741 case LSHIFT: case ASHIFT: case LSHIFTRT: case ASHIFTRT:
2742 write_test_expr (XEXP (exp, 0), in_comparison || comparison_operator);
2758 printf (" >= (unsigned) ");
2761 printf (" > (unsigned) ");
2770 printf (" <= (unsigned) ");
2773 printf (" < (unsigned) ");
2815 write_test_expr (XEXP (exp, 1), in_comparison || comparison_operator);
2819 /* Special-case (not (eq_attrq "alternative" "x")) */
2820 if (! in_comparison && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2821 && XSTR (XEXP (exp, 0), 0) == alternative_name)
2823 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
2827 /* Otherwise, fall through to normal unary operator. */
2829 /* Unary operators. */
2847 write_test_expr (XEXP (exp, 0), in_comparison);
2850 /* Comparison test of an attribute with a value. Most of these will
2851 have been removed by optimization. Handle "alternative"
2852 specially and give error if EQ_ATTR present inside a comparison. */
2855 fatal ("EQ_ATTR not valid inside comparison");
2857 if (XSTR (exp, 0) == alternative_name)
2859 printf ("which_alternative == %s", XSTR (exp, 1));
2863 attr = find_attr (XSTR (exp, 0), 0);
2864 if (! attr) abort ();
2865 printf ("get_attr_%s (insn) == ", attr->name);
2866 write_attr_valueq (attr, XSTR (exp, 1));
2869 /* See if an operand matches a predicate. */
2871 /* If only a mode is given, just ensure the mode matches the operand.
2872 If neither a mode nor predicate is given, error. */
2873 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
2875 if (GET_MODE (exp) == VOIDmode)
2876 fatal ("Null MATCH_OPERAND specified as test");
2878 printf ("GET_MODE (operands[%d]) == %smode",
2879 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
2882 printf ("%s (operands[%d], %smode)",
2883 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
2886 /* Constant integer. */
2888 printf ("%d", XINT (exp, 0));
2891 /* A random C expression. */
2893 printf ("%s", XSTR (exp, 0));
2896 /* The address of the branch target. */
2898 printf ("insn_addresses[INSN_UID (JUMP_LABEL (insn))]");
2901 /* The address of the current insn. It would be more consistent with
2902 other usage to make this the address of the NEXT insn, but this gets
2903 too confusing because of the ambiguity regarding the length of the
2906 printf ("insn_current_address");
2910 fatal ("bad RTX code `%s' in attribute calculation\n",
2911 GET_RTX_NAME (code));
2917 /* Given an attribute value, return the maximum CONST_STRING argument
2918 encountered. It is assumed that they are all numeric. */
2921 max_attr_value (exp)
2924 int current_max = 0;
2928 if (GET_CODE (exp) == CONST_STRING)
2929 return atoi (XSTR (exp, 0));
2931 else if (GET_CODE (exp) == COND)
2933 for (i = 0; i < XVECLEN (exp, 0); i += 2)
2935 n = max_attr_value (XVECEXP (exp, 0, i + 1));
2936 if (n > current_max)
2940 n = max_attr_value (XEXP (exp, 1));
2941 if (n > current_max)
2951 /* Scan an attribute value, possibly a conditional, and record what actions
2952 will be required to do any conditional tests in it.
2955 `must_extract' if we need to extract the insn operands
2956 `must_constrain' if we must compute `which_alternative'
2957 `address_used' if an address expression was used
2961 walk_attr_value (exp)
2971 code = GET_CODE (exp);
2975 if (! RTX_UNCHANGING_P (exp))
2976 /* Since this is an arbitrary expression, it can look at anything.
2977 However, constant expressions do not depend on any particular
2979 must_extract = must_constrain = 1;
2987 if (XSTR (exp, 0) == alternative_name)
2988 must_extract = must_constrain = 1;
2997 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
3002 walk_attr_value (XEXP (exp, i));
3006 if (XVEC (exp, i) != NULL)
3007 for (j = 0; j < XVECLEN (exp, i); j++)
3008 walk_attr_value (XVECEXP (exp, i, j));
3013 /* Write out a function to obtain the attribute for a given INSN. */
3016 write_attr_get (attr)
3017 struct attr_desc *attr;
3019 struct attr_value *av, *common_av;
3021 /* Find the most used attribute value. Handle that as the `default' of the
3022 switch we will generate. */
3023 common_av = find_most_used (attr);
3025 /* Write out start of function, then all values with explicit `case' lines,
3026 then a `default', then the value with the most uses. */
3027 if (attr->is_numeric)
3030 printf ("enum attr_%s\n", attr->name);
3032 /* If the attribute name starts with a star, the remainder is the name of
3033 the subroutine to use, instead of `get_attr_...'. */
3034 if (attr->name[0] == '*')
3035 printf ("%s (insn)\n", &attr->name[1]);
3036 else if (attr->is_const == 0)
3037 printf ("get_attr_%s (insn)\n", attr->name);
3040 printf ("get_attr_%s ()\n", attr->name);
3043 for (av = attr->first_value; av; av = av->next)
3044 if (av->num_insns != 0)
3045 write_attr_set (attr, 2, av->value, "return", ";",
3046 true_rtx, av->first_insn->insn_code,
3047 av->first_insn->insn_index);
3052 printf (" rtx insn;\n");
3054 printf (" switch (recog_memoized (insn))\n");
3057 for (av = attr->first_value; av; av = av->next)
3058 if (av != common_av)
3059 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
3061 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
3062 printf (" }\n}\n\n");
3065 /* Given an AND tree of known true terms (because we are inside an `if' with
3066 that as the condition or are in an `else' clause) and an expression,
3067 replace any known true terms with TRUE. Use `simplify_and_tree' to do
3068 the bulk of the work. */
3071 eliminate_known_true (known_true, exp, insn_code, insn_index)
3074 int insn_code, insn_index;
3078 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
3080 if (GET_CODE (known_true) == AND)
3082 exp = eliminate_known_true (XEXP (known_true, 0), exp,
3083 insn_code, insn_index);
3084 exp = eliminate_known_true (XEXP (known_true, 1), exp,
3085 insn_code, insn_index);
3090 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
3096 /* Write out a series of tests and assignment statements to perform tests and
3097 sets of an attribute value. We are passed an indentation amount and prefix
3098 and suffix strings to write around each attribute value (e.g., "return"
3102 write_attr_set (attr, indent, value, prefix, suffix, known_true,
3103 insn_code, insn_index)
3104 struct attr_desc *attr;
3110 int insn_code, insn_index;
3112 if (GET_CODE (value) == CONST_STRING)
3114 write_indent (indent);
3115 printf ("%s ", prefix);
3116 write_attr_value (attr, value);
3117 printf ("%s\n", suffix);
3119 else if (GET_CODE (value) == COND)
3121 /* Assume the default value will be the default of the COND unless we
3122 find an always true expression. */
3123 rtx default_val = XEXP (value, 1);
3124 rtx our_known_true = known_true;
3129 for (i = 0; i < XVECLEN (value, 0); i += 2)
3134 testexp = eliminate_known_true (our_known_true,
3135 XVECEXP (value, 0, i),
3136 insn_code, insn_index);
3137 newexp = attr_rtx (NOT, testexp);
3138 newexp = insert_right_side (AND, our_known_true, newexp,
3139 insn_code, insn_index);
3141 /* If the test expression is always true or if the next `known_true'
3142 expression is always false, this is the last case, so break
3143 out and let this value be the `else' case. */
3144 if (testexp == true_rtx || newexp == false_rtx)
3146 default_val = XVECEXP (value, 0, i + 1);
3150 /* Compute the expression to pass to our recursive call as being
3152 inner_true = insert_right_side (AND, our_known_true,
3153 testexp, insn_code, insn_index);
3155 /* If this is always false, skip it. */
3156 if (inner_true == false_rtx)
3159 write_indent (indent);
3160 printf ("%sif ", first_if ? "" : "else ");
3162 write_test_expr (testexp, 0);
3164 write_indent (indent + 2);
3167 write_attr_set (attr, indent + 4,
3168 XVECEXP (value, 0, i + 1), prefix, suffix,
3169 inner_true, insn_code, insn_index);
3170 write_indent (indent + 2);
3172 our_known_true = newexp;
3177 write_indent (indent);
3179 write_indent (indent + 2);
3183 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
3184 prefix, suffix, our_known_true, insn_code, insn_index);
3188 write_indent (indent + 2);
3196 /* Write out the computation for one attribute value. */
3199 write_attr_case (attr, av, write_case_lines, prefix, suffix, indent, known_true)
3200 struct attr_desc *attr;
3201 struct attr_value *av;
3202 int write_case_lines;
3203 char *prefix, *suffix;
3207 struct insn_ent *ie;
3209 if (av->num_insns == 0)
3212 if (av->has_asm_insn)
3214 write_indent (indent);
3215 printf ("case -1:\n");
3216 write_indent (indent + 2);
3217 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
3218 write_indent (indent + 2);
3219 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
3220 write_indent (indent + 2);
3221 printf (" fatal_insn_not_found (insn);\n");
3224 if (write_case_lines)
3226 for (ie = av->first_insn; ie; ie = ie->next)
3227 if (ie->insn_code != -1)
3229 write_indent (indent);
3230 printf ("case %d:\n", ie->insn_code);
3235 write_indent (indent);
3236 printf ("default:\n");
3239 /* See what we have to do to handle output this value. */
3240 must_extract = must_constrain = address_used = 0;
3241 walk_attr_value (av->value);
3245 write_indent (indent + 2);
3246 printf ("insn_extract (insn);\n");
3251 #ifdef REGISTER_CONSTRAINTS
3252 write_indent (indent + 2);
3253 printf ("if (! constrain_operands (INSN_CODE (insn), reload_completed))\n");
3254 write_indent (indent + 2);
3255 printf (" fatal_insn_not_found (insn);\n");
3259 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
3260 known_true, av->first_insn->insn_code,
3261 av->first_insn->insn_index);
3263 if (strncmp (prefix, "return", 6))
3265 write_indent (indent + 2);
3266 printf ("break;\n");
3271 /* Utilities to write names in various forms. */
3274 write_attr_valueq (attr, s)
3275 struct attr_desc *attr;
3278 if (attr->is_numeric)
3282 write_upcase (attr->name);
3289 write_attr_value (attr, value)
3290 struct attr_desc *attr;
3293 if (GET_CODE (value) != CONST_STRING)
3296 write_attr_valueq (attr, XSTR (value, 0));
3304 if (*str < 'a' || *str > 'z')
3305 printf ("%c", *str++);
3307 printf ("%c", *str++ - 'a' + 'A');
3311 write_indent (indent)
3314 for (; indent > 8; indent -= 8)
3317 for (; indent; indent--)
3321 /* Write a subroutine that is given an insn that requires a delay slot, a
3322 delay slot ordinal, and a candidate insn. It returns non-zero if the
3323 candidate can be placed in the specified delay slot of the insn.
3325 We can write as many as three subroutines. `eligible_for_delay'
3326 handles normal delay slots, `eligible_for_annul_true' indicates that
3327 the specified insn can be annulled if the branch is true, and likewise
3328 for `eligible_for_annul_false'.
3330 KIND is a string distingushing these three cases ("delay", "annul_true",
3331 or "annul_false"). */
3334 write_eligible_delay (kind)
3337 struct delay_desc *delay;
3340 struct attr_desc *attr;
3341 struct attr_value *av, *common_av;
3344 /* Compute the maximum number of delay slots required. We use the delay
3345 ordinal times this number plus one, plus the slot number as an index into
3346 the appropriate predicate to test. */
3348 for (delay = delays, max_slots = 0; delay; delay = delay->next)
3349 if (XVECLEN (delay->def, 1) / 3 > max_slots)
3350 max_slots = XVECLEN (delay->def, 1) / 3;
3352 /* Write function prelude. */
3355 printf ("eligible_for_%s (delay_insn, slot, candidate_insn)\n", kind);
3356 printf (" rtx delay_insn;\n");
3357 printf (" int slot;\n");
3358 printf (" rtx candidate_insn;\n");
3360 printf (" rtx insn;\n");
3362 printf (" if (slot >= %d)\n", max_slots);
3363 printf (" abort ();\n");
3366 /* If more than one delay type, find out which type the delay insn is. */
3370 attr = find_attr ("*delay_type", 0);
3371 if (! attr) abort ();
3372 common_av = find_most_used (attr);
3374 printf (" insn = delay_insn;\n");
3375 printf (" switch (recog_memoized (insn))\n");
3378 sprintf (str, " * %d;\n break;", max_slots);
3379 for (av = attr->first_value; av; av = av->next)
3380 if (av != common_av)
3381 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
3383 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
3386 /* Ensure matched. Otherwise, shouldn't have been called. */
3387 printf (" if (slot < %d)\n", max_slots);
3388 printf (" abort ();\n\n");
3391 /* If just one type of delay slot, write simple switch. */
3392 if (num_delays == 1 && max_slots == 1)
3394 printf (" insn = candidate_insn;\n");
3395 printf (" switch (recog_memoized (insn))\n");
3398 attr = find_attr ("*delay_1_0", 0);
3399 if (! attr) abort ();
3400 common_av = find_most_used (attr);
3402 for (av = attr->first_value; av; av = av->next)
3403 if (av != common_av)
3404 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
3406 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
3412 /* Write a nested CASE. The first indicates which condition we need to
3413 test, and the inner CASE tests the condition. */
3414 printf (" insn = candidate_insn;\n");
3415 printf (" switch (slot)\n");
3418 for (delay = delays; delay; delay = delay->next)
3419 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
3421 printf (" case %d:\n",
3422 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
3423 printf (" switch (recog_memoized (insn))\n");
3426 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
3427 attr = find_attr (str, 0);
3428 if (! attr) abort ();
3429 common_av = find_most_used (attr);
3431 for (av = attr->first_value; av; av = av->next)
3432 if (av != common_av)
3433 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
3435 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
3439 printf (" default:\n");
3440 printf (" abort ();\n");
3447 /* Write routines to compute conflict cost for function units. Then write a
3448 table describing the available function units. */
3451 write_function_unit_info ()
3453 struct function_unit *unit;
3454 struct attr_desc *case_attr, *attr;
3455 struct attr_value *av, *common_av;
3461 /* Write out conflict routines for function units. Don't bother writing
3462 one if there is only one busy value. */
3464 for (unit = units; unit; unit = unit->next)
3466 /* See if only one case exists and if there is a constant value for
3467 that case. If so, we don't need a function. */
3468 str = (char *) alloca (strlen (unit->name) + 10);
3469 sprintf (str, "*%s_cases", unit->name);
3470 attr = find_attr (str, 0);
3471 if (! attr) abort ();
3472 value = find_single_value (attr);
3473 if (value && GET_CODE (value) == CONST_STRING)
3475 sprintf (str, "*%s_case_%s", unit->name, XSTR (value, 0));
3476 attr = find_attr (str, 0);
3477 if (! attr) abort ();
3478 value = find_single_value (attr);
3479 if (value && GET_CODE (value) == CONST_STRING)
3481 unit->needs_conflict_function = 0;
3482 unit->default_cost = value;
3487 /* The function first computes the case from the candidate insn. */
3488 unit->needs_conflict_function = 1;
3489 unit->default_cost = make_numeric_value (0);
3491 printf ("static int\n");
3492 printf ("%s_unit_conflict_cost (executing_insn, candidate_insn)\n",
3494 printf (" rtx executing_insn;\n");
3495 printf (" rtx candidate_insn;\n");
3497 printf (" rtx insn;\n");
3498 printf (" int casenum;\n\n");
3499 printf (" insn = candidate_insn;\n");
3500 printf (" switch (recog_memoized (insn))\n");
3503 /* Write the `switch' statement to get the case value. */
3504 sprintf (str, "*%s_cases", unit->name);
3505 case_attr = find_attr (str, 0);
3506 if (! case_attr) abort ();
3507 common_av = find_most_used (case_attr);
3509 for (av = case_attr->first_value; av; av = av->next)
3510 if (av != common_av)
3511 write_attr_case (case_attr, av, 1,
3512 "casenum =", ";", 4, unit->condexp);
3514 write_attr_case (case_attr, common_av, 0,
3515 "casenum =", ";", 4, unit->condexp);
3518 /* Now write an outer switch statement on each case. Then write
3519 the tests on the executing function within each. */
3520 printf (" insn = executing_insn;\n");
3521 printf (" switch (casenum)\n");
3524 for (i = 0; i < unit->num_opclasses; i++)
3526 /* Ensure using this case. */
3528 for (av = case_attr->first_value; av; av = av->next)
3530 && contained_in_p (make_numeric_value (i), av->value))
3536 printf (" case %d:\n", i);
3537 sprintf (str, "*%s_case_%d", unit->name, i);
3538 attr = find_attr (str, 0);
3539 if (! attr) abort ();
3541 /* If single value, just write it. */
3542 value = find_single_value (attr);
3544 write_attr_set (attr, 6, value, "return", ";\n", true_rtx, -2);
3547 common_av = find_most_used (attr);
3548 printf (" switch (recog_memoized (insn))\n");
3551 for (av = attr->first_value; av; av = av->next)
3552 if (av != common_av)
3553 write_attr_case (attr, av, 1,
3554 "return", ";", 8, unit->condexp);
3556 write_attr_case (attr, common_av, 0,
3557 "return", ";", 8, unit->condexp);
3562 printf (" }\n}\n\n");
3565 /* Now that all functions have been written, write the table describing
3566 the function units. The name is included for documenation purposes
3569 printf ("struct function_unit_desc function_units[] = {\n");
3571 for (unit = units; unit; unit = unit->next)
3573 printf (" {\"%s\", %d, %d, %d, %s, %s_unit_ready_cost, ",
3574 unit->name, 1 << unit->num, unit->multiplicity,
3575 unit->simultaneity, XSTR (unit->default_cost, 0), unit->name);
3577 if (unit->needs_conflict_function)
3578 printf ("%s_unit_conflict_cost", unit->name);
3588 /* This page contains miscellaneous utility routines. */
3590 /* Given a string, return the number of comma-separated elements in it.
3591 Return 0 for the null string. */
3602 for (n = 1; *s; s++)
3609 /* Given a pointer to a (char *), return a malloc'ed string containing the
3610 next comma-separated element. Advance the pointer to after the string
3611 scanned, or the end-of-string. Return NULL if at end of string. */
3614 next_comma_elt (pstr)
3623 /* Find end of string to compute length. */
3624 for (p = *pstr; *p != ',' && *p != '\0'; p++)
3627 out_str = attr_string (*pstr, p - *pstr);
3636 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
3637 is non-zero, build a new attribute, if one does not exist. */
3639 static struct attr_desc *
3640 find_attr (name, create)
3644 struct attr_desc *attr;
3647 /* Before we resort to using `strcmp', see if the string address matches
3648 anywhere. In most cases, it should have been canonicalized to do so. */
3649 if (name == alternative_name)
3652 for (attr = attrs; attr; attr = attr->next)
3653 if (name == attr->name)
3656 /* Otherwise, do it the slow way. */
3657 for (attr = attrs; attr; attr = attr->next)
3658 if (! strcmp (name, attr->name))
3664 new_name = (char *) xmalloc (strlen (name) + 1);
3665 strcpy (new_name, name);
3667 attr = (struct attr_desc *) xmalloc (sizeof (struct attr_desc));
3668 attr->name = new_name;
3669 attr->first_value = attr->default_val = NULL;
3670 attr->is_numeric = attr->is_const = attr->is_special = 0;
3677 /* Create internal attribute with the given default value. */
3680 make_internal_attr (name, value, special)
3685 struct attr_desc *attr;
3687 attr = find_attr (name, 1);
3688 if (attr->default_val)
3691 attr->is_numeric = 1;
3693 attr->is_special = special;
3694 attr->default_val = get_attr_value (value, attr, -2);
3697 /* Find the most used value of an attribute. */
3699 static struct attr_value *
3700 find_most_used (attr)
3701 struct attr_desc *attr;
3703 struct attr_value *av;
3704 struct attr_value *most_used;
3710 for (av = attr->first_value; av; av = av->next)
3711 if (av->num_insns > nuses)
3712 nuses = av->num_insns, most_used = av;
3717 /* If an attribute only has a single value used, return it. Otherwise
3721 find_single_value (attr)
3722 struct attr_desc *attr;
3724 struct attr_value *av;
3727 unique_value = NULL;
3728 for (av = attr->first_value; av; av = av->next)
3734 unique_value = av->value;
3737 return unique_value;
3740 /* Return (attr_value "n") */
3743 make_numeric_value (n)
3746 static rtx int_values[20];
3753 if (n < 20 && int_values[n])
3754 return int_values[n];
3756 p = attr_printf ((n < 1000 ? 4 : HOST_BITS_PER_INT * 3 / 10 + 3), "%d", n);
3757 exp = attr_rtx (CONST_STRING, p);
3760 int_values[n] = exp;
3766 xrealloc (ptr, size)
3770 char *result = (char *) realloc (ptr, size);
3772 fatal ("virtual memory exhausted");
3780 register char *val = (char *) malloc (size);
3783 fatal ("virtual memory exhausted");
3791 fprintf (stderr, "genattrtab: ");
3792 fprintf (stderr, s, a1, a2);
3793 fprintf (stderr, "\n");
3794 exit (FATAL_EXIT_CODE);
3797 /* More 'friendly' abort that prints the line and file.
3798 config.h can #define abort fancy_abort if you like that sort of thing. */
3803 fatal ("Internal gcc abort.");
3813 extern rtx read_rtx ();
3815 struct attr_desc *attr;
3816 struct attr_value *av;
3817 struct insn_def *id;
3820 obstack_init (rtl_obstack);
3823 fatal ("No input file name.");
3825 infile = fopen (argv[1], "r");
3829 exit (FATAL_EXIT_CODE);
3834 /* Set up true and false rtx's */
3835 true_rtx = attr_rtx (CONST_INT, 1);
3836 false_rtx = attr_rtx (CONST_INT, 0);
3837 RTX_UNCHANGING_P (true_rtx) = RTX_UNCHANGING_P (false_rtx) = 1;
3839 printf ("/* Generated automatically by the program `genattrtab'\n\
3840 from the machine description file `md'. */\n\n");
3842 /* Read the machine description. */
3846 c = read_skip_spaces (infile);
3851 desc = read_rtx (infile);
3852 if (GET_CODE (desc) == DEFINE_INSN
3853 || GET_CODE (desc) == DEFINE_PEEPHOLE
3854 || GET_CODE (desc) == DEFINE_ASM_ATTRIBUTES)
3857 else if (GET_CODE (desc) == DEFINE_EXPAND)
3858 insn_code_number++, insn_index_number++;
3860 else if (GET_CODE (desc) == DEFINE_SPLIT)
3861 insn_code_number++, insn_index_number++;
3863 else if (GET_CODE (desc) == DEFINE_ATTR)
3866 insn_index_number++;
3869 else if (GET_CODE (desc) == DEFINE_DELAY)
3872 insn_index_number++;
3875 else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
3878 insn_index_number++;
3882 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
3883 if (! got_define_asm_attributes)
3885 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
3886 XVEC (tem, 0) = rtvec_alloc (0);
3890 /* Expand DEFINE_DELAY information into new attribute. */
3894 /* Expand DEFINE_FUNCTION_UNIT information into new attributes. */
3898 printf ("#include \"config.h\"\n");
3899 printf ("#include \"rtl.h\"\n");
3900 printf ("#include \"insn-config.h\"\n");
3901 printf ("#include \"recog.h\"\n");
3902 printf ("#include \"regs.h\"\n");
3903 printf ("#include \"real.h\"\n");
3904 printf ("#include \"output.h\"\n");
3905 printf ("#include \"insn-attr.h\"\n");
3907 printf ("#define operands recog_operand\n\n");
3909 /* Make `insn_alternatives'. */
3910 insn_alternatives = (int *) xmalloc (insn_code_number * sizeof (int));
3911 for (id = defs; id; id = id->next)
3912 if (id->insn_code >= 0)
3913 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
3915 /* Prepare to write out attribute subroutines by checking everything stored
3916 away and building the attribute cases. */
3919 for (attr = attrs; attr; attr = attr->next)
3921 check_attr_value (attr->default_val->value, attr);
3925 /* Construct extra attributes for `length'. */
3926 make_length_attrs ();
3928 /* Perform any possible optimizations to speed up compilation. */
3931 /* Now write out all the `gen_attr_...' routines. Do these before the
3932 special routines (specifically before write_function_unit_info), so
3933 that they get defined before they are used. */
3935 for (attr = attrs; attr; attr = attr->next)
3937 if (! attr->is_special)
3938 write_attr_get (attr);
3941 /* Write out delay eligibility information, if DEFINE_DELAY present.
3942 (The function to compute the number of delay slots will be written
3946 write_eligible_delay ("delay");
3947 if (have_annul_true)
3948 write_eligible_delay ("annul_true");
3949 if (have_annul_false)
3950 write_eligible_delay ("annul_false");
3953 /* Write out information about function units. */
3955 write_function_unit_info ();
3958 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);