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).
86 We use the flags in an RTX as follows:
87 `unchanging' (RTX_UNCHANGING_P): This rtx is fully simplified
88 independent of the insn code.
89 `in_struct' (MEM_IN_STRUCT_P): This rtx is fully simplified
90 for the insn code currently being processed (see optimize_attrs).
91 `integrated' (RTX_INTEGRATED_P): This rtx is permanent and unique
100 #include "insn-config.h" /* For REGISTER_CONSTRAINTS */
102 static struct obstack obstack, obstack1, obstack2;
103 struct obstack *rtl_obstack = &obstack;
104 struct obstack *hash_obstack = &obstack1;
105 struct obstack *temp_obstack = &obstack2;
107 #define obstack_chunk_alloc xmalloc
108 #define obstack_chunk_free free
110 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
111 char **insn_name_ptr = 0;
114 extern rtx read_rtx ();
116 static void fatal ();
119 /* Define structures used to record attributes and values. */
121 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
122 encountered, we store all the relevant information into a
123 `struct insn_def'. This is done to allow attribute definitions to occur
124 anywhere in the file. */
128 int insn_code; /* Instruction number. */
129 int insn_index; /* Expression numer in file, for errors. */
130 struct insn_def *next; /* Next insn in chain. */
131 rtx def; /* The DEFINE_... */
132 int num_alternatives; /* Number of alternatives. */
133 int vec_idx; /* Index of attribute vector in `def'. */
136 /* Once everything has been read in, we store in each attribute value a list
137 of insn codes that have that value. Here is the structure used for the
142 int insn_code; /* Instruction number. */
143 int insn_index; /* Index of definition in file */
144 struct insn_ent *next; /* Next in chain. */
147 /* Each value of an attribute (either constant or computed) is assigned a
148 structure which is used as the listhead of the insns that have that
153 rtx value; /* Value of attribute. */
154 struct attr_value *next; /* Next attribute value in chain. */
155 struct insn_ent *first_insn; /* First insn with this value. */
156 int num_insns; /* Number of insns with this value. */
157 int has_asm_insn; /* True if this value used for `asm' insns */
160 /* Structure for each attribute. */
164 char *name; /* Name of attribute. */
165 struct attr_desc *next; /* Next attribute. */
166 int is_numeric; /* Values of this attribute are numeric. */
167 int is_const; /* Attribute value constant for each run. */
168 int is_special; /* Don't call `write_attr_set'. */
169 struct attr_value *first_value; /* First value of this attribute. */
170 struct attr_value *default_val; /* Default value for this attribute. */
173 /* Structure for each DEFINE_DELAY. */
177 rtx def; /* DEFINE_DELAY expression. */
178 struct delay_desc *next; /* Next DEFINE_DELAY. */
179 int num; /* Number of DEFINE_DELAY, starting at 1. */
182 /* Record information about each DEFINE_FUNCTION_UNIT. */
184 struct function_unit_op
186 rtx condexp; /* Expression TRUE for applicable insn. */
187 struct function_unit_op *next; /* Next operation for this function unit. */
188 int num; /* Ordinal for this operation type in unit. */
189 int ready; /* Cost until data is ready. */
190 rtx busyexp; /* Expression computing conflict cost. */
193 /* Record information about each function unit mentioned in a
194 DEFINE_FUNCTION_UNIT. */
198 char *name; /* Function unit name. */
199 struct function_unit *next; /* Next function unit. */
200 int num; /* Ordinal of this unit type. */
201 int multiplicity; /* Number of units of this type. */
202 int simultaneity; /* Maximum number of simultaneous insns
203 on this function unit or 0 if unlimited. */
204 rtx condexp; /* Expression TRUE for insn needing unit. */
205 rtx costexp; /* Worst-case cost as function of insn. */
206 int num_opclasses; /* Number of different operation types. */
207 struct function_unit_op *ops; /* Pointer to first operation type. */
208 int needs_conflict_function; /* Nonzero if a conflict function required. */
209 rtx default_cost; /* Conflict cost, if constant. */
212 /* Listheads of above structures. */
214 /* This one is indexed by the first character of the attribute name. */
215 #define MAX_ATTRS_INDEX 256
216 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
217 static struct insn_def *defs;
218 static struct delay_desc *delays;
219 static struct function_unit *units;
221 /* Other variables. */
223 static int insn_code_number;
224 static int insn_index_number;
225 static int got_define_asm_attributes;
226 static int must_extract;
227 static int must_constrain;
228 static int address_used;
229 static int num_delays;
230 static int have_annul_true, have_annul_false;
231 static int num_units;
233 /* Used as operand to `operate_exp': */
235 enum operator {PLUS_OP, MINUS_OP, OR_OP, MAX_OP};
237 /* Stores, for each insn code, the number of constraint alternatives. */
239 static int *insn_n_alternatives;
241 /* Stores, for each insn code, a bitmap that has bits on for each possible
244 static int *insn_alternatives;
246 /* If nonzero, assume that the `alternative' attr has this value.
247 This is the hashed, unique string for the numeral
248 whose value is chosen alternative. */
250 static char *current_alternative_string;
252 /* Used to simplify expressions. */
254 static rtx true_rtx, false_rtx;
256 /* Used to reduce calls to `strcmp' */
258 static char *alternative_name;
260 /* Simplify an expression. Only call the routine if there is something to
262 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
263 (RTX_UNCHANGING_P (EXP) || MEM_IN_STRUCT_P (EXP) ? (EXP) \
264 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
266 /* Simplify (eq_attr ("alternative") ...)
267 when we are working with a particular alternative. */
268 #define SIMPLIFY_ALTERNATIVE(EXP) \
269 if (current_alternative_string \
270 && GET_CODE ((EXP)) == EQ_ATTR \
271 && XSTR ((EXP), 0) == alternative_name) \
272 (EXP) = (XSTR ((EXP), 1) == current_alternative_string \
273 ? true_rtx : false_rtx);
275 /* These are referenced by rtlanal.c and hence need to be defined somewhere.
276 They won't actually be used. */
278 rtx frame_pointer_rtx, stack_pointer_rtx, arg_pointer_rtx;
280 static rtx attr_rtx ();
281 static char *attr_printf ();
282 static char *attr_string ();
283 static rtx check_attr_test ();
284 static rtx check_attr_value ();
285 static rtx convert_set_attr_alternative ();
286 static rtx convert_set_attr ();
287 static void check_defs ();
288 static rtx convert_const_symbol_ref ();
289 static rtx make_canonical ();
290 static struct attr_value *get_attr_value ();
291 static rtx copy_rtx_unchanging ();
292 static rtx copy_boolean ();
293 static void expand_delays ();
294 static rtx operate_exp ();
295 static void expand_units ();
296 static void fill_attr ();
297 static rtx substitute_address ();
298 static void make_length_attrs ();
299 static rtx identity_fn ();
300 static rtx zero_fn ();
301 static rtx one_fn ();
302 static rtx max_fn ();
303 static rtx simplify_cond ();
304 static rtx simplify_by_alternatives ();
305 static void remove_insn_ent ();
306 static void insert_insn_ent ();
307 static rtx insert_right_side ();
308 static rtx make_alternative_compare ();
309 static int compute_alternative_mask ();
310 static rtx evaluate_eq_attr ();
311 static rtx simplify_and_tree ();
312 static rtx simplify_or_tree ();
313 static rtx simplify_test_exp ();
314 static void optimize_attrs ();
315 static void gen_attr ();
316 static int count_alternatives ();
317 static int compares_alternatives_p ();
318 static int contained_in_p ();
319 static void gen_insn ();
320 static void gen_delay ();
321 static void gen_unit ();
322 static void write_test_expr ();
323 static int max_attr_value ();
324 static void walk_attr_value ();
325 static void write_attr_get ();
326 static rtx eliminate_known_true ();
327 static void write_attr_set ();
328 static void write_attr_case ();
329 static void write_attr_value ();
330 static void write_attr_valueq ();
331 static void write_upcase ();
332 static void write_indent ();
333 static void write_eligible_delay ();
334 static void write_function_unit_info ();
335 static int n_comma_elts ();
336 static char *next_comma_elt ();
337 static struct attr_desc *find_attr ();
338 static void make_internal_attr ();
339 static struct attr_value *find_most_used ();
340 static rtx find_single_value ();
341 static rtx make_numeric_value ();
344 static void fatal ();
346 /* Hash table for sharing RTL and strings. */
348 /* Each hash table slot is a bucket containing a chain of these structures.
349 Strings are given negative hash codes; RTL expressions are given positive
354 struct attr_hash *next; /* Next structure in the bucket. */
355 int hashcode; /* Hash code of this rtx or string. */
358 char *str; /* The string (negative hash codes) */
359 rtx rtl; /* or the RTL recorded here. */
363 /* Now here is the hash table. When recording an RTL, it is added to
364 the slot whose index is the hash code mod the table size. Note
365 that the hash table is used for several kinds of RTL (see attr_rtx)
366 and for strings. While all these live in the same table, they are
367 completely independent, and the hash code is computed differently
370 #define RTL_HASH_SIZE 4093
371 struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
373 /* Here is how primitive or already-shared RTL's hash
375 #define RTL_HASH(RTL) ((int) (RTL) & 0777777)
377 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
380 attr_hash_add_rtx (hashcode, rtl)
384 register struct attr_hash *h;
386 h = (struct attr_hash *) obstack_alloc (hash_obstack,
387 sizeof (struct attr_hash));
388 h->hashcode = hashcode;
390 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
391 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
394 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
397 attr_hash_add_string (hashcode, str)
401 register struct attr_hash *h;
403 h = (struct attr_hash *) obstack_alloc (hash_obstack,
404 sizeof (struct attr_hash));
405 h->hashcode = -hashcode;
407 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
408 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
411 /* Generate an RTL expression, but avoid duplicates.
412 Set the RTX_INTEGRATED_P flag for these permanent objects.
414 In some cases we cannot uniquify; then we return an ordinary
415 impermanent rtx with RTX_INTEGRATED_P clear.
417 Args are like gen_rtx, but without the mode:
419 rtx attr_rtx (code, [element1, ..., elementn]) */
428 register int i; /* Array indices... */
429 register char *fmt; /* Current rtx's format... */
430 register rtx rt_val; /* RTX to return to caller... */
432 register struct attr_hash *h;
433 struct obstack *old_obstack = rtl_obstack;
436 code = va_arg (p, enum rtx_code);
438 /* For each of several cases, search the hash table for an existing entry.
439 Use that entry if one is found; otherwise create a new RTL and add it
442 if (GET_RTX_CLASS (code) == '1')
444 rtx arg0 = va_arg (p, rtx);
446 /* A permanent object cannot point to impermanent ones. */
447 if (! RTX_INTEGRATED_P (arg0))
449 rt_val = rtx_alloc (code);
450 XEXP (rt_val, 0) = arg0;
455 hashcode = ((int) code + RTL_HASH (arg0));
456 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
457 if (h->hashcode == hashcode
458 && GET_CODE (h->u.rtl) == code
459 && XEXP (h->u.rtl, 0) == arg0)
464 rtl_obstack = hash_obstack;
465 rt_val = rtx_alloc (code);
466 XEXP (rt_val, 0) = arg0;
469 else if (GET_RTX_CLASS (code) == 'c'
470 || GET_RTX_CLASS (code) == '2'
471 || GET_RTX_CLASS (code) == '<')
473 rtx arg0 = va_arg (p, rtx);
474 rtx arg1 = va_arg (p, rtx);
476 /* A permanent object cannot point to impermanent ones. */
477 if (! RTX_INTEGRATED_P (arg0) || ! RTX_INTEGRATED_P (arg1))
479 rt_val = rtx_alloc (code);
480 XEXP (rt_val, 0) = arg0;
481 XEXP (rt_val, 1) = arg1;
486 hashcode = ((int) code + RTL_HASH (arg0) + RTL_HASH (arg1));
487 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
488 if (h->hashcode == hashcode
489 && GET_CODE (h->u.rtl) == code
490 && XEXP (h->u.rtl, 0) == arg0
491 && XEXP (h->u.rtl, 1) == arg1)
496 rtl_obstack = hash_obstack;
497 rt_val = rtx_alloc (code);
498 XEXP (rt_val, 0) = arg0;
499 XEXP (rt_val, 1) = arg1;
502 else if (GET_RTX_LENGTH (code) == 1
503 && GET_RTX_FORMAT (code)[0] == 's')
505 char * arg0 = va_arg (p, char *);
507 if (code == SYMBOL_REF)
508 arg0 = attr_string (arg0, strlen (arg0));
510 hashcode = ((int) code + RTL_HASH (arg0));
511 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
512 if (h->hashcode == hashcode
513 && GET_CODE (h->u.rtl) == code
514 && XSTR (h->u.rtl, 0) == arg0)
519 rtl_obstack = hash_obstack;
520 rt_val = rtx_alloc (code);
521 XSTR (rt_val, 0) = arg0;
524 else if (GET_RTX_LENGTH (code) == 2
525 && GET_RTX_FORMAT (code)[0] == 's'
526 && GET_RTX_FORMAT (code)[1] == 's')
528 char *arg0 = va_arg (p, char *);
529 char *arg1 = va_arg (p, char *);
531 hashcode = ((int) code + RTL_HASH (arg0) + RTL_HASH (arg1));
532 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
533 if (h->hashcode == hashcode
534 && GET_CODE (h->u.rtl) == code
535 && XSTR (h->u.rtl, 0) == arg0
536 && XSTR (h->u.rtl, 1) == arg1)
541 rtl_obstack = hash_obstack;
542 rt_val = rtx_alloc (code);
543 XSTR (rt_val, 0) = arg0;
544 XSTR (rt_val, 1) = arg1;
547 else if (code == CONST_INT)
549 int arg0 = va_arg (p, int);
559 rt_val = rtx_alloc (code); /* Allocate the storage space. */
561 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
562 for (i = 0; i < GET_RTX_LENGTH (code); i++)
566 case '0': /* Unused field. */
569 case 'i': /* An integer? */
570 XINT (rt_val, i) = va_arg (p, int);
573 case 's': /* A string? */
574 XSTR (rt_val, i) = va_arg (p, char *);
577 case 'e': /* An expression? */
578 case 'u': /* An insn? Same except when printing. */
579 XEXP (rt_val, i) = va_arg (p, rtx);
582 case 'E': /* An RTX vector? */
583 XVEC (rt_val, i) = va_arg (p, rtvec);
594 rtl_obstack = old_obstack;
596 attr_hash_add_rtx (hashcode, rt_val);
597 RTX_INTEGRATED_P (rt_val) = 1;
605 /* Create a new string printed with the printf line arguments into a space
606 of at most LEN bytes:
608 rtx attr_printf (len, format, [arg1, ..., argn]) */
614 attr_printf (va_alist)
622 /* Print the string into a temporary location. */
624 len = va_arg (p, int);
625 str = (char *) alloca (len);
626 fmt = va_arg (p, char *);
627 vsprintf (str, fmt, p);
630 return attr_string (str, strlen (str));
633 #else /* not HAVE_VPRINTF */
636 attr_printf (len, fmt, arg1, arg2, arg3)
639 char *arg1, *arg2, *arg3; /* also int */
643 /* Print the string into a temporary location. */
644 str = (char *) alloca (len);
645 sprintf (str, fmt, arg1, arg2, arg3);
647 return attr_string (str, strlen (str));
649 #endif /* not HAVE_VPRINTF */
652 attr_eq (name, value)
655 return attr_rtx (EQ_ATTR, attr_string (name, strlen (name)),
656 attr_string (value, strlen (value)));
663 return XSTR (make_numeric_value (n), 0);
666 /* Return a permanent (possibly shared) copy of a string STR (not assumed
667 to be null terminated) with LEN bytes. */
670 attr_string (str, len)
674 register struct attr_hash *h;
677 register char *new_str;
679 /* Compute the hash code. */
680 hashcode = (len + 1) * 613 + (unsigned)str[0];
681 for (i = 1; i <= len; i += 2)
682 hashcode = ((hashcode * 613) + (unsigned)str[i]);
684 hashcode = -hashcode;
686 /* Search the table for the string. */
687 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
688 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
689 && !strncmp (h->u.str, str, len))
690 return h->u.str; /* <-- return if found. */
692 /* Not found; create a permanent copy and add it to the hash table. */
693 new_str = (char *) xmalloc (len + 1);
694 bcopy (str, new_str, len);
696 attr_hash_add_string (hashcode, new_str);
698 return new_str; /* Return the new string. */
701 /* Check two rtx's for equality of contents,
702 taking advantage of the fact that if both are hashed
703 then they can't be equal unless they are the same object. */
709 return (x == y || (! (RTX_INTEGRATED_P (x) && RTX_INTEGRATED_P (y))
710 && rtx_equal_p (x, y)));
713 /* Copy an attribute value expression,
714 descending to all depths, but not copying any
715 permanent hashed subexpressions. */
723 register RTX_CODE code;
724 register char *format_ptr;
726 /* No need to copy a permanent object. */
727 if (RTX_INTEGRATED_P (orig))
730 code = GET_CODE (orig);
745 copy = rtx_alloc (code);
746 PUT_MODE (copy, GET_MODE (orig));
747 copy->in_struct = orig->in_struct;
748 copy->volatil = orig->volatil;
749 copy->unchanging = orig->unchanging;
750 copy->integrated = orig->integrated;
752 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
754 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
756 switch (*format_ptr++)
759 XEXP (copy, i) = XEXP (orig, i);
760 if (XEXP (orig, i) != NULL)
761 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
766 XVEC (copy, i) = XVEC (orig, i);
767 if (XVEC (orig, i) != NULL)
769 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
770 for (j = 0; j < XVECLEN (copy, i); j++)
771 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
776 XINT (copy, i) = XINT (orig, i);
783 /* Given a test expression for an attribute, ensure it is validly formed.
784 IS_CONST indicates whether the expression is constant for each compiler
785 run (a constant expression may not test any particular insn).
787 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
788 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
789 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
791 Update the string address in EQ_ATTR expression to be the same used
792 in the attribute (or `alternative_name') to speed up subsequent
793 `find_attr' calls and eliminate most `strcmp' calls.
795 Return the new expression, if any. */
798 check_attr_test (exp, is_const)
802 struct attr_desc *attr;
803 struct attr_value *av;
807 switch (GET_CODE (exp))
810 /* Handle negation test. */
811 if (XSTR (exp, 1)[0] == '!')
812 return check_attr_test (attr_rtx (NOT,
813 attr_eq (XSTR (exp, 0),
817 else if (n_comma_elts (XSTR (exp, 1)) == 1)
819 attr = find_attr (XSTR (exp, 0), 0);
822 if (! strcmp (XSTR (exp, 0), "alternative"))
824 XSTR (exp, 0) = alternative_name;
825 /* This can't be simplified any further. */
826 RTX_UNCHANGING_P (exp) = 1;
830 fatal ("Unknown attribute `%s' in EQ_ATTR", XEXP (exp, 0));
833 if (is_const && ! attr->is_const)
834 fatal ("Constant expression uses insn attribute `%s' in EQ_ATTR",
837 /* Copy this just to make it permanent,
838 so expressions using it can be permanent too. */
839 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
841 if (attr->is_numeric)
843 for (p = XSTR (exp, 1); *p; p++)
844 if (*p < '0' || *p > '9')
845 fatal ("Attribute `%s' takes only numeric values",
850 for (av = attr->first_value; av; av = av->next)
851 if (GET_CODE (av->value) == CONST_STRING
852 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
856 fatal ("Unknown value `%s' for `%s' attribute",
857 XEXP (exp, 1), XEXP (exp, 0));
862 /* Make an IOR tree of the possible values. */
864 name_ptr = XSTR (exp, 1);
865 while ((p = next_comma_elt (&name_ptr)) != NULL)
867 newexp = attr_eq (XSTR (exp, 0), p);
868 orexp = insert_right_side (IOR, orexp, newexp, -2);
871 return check_attr_test (orexp, is_const);
876 /* Either TRUE or FALSE. */
884 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const);
885 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const);
889 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const);
894 fatal ("RTL operator \"%s\" not valid in constant attribute test",
895 GET_RTX_NAME (MATCH_OPERAND));
896 /* These cases can't be simplified. */
897 RTX_UNCHANGING_P (exp) = 1;
900 case LE: case LT: case GT: case GE:
901 case LEU: case LTU: case GTU: case GEU:
903 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
904 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
905 exp = attr_rtx (GET_CODE (exp),
906 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
907 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
908 /* These cases can't be simplified. */
909 RTX_UNCHANGING_P (exp) = 1;
915 /* These cases are valid for constant attributes, but can't be
917 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
918 RTX_UNCHANGING_P (exp) = 1;
922 fatal ("RTL operator \"%s\" not valid in attribute test",
923 GET_RTX_NAME (GET_CODE (exp)));
929 /* Given an expression, ensure that it is validly formed and that all named
930 attribute values are valid for the given attribute. Issue a fatal error
931 if not. If no attribute is specified, assume a numeric attribute.
933 Return a perhaps modified replacement expression for the value. */
936 check_attr_value (exp, attr)
938 struct attr_desc *attr;
940 struct attr_value *av;
944 switch (GET_CODE (exp))
947 if (attr && ! attr->is_numeric)
948 fatal ("CONST_INT not valid for non-numeric `%s' attribute",
951 if (INTVAL (exp) < 0)
952 fatal ("Negative numeric value specified for `%s' attribute",
958 if (! strcmp (XSTR (exp, 0), "*"))
961 if (attr == 0 || attr->is_numeric)
963 for (p = XSTR (exp, 0); *p; p++)
964 if (*p > '9' || *p < '0')
965 fatal ("Non-numeric value for numeric `%s' attribute",
966 attr ? attr->name : "internal");
970 for (av = attr->first_value; av; av = av->next)
971 if (GET_CODE (av->value) == CONST_STRING
972 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
976 fatal ("Unknown value `%s' for `%s' attribute",
977 XSTR (exp, 0), attr ? attr->name : "internal");
982 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
983 attr ? attr->is_const : 0);
984 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
985 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
989 if (XVECLEN (exp, 0) % 2 != 0)
990 fatal ("First operand of COND must have even length");
992 for (i = 0; i < XVECLEN (exp, 0); i += 2)
994 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
995 attr ? attr->is_const : 0);
996 XVECEXP (exp, 0, i + 1)
997 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
1000 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
1004 if (attr && attr->is_const)
1005 /* A constant SYMBOL_REF is valid as a constant attribute test and
1006 is expanded later by make_canonical into a COND. */
1007 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1008 /* Otherwise, fall through... */
1011 fatal ("Illegal operation `%s' for attribute value",
1012 GET_RTX_NAME (GET_CODE (exp)));
1018 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1019 It becomes a COND with each test being (eq_attr "alternative "n") */
1022 convert_set_attr_alternative (exp, num_alt, insn_code, insn_index)
1025 int insn_code, insn_index;
1031 if (XVECLEN (exp, 1) != num_alt)
1032 fatal ("Bad number of entries in SET_ATTR_ALTERNATIVE for insn %d",
1035 /* Make a COND with all tests but the last. Select the last value via the
1037 condexp = rtx_alloc (COND);
1038 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1040 for (i = 0; i < num_alt - 1; i++)
1043 p = attr_numeral (i);
1045 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1047 /* Sharing this EQ_ATTR rtl causes trouble. */
1048 XVECEXP (condexp, 0, 2 * i) = rtx_alloc (EQ_ATTR);
1049 XSTR (XVECEXP (condexp, 0, 2 * i), 0) = alternative_name;
1050 XSTR (XVECEXP (condexp, 0, 2 * i), 1) = p;
1052 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1055 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1057 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1060 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1061 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1064 convert_set_attr (exp, num_alt, insn_code, insn_index)
1067 int insn_code, insn_index;
1074 /* See how many alternative specified. */
1075 n = n_comma_elts (XSTR (exp, 1));
1077 return attr_rtx (SET,
1078 attr_rtx (ATTR, XSTR (exp, 0)),
1079 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1081 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1082 XSTR (newexp, 0) = XSTR (exp, 0);
1083 XVEC (newexp, 1) = rtvec_alloc (n);
1085 /* Process each comma-separated name. */
1086 name_ptr = XSTR (exp, 1);
1088 while ((p = next_comma_elt (&name_ptr)) != NULL)
1089 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1091 return convert_set_attr_alternative (newexp, num_alt, insn_code, insn_index);
1094 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1095 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1101 struct insn_def *id;
1102 struct attr_desc *attr;
1106 for (id = defs; id; id = id->next)
1108 if (XVEC (id->def, id->vec_idx) == NULL)
1111 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1113 value = XVECEXP (id->def, id->vec_idx, i);
1114 switch (GET_CODE (value))
1117 if (GET_CODE (XEXP (value, 0)) != ATTR)
1118 fatal ("Bad attribute set in pattern %d", id->insn_index);
1121 case SET_ATTR_ALTERNATIVE:
1122 value = convert_set_attr_alternative (value,
1123 id->num_alternatives,
1129 value = convert_set_attr (value, id->num_alternatives,
1130 id->insn_code, id->insn_index);
1134 fatal ("Invalid attribute code `%s' for pattern %d",
1135 GET_RTX_NAME (GET_CODE (value)), id->insn_index);
1138 if ((attr = find_attr (XSTR (XEXP (value, 0), 0), 0)) == NULL)
1139 fatal ("Unknown attribute `%s' for pattern number %d",
1140 XSTR (XEXP (value, 0), 0), id->insn_index);
1142 XVECEXP (id->def, id->vec_idx, i) = value;
1143 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1148 /* Given a constant SYMBOL_REF expression, convert to a COND that
1149 explicitly tests each enumerated value. */
1152 convert_const_symbol_ref (exp, attr)
1154 struct attr_desc *attr;
1157 struct attr_value *av;
1161 for (av = attr->first_value; av; av = av->next)
1164 /* Make a COND with all tests but the last, and in the original order.
1165 Select the last value via the default. Note that the attr values
1166 are constructed in reverse order. */
1168 condexp = rtx_alloc (COND);
1169 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1170 av = attr->first_value;
1171 XEXP (condexp, 1) = av->value;
1173 for (i = num_alt - 2; av = av->next, i >= 0; i--)
1178 string = p = (char *) xmalloc (2
1179 + strlen (attr->name)
1180 + strlen (XSTR (av->value, 0)));
1181 strcpy (p, attr->name);
1183 strcat (p, XSTR (av->value, 0));
1184 for (; *p != '\0'; p++)
1185 if (*p >= 'a' && *p <= 'z')
1188 value = attr_rtx (SYMBOL_REF, string);
1189 RTX_UNCHANGING_P (value) = 1;
1191 XVECEXP (condexp, 0, 2 * i) = attr_rtx (EQ, exp, value);
1193 XVECEXP (condexp, 0, 2 * i + 1) = av->value;
1199 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1200 expressions by converting them into a COND. This removes cases from this
1201 program. Also, replace an attribute value of "*" with the default attribute
1205 make_canonical (attr, exp)
1206 struct attr_desc *attr;
1212 switch (GET_CODE (exp))
1215 exp = make_numeric_value (INTVAL (exp));
1219 if (! strcmp (XSTR (exp, 0), "*"))
1221 if (attr == 0 || attr->default_val == 0)
1222 fatal ("(attr_value \"*\") used in invalid context.");
1223 exp = attr->default_val->value;
1229 if (!attr->is_const || RTX_UNCHANGING_P (exp))
1231 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1232 This makes the COND something that won't be considered an arbitrary
1233 expression by walk_attr_value. */
1234 RTX_UNCHANGING_P (exp) = 1;
1235 exp = convert_const_symbol_ref (exp, attr);
1236 RTX_UNCHANGING_P (exp) = 1;
1237 exp = check_attr_value (exp, attr);
1238 /* Goto COND case since this is now a COND. Note that while the
1239 new expression is rescanned, all symbol_ref notes are mared as
1244 newexp = rtx_alloc (COND);
1245 XVEC (newexp, 0) = rtvec_alloc (2);
1246 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1247 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1249 XEXP (newexp, 1) = XEXP (exp, 2);
1252 /* Fall through to COND case since this is now a COND. */
1260 /* First, check for degenerate COND. */
1261 if (XVECLEN (exp, 0) == 0)
1262 return make_canonical (attr, XEXP (exp, 1));
1263 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1265 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1267 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1268 XVECEXP (exp, 0, i + 1)
1269 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1270 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1286 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1287 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1288 copy_boolean (XEXP (exp, 1)));
1292 /* Given a value and an attribute description, return a `struct attr_value *'
1293 that represents that value. This is either an existing structure, if the
1294 value has been previously encountered, or a newly-created structure.
1296 `insn_code' is the code of an insn whose attribute has the specified
1297 value (-2 if not processing an insn). We ensure that all insns for
1298 a given value have the same number of alternatives if the value checks
1301 static struct attr_value *
1302 get_attr_value (value, attr, insn_code)
1304 struct attr_desc *attr;
1307 struct attr_value *av;
1310 value = make_canonical (attr, value);
1311 if (compares_alternatives_p (value))
1313 if (insn_code < 0 || insn_alternatives == NULL)
1314 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1316 num_alt = insn_alternatives[insn_code];
1319 for (av = attr->first_value; av; av = av->next)
1320 if (rtx_equal_p (value, av->value)
1321 && (num_alt == 0 || av->first_insn == NULL
1322 || insn_alternatives[av->first_insn->insn_code]))
1325 av = (struct attr_value *) xmalloc (sizeof (struct attr_value));
1327 av->next = attr->first_value;
1328 attr->first_value = av;
1329 av->first_insn = NULL;
1331 av->has_asm_insn = 0;
1336 /* After all DEFINE_DELAYs have been read in, create internal attributes
1337 to generate the required routines.
1339 First, we compute the number of delay slots for each insn (as a COND of
1340 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1341 delay type is specified, we compute a similar function giving the
1342 DEFINE_DELAY ordinal for each insn.
1344 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1345 tells whether a given insn can be in that delay slot.
1347 Normal attrbute filling and optimization expands these to contain the
1348 information needed to handle delay slots. */
1353 struct delay_desc *delay;
1359 /* First, generate data for `num_delay_slots' function. */
1361 condexp = rtx_alloc (COND);
1362 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1363 XEXP (condexp, 1) = make_numeric_value (0);
1365 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1367 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1368 XVECEXP (condexp, 0, i + 1)
1369 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1372 make_internal_attr ("*num_delay_slots", condexp, 0);
1374 /* If more than one delay type, do the same for computing the delay type. */
1377 condexp = rtx_alloc (COND);
1378 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1379 XEXP (condexp, 1) = make_numeric_value (0);
1381 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1383 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1384 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1387 make_internal_attr ("*delay_type", condexp, 1);
1390 /* For each delay possibility and delay slot, compute an eligability
1391 attribute for non-anulled insns and for each type of annulled (annul
1392 if true and annul if false). */
1393 for (delay = delays; delay; delay = delay->next)
1395 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1397 condexp = XVECEXP (delay->def, 1, i);
1398 if (condexp == 0) condexp = false_rtx;
1399 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1400 make_numeric_value (1), make_numeric_value (0));
1402 p = attr_printf (13, "*delay_%d_%d", delay->num, i / 3);
1403 make_internal_attr (p, newexp, 1);
1405 if (have_annul_true)
1407 condexp = XVECEXP (delay->def, 1, i + 1);
1408 if (condexp == 0) condexp = false_rtx;
1409 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1410 make_numeric_value (1),
1411 make_numeric_value (0));
1412 p = attr_printf (18, "*annul_true_%d_%d", delay->num, i / 3);
1413 make_internal_attr (p, newexp, 1);
1416 if (have_annul_false)
1418 condexp = XVECEXP (delay->def, 1, i + 2);
1419 if (condexp == 0) condexp = false_rtx;
1420 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1421 make_numeric_value (1),
1422 make_numeric_value (0));
1423 p = attr_printf (18, "*annul_false_%d_%d", delay->num, i / 3);
1424 make_internal_attr (p, newexp, 1);
1430 /* This function is given a left and right side expression and an operator.
1431 Each side is a conditional expression, each alternative of which has a
1432 numerical value. The function returns another conditional expression
1433 which, for every possible set of condition values, returns a value that is
1434 the operator applied to the values of the two sides.
1436 Since this is called early, it must also support IF_THEN_ELSE. */
1439 operate_exp (op, left, right)
1443 int left_value, right_value;
1447 /* If left is a string, apply operator to it and the right side. */
1448 if (GET_CODE (left) == CONST_STRING)
1450 /* If right is also a string, just perform the operation. */
1451 if (GET_CODE (right) == CONST_STRING)
1453 left_value = atoi (XSTR (left, 0));
1454 right_value = atoi (XSTR (right, 0));
1458 i = left_value + right_value;
1462 i = left_value - right_value;
1466 i = left_value | right_value;
1470 if (left_value > right_value)
1480 return make_numeric_value (i);
1482 else if (GET_CODE (right) == IF_THEN_ELSE)
1484 /* Apply recursively to all values within. */
1485 rtx newleft = operate_exp (op, left, XEXP (right, 1));
1486 rtx newright = operate_exp (op, left, XEXP (right, 2));
1487 if (rtx_equal_p (newleft, newright))
1489 return attr_rtx (IF_THEN_ELSE, XEXP (right, 0), newleft, newright);
1491 else if (GET_CODE (right) == COND)
1496 newexp = rtx_alloc (COND);
1497 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (right, 0));
1498 defval = XEXP (newexp, 1) = operate_exp (op, left, XEXP (right, 1));
1500 for (i = 0; i < XVECLEN (right, 0); i += 2)
1502 XVECEXP (newexp, 0, i) = XVECEXP (right, 0, i);
1503 XVECEXP (newexp, 0, i + 1)
1504 = operate_exp (op, left, XVECEXP (right, 0, i + 1));
1505 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1510 /* If the resulting cond is trivial (all alternatives
1511 give the same value), optimize it away. */
1514 obstack_free (rtl_obstack, newexp);
1515 return operate_exp (op, left, XEXP (right, 1));
1518 /* If the result is the same as the RIGHT operand,
1520 if (rtx_equal_p (newexp, right))
1522 obstack_free (rtl_obstack, newexp);
1529 fatal ("Badly formed attribute value");
1532 /* Otherwise, do recursion the other way. */
1533 else if (GET_CODE (left) == IF_THEN_ELSE)
1535 rtx newleft = operate_exp (op, XEXP (left, 1), right);
1536 rtx newright = operate_exp (op, XEXP (left, 2), right);
1537 if (rtx_equal_p (newleft, newright))
1539 return attr_rtx (IF_THEN_ELSE, XEXP (left, 0), newleft, newright);
1541 else if (GET_CODE (left) == COND)
1546 newexp = rtx_alloc (COND);
1547 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (left, 0));
1548 defval = XEXP (newexp, 1) = operate_exp (op, XEXP (left, 1), right);
1550 for (i = 0; i < XVECLEN (left, 0); i += 2)
1552 XVECEXP (newexp, 0, i) = XVECEXP (left, 0, i);
1553 XVECEXP (newexp, 0, i + 1)
1554 = operate_exp (op, XVECEXP (left, 0, i + 1), right);
1555 if (! rtx_equal_p (XVECEXP (newexp, 0, i + 1),
1560 /* If the cond is trivial (all alternatives give the same value),
1561 optimize it away. */
1564 obstack_free (rtl_obstack, newexp);
1565 return operate_exp (op, XEXP (left, 1), right);
1568 /* If the result is the same as the LEFT operand,
1570 if (rtx_equal_p (newexp, left))
1572 obstack_free (rtl_obstack, newexp);
1580 fatal ("Badly formed attribute value.");
1585 /* Once all attributes and DEFINE_FUNCTION_UNITs have been read, we
1586 construct a number of attributes.
1588 The first produces a function `function_units_used' which is given an
1589 insn and produces a mask showing which function units are required for
1590 the execution of that insn.
1592 The second produces a function `result_ready_cost' which is used to
1593 determine the time that the result of an insn will be ready and hence
1594 a worst-case schedule.
1596 Both of these produce quite complex expressions which are then set as the
1597 default value of internal attributes. Normal attribute simplification
1598 should produce reasonable expressions.
1600 For each unit, a `<name>_unit_ready_cost' function will take an
1601 insn and give the delay until that unit will be ready with the result
1602 and a `<name>_unit_busy_delay' function is given an insn already
1603 executing on the unit and a candidate to execute and will give the
1604 cost from the time the executing insn started until the candidate
1605 can start (ignore limitations on the number of simultaneous insns). */
1610 struct function_unit *unit;
1611 struct function_unit_op *op;
1617 /* Initially, cost and masks are zero. */
1618 unitsmask = readycost = make_numeric_value (0);
1620 /* Set up a conditional for costs and unit mask. */
1621 newexp = rtx_alloc (IF_THEN_ELSE);
1622 XEXP (newexp, 2) = make_numeric_value (0);
1624 /* For each unit, insert its contribution to the above three values. */
1625 for (unit = units; unit; unit = unit->next)
1627 /* An expression that computes the ready cost for this unit. */
1628 rtx readyexp = rtx_alloc (COND);
1629 /* An expression that maps insns to operation number for conflicts. */
1630 rtx caseexp = rtx_alloc (COND);
1632 XVEC (readyexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
1633 XVEC (caseexp, 0) = rtvec_alloc ((unit->num_opclasses - 1) * 2);
1635 for (op = unit->ops; op; op = op->next)
1637 /* Validate the expressions we were given for the conditions
1638 and busy cost. Then make an attribute for use in the conflict
1640 op->condexp = check_attr_test (op->condexp, 0);
1641 op->busyexp = check_attr_value (op->busyexp, 0);
1642 str = attr_printf (strlen (unit->name) + 11, "*%s_case_%d",
1643 unit->name, op->num);
1644 make_internal_attr (str, make_canonical (0, op->busyexp));
1646 /* Make our adjustment to the two COND's being computed. If we are
1647 the last operation class, place our values into the default of
1649 if (op->num == unit->num_opclasses - 1)
1651 XEXP (readyexp, 1) = make_numeric_value (op->ready);
1652 XEXP (caseexp, 1) = make_numeric_value (op->num);
1656 XVECEXP (readyexp, 0, op->num * 2) = op->condexp;
1657 XVECEXP (readyexp, 0, op->num * 2 + 1)
1658 = make_numeric_value (op->ready);
1659 XVECEXP (caseexp, 0, op->num * 2) = op->condexp;
1660 XVECEXP (caseexp, 0, op->num * 2 + 1)
1661 = make_numeric_value (op->num);
1665 /* Make an attribute for the case number and ready delay. */
1666 str = attr_printf (strlen (unit->name) + 8, "*%s_cases", unit->name);
1667 make_internal_attr (str, caseexp, 1);
1669 str = attr_printf (strlen (unit->name) + 20, "*%s_unit_ready_cost",
1671 make_internal_attr (str, readyexp, 0);
1673 /* Merge this function unit into the ready cost and unit mask
1675 XEXP (newexp, 0) = check_attr_test (unit->condexp, 0);
1676 XEXP (newexp, 1) = make_numeric_value (1 << unit->num);
1677 unitsmask = operate_exp (OR_OP, unitsmask, newexp);
1679 XEXP (newexp, 1) = readyexp;
1680 readycost = operate_exp (MAX_OP, readycost, newexp);
1683 make_internal_attr ("*function_units_used", unitsmask, 0);
1684 make_internal_attr ("*result_ready_cost", readycost, 0);
1687 /* Once all attributes and insns have been read and checked, we construct for
1688 each attribute value a list of all the insns that have that value for
1693 struct attr_desc *attr;
1695 struct attr_value *av;
1696 struct insn_ent *ie;
1697 struct insn_def *id;
1701 for (id = defs; id; id = id->next)
1703 /* If no value is specified for this insn for this attribute, use the
1706 if (XVEC (id->def, id->vec_idx))
1707 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1708 if (! strcmp (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
1710 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
1713 av = attr->default_val;
1715 av = get_attr_value (value, attr, id->insn_code);
1717 ie = (struct insn_ent *) xmalloc (sizeof (struct insn_ent));
1718 ie->insn_code = id->insn_code;
1719 ie->insn_index = id->insn_code;
1720 insert_insn_ent (av, ie);
1724 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
1725 test that checks relative positions of insns (uses MATCH_DUP or PC).
1726 If so, replace it with what is obtained by passing the expression to
1727 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
1728 recursively on each value (including the default value). Otherwise,
1729 return the value returned by NO_ADDRESS_FN applied to EXP. */
1732 substitute_address (exp, no_address_fn, address_fn)
1734 rtx (*no_address_fn) ();
1735 rtx (*address_fn) ();
1740 if (GET_CODE (exp) == COND)
1742 /* See if any tests use addresses. */
1744 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1745 walk_attr_value (XVECEXP (exp, 0, i));
1748 return (*address_fn) (exp);
1750 /* Make a new copy of this COND, replacing each element. */
1751 newexp = rtx_alloc (COND);
1752 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1753 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1755 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
1756 XVECEXP (newexp, 0, i + 1)
1757 = substitute_address (XVECEXP (exp, 0, i + 1),
1758 no_address_fn, address_fn);
1761 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
1762 no_address_fn, address_fn);
1767 else if (GET_CODE (exp) == IF_THEN_ELSE)
1770 walk_attr_value (XEXP (exp, 0));
1772 return (*address_fn) (exp);
1774 return attr_rtx (IF_THEN_ELSE,
1775 substitute_address (XEXP (exp, 0),
1776 no_address_fn, address_fn),
1777 substitute_address (XEXP (exp, 1),
1778 no_address_fn, address_fn),
1779 substitute_address (XEXP (exp, 2),
1780 no_address_fn, address_fn));
1783 return (*no_address_fn) (exp);
1786 /* Make new attributes from the `length' attribute. The following are made,
1787 each corresponding to a function called from `shorten_branches' or
1790 *insn_default_length This is the length of the insn to be returned
1791 by `get_attr_length' before `shorten_branches'
1792 has been called. In each case where the length
1793 depends on relative addresses, the largest
1794 possible is used. This routine is also used
1795 to compute the initial size of the insn.
1797 *insn_variable_length_p This returns 1 if the insn's length depends
1798 on relative addresses, zero otherwise.
1800 *insn_current_length This is only called when it is known that the
1801 insn has a variable length and returns the
1802 current length, based on relative addresses.
1806 make_length_attrs ()
1808 static char *new_names[] = {"*insn_default_length",
1809 "*insn_variable_length_p",
1810 "*insn_current_length"};
1811 static rtx (*no_address_fn[]) () = {identity_fn, zero_fn, zero_fn};
1812 static rtx (*address_fn[]) () = {max_fn, one_fn, identity_fn};
1814 struct attr_desc *length_attr, *new_attr;
1815 struct attr_value *av, *new_av;
1816 struct insn_ent *ie, *new_ie;
1818 /* See if length attribute is defined. If so, it must be numeric. Make
1819 it special so we don't output anything for it. */
1820 length_attr = find_attr ("length", 0);
1821 if (length_attr == 0)
1824 if (! length_attr->is_numeric)
1825 fatal ("length attribute must be numeric.");
1827 length_attr->is_const = 0;
1828 length_attr->is_special = 1;
1830 /* Make each new attribute, in turn. */
1831 for (i = 0; i < sizeof new_names / sizeof new_names[0]; i++)
1833 make_internal_attr (new_names[i],
1834 substitute_address (length_attr->default_val->value,
1835 no_address_fn[i], address_fn[i]),
1837 new_attr = find_attr (new_names[i], 0);
1838 for (av = length_attr->first_value; av; av = av->next)
1839 for (ie = av->first_insn; ie; ie = ie->next)
1841 new_av = get_attr_value (substitute_address (av->value,
1844 new_attr, ie->insn_code);
1845 new_ie = (struct insn_ent *) xmalloc (sizeof (struct insn_ent));
1846 new_ie->insn_code = ie->insn_code;
1847 new_ie->insn_index = ie->insn_index;
1848 insert_insn_ent (new_av, new_ie);
1853 /* Utility functions called from above routine. */
1866 return make_numeric_value (0);
1873 return make_numeric_value (1);
1880 return make_numeric_value (max_attr_value (exp));
1883 /* Take a COND expression and see if any of the conditions in it can be
1884 simplified. If any are known true or known false for the particular insn
1885 code, the COND can be further simplified.
1887 Also call ourselves on any COND operations that are values of this COND.
1889 We do not modify EXP; rather, we make and return a new rtx. */
1892 simplify_cond (exp, insn_code, insn_index)
1894 int insn_code, insn_index;
1897 /* We store the desired contents here,
1898 then build a new expression if they don't match EXP. */
1899 rtx defval = XEXP (exp, 1);
1900 rtx new_defval = XEXP (exp, 1);
1902 int len = XVECLEN (exp, 0);
1903 rtx *tests = (rtx *) alloca (len * sizeof (rtx));
1905 char *spacer, *first_spacer;
1907 /* This lets us free all storage allocated below, if appropriate. */
1908 first_spacer = (char *) obstack_finish (rtl_obstack);
1910 bcopy (&XVECEXP (exp, 0, 0), tests, len * sizeof (rtx));
1912 /* See if default value needs simplification. */
1913 if (GET_CODE (defval) == COND)
1914 new_defval = simplify_cond (defval, insn_code, insn_index);
1916 /* Simplify the subexpressions, and see what tests we can get rid of. */
1918 for (i = 0; i < len; i += 2)
1920 rtx newtest, newval;
1922 /* Simplify this test. */
1923 newtest = SIMPLIFY_TEST_EXP (tests[i], insn_code, insn_index);
1926 newval = tests[i + 1];
1927 /* See if this value may need simplification. */
1928 if (GET_CODE (newval) == COND)
1929 newval = simplify_cond (newval, insn_code, insn_index);
1931 /* Look for ways to delete or combine this test. */
1932 if (newtest == true_rtx)
1934 /* If test is true, make this value the default
1935 and discard this + any following tests. */
1937 defval = tests[i + 1];
1938 new_defval = newval;
1941 else if (newtest == false_rtx)
1943 /* If test is false, discard it and its value. */
1944 for (j = i; j < len - 2; j++)
1945 tests[j] = tests[j + 2];
1949 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
1951 /* If this value and the value for the prev test are the same,
1955 = insert_right_side (IOR, tests[i - 2], newtest,
1956 insn_code, insn_index);
1958 /* Delete this test/value. */
1959 for (j = i; j < len - 2; j++)
1960 tests[j] = tests[j + 2];
1965 tests[i + 1] = newval;
1968 /* If the last test in a COND has the same value
1969 as the default value, that test isn't needed. */
1971 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
1974 /* See if we changed anything. */
1975 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
1978 for (i = 0; i < len; i++)
1979 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
1987 obstack_free (rtl_obstack, first_spacer);
1988 if (GET_CODE (defval) == COND)
1989 return simplify_cond (defval, insn_code, insn_index);
1994 obstack_free (rtl_obstack, first_spacer);
1999 rtx newexp = rtx_alloc (COND);
2001 XVEC (newexp, 0) = rtvec_alloc (len);
2002 bcopy (tests, &XVECEXP (newexp, 0, 0), len * sizeof (rtx));
2003 XEXP (newexp, 1) = new_defval;
2008 /* Remove an insn entry from an attribute value. */
2011 remove_insn_ent (av, ie)
2012 struct attr_value *av;
2013 struct insn_ent *ie;
2015 struct insn_ent *previe;
2017 if (av->first_insn == ie)
2018 av->first_insn = ie->next;
2021 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
2023 previe->next = ie->next;
2027 if (ie->insn_code == -1)
2028 av->has_asm_insn = 0;
2031 /* Insert an insn entry in an attribute value list. */
2034 insert_insn_ent (av, ie)
2035 struct attr_value *av;
2036 struct insn_ent *ie;
2038 ie->next = av->first_insn;
2039 av->first_insn = ie;
2041 if (ie->insn_code == -1)
2042 av->has_asm_insn = 1;
2045 /* This is a utility routine to take an expression that is a tree of either
2046 AND or IOR expressions and insert a new term. The new term will be
2047 inserted at the right side of the first node whose code does not match
2048 the root. A new node will be created with the root's code. Its left
2049 side will be the old right side and its right side will be the new
2052 If the `term' is itself a tree, all its leaves will be inserted. */
2055 insert_right_side (code, exp, term, insn_code, insn_index)
2059 int insn_code, insn_index;
2063 /* Avoid consing in some special cases. */
2064 if (code == AND && term == true_rtx)
2066 if (code == AND && term == false_rtx)
2068 if (code == AND && exp == true_rtx)
2070 if (code == AND && exp == false_rtx)
2072 if (code == IOR && term == true_rtx)
2074 if (code == IOR && term == false_rtx)
2076 if (code == IOR && exp == true_rtx)
2078 if (code == IOR && exp == false_rtx)
2080 if (attr_equal_p (exp, term))
2083 if (GET_CODE (term) == code)
2085 exp = insert_right_side (code, exp, XEXP (term, 0),
2086 insn_code, insn_index);
2087 exp = insert_right_side (code, exp, XEXP (term, 1),
2088 insn_code, insn_index);
2093 if (GET_CODE (exp) == code)
2095 rtx new = insert_right_side (code, XEXP (exp, 1),
2096 term, insn_code, insn_index);
2097 if (new != XEXP (exp, 1))
2098 /* Make a copy of this expression and call recursively. */
2099 newexp = attr_rtx (code, XEXP (exp, 0), new);
2105 /* Insert the new term. */
2106 newexp = attr_rtx (code, exp, term);
2109 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2112 /* If we have an expression which AND's a bunch of
2113 (not (eq_attrq "alternative" "n"))
2114 terms, we may have covered all or all but one of the possible alternatives.
2115 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
2117 This routine is passed an expression and either AND or IOR. It returns a
2118 bitmask indicating which alternatives are present.
2119 ??? What does "present" mean? */
2122 compute_alternative_mask (exp, code)
2126 if (GET_CODE (exp) == code)
2127 return compute_alternative_mask (XEXP (exp, 0), code)
2128 | compute_alternative_mask (XEXP (exp, 1), code);
2130 else if (code == AND && GET_CODE (exp) == NOT
2131 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2132 && XSTR (XEXP (exp, 0), 0) == alternative_name)
2133 return 1 << atoi (XSTR (XEXP (exp, 0), 1));
2135 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
2136 && XSTR (exp, 0) == alternative_name)
2137 return 1 << atoi (XSTR (exp, 1));
2143 /* Given I, a single-bit mask, return RTX to compare the `alternative'
2144 attribute with the value represented by that bit. */
2147 make_alternative_compare (mask)
2155 for (i = 0; (mask & (1 << i)) == 0; i++)
2158 newexp = attr_rtx (EQ_ATTR, alternative_name, attr_numeral (i));
2159 RTX_UNCHANGING_P (newexp) = 1;
2164 /* If we are processing an (eq_attr "attr" "value") test, we find the value
2165 of "attr" for this insn code. From that value, we can compute a test
2166 showing when the EQ_ATTR will be true. This routine performs that
2167 computation. If a test condition involves an address, we leave the EQ_ATTR
2168 intact because addresses are only valid for the `length' attribute. */
2170 /* ??? Kenner, document the meanings of the arguments!!! */
2173 evaluate_eq_attr (exp, value, insn_code, insn_index)
2176 int insn_code, insn_index;
2183 if (GET_CODE (value) == CONST_STRING)
2185 if (! strcmp (XSTR (value, 0), XSTR (exp, 1)))
2190 else if (GET_CODE (value) == COND)
2192 /* We construct an IOR of all the cases for which the requested attribute
2193 value is present. Since we start with FALSE, if it is not present,
2194 FALSE will be returned.
2196 Each case is the AND of the NOT's of the previous conditions with the
2197 current condition; in the default case the current condition is TRUE.
2199 For each possible COND value, call ourselves recursively.
2201 The extra TRUE and FALSE expressions will be eliminated by another
2202 call to the simplification routine. */
2207 if (current_alternative_string)
2208 clear_struct_flag (value);
2210 for (i = 0; i < XVECLEN (value, 0); i += 2)
2212 rtx this = SIMPLIFY_TEST_EXP (XVECEXP (value, 0, i),
2213 insn_code, insn_index);
2215 SIMPLIFY_ALTERNATIVE (this);
2217 right = insert_right_side (AND, andexp, this,
2218 insn_code, insn_index);
2219 right = insert_right_side (AND, right,
2220 evaluate_eq_attr (exp, XVECEXP (value, 0, i + 1),
2221 insn_code, insn_index),
2222 insn_code, insn_index);
2223 orexp = insert_right_side (IOR, orexp, right,
2224 insn_code, insn_index);
2226 /* Add this condition into the AND expression. */
2227 newexp = attr_rtx (NOT, this);
2228 andexp = insert_right_side (AND, andexp, newexp,
2229 insn_code, insn_index);
2232 /* Handle the default case. */
2233 right = insert_right_side (AND, andexp,
2234 evaluate_eq_attr (exp, XEXP (value, 1),
2235 insn_code, insn_index),
2236 insn_code, insn_index);
2237 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2242 /* If uses an address, must return original expression. But set the
2243 RTX_UNCHANGING_P bit so we don't try to simplify it again. */
2246 walk_attr_value (newexp);
2250 if (! RTX_UNCHANGING_P (exp) && current_alternative_string)
2251 return copy_rtx_unchanging (exp);
2258 /* This routine is called when an AND of a term with a tree of AND's is
2259 encountered. If the term or its complement is present in the tree, it
2260 can be replaced with TRUE or FALSE, respectively.
2262 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2263 be true and hence are complementary.
2265 There is one special case: If we see
2266 (and (not (eq_attr "att" "v1"))
2267 (eq_attr "att" "v2"))
2268 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2269 replace the term, not anything in the AND tree. So we pass a pointer to
2273 simplify_and_tree (exp, pterm, insn_code, insn_index)
2276 int insn_code, insn_index;
2281 int left_eliminates_term, right_eliminates_term;
2283 if (GET_CODE (exp) == AND)
2285 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2286 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2287 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2289 newexp = attr_rtx (GET_CODE (exp), left, right);
2291 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2295 else if (GET_CODE (exp) == IOR)
2297 /* For the IOR case, we do the same as above, except that we can
2298 only eliminate `term' if both sides of the IOR would do so. */
2300 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2301 left_eliminates_term = (temp == true_rtx);
2304 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2305 right_eliminates_term = (temp == true_rtx);
2307 if (left_eliminates_term && right_eliminates_term)
2310 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2312 newexp = attr_rtx (GET_CODE (exp), left, right);
2314 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2318 /* Check for simplifications. Do some extra checking here since this
2319 routine is called so many times. */
2324 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2327 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2330 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2332 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2335 if (! strcmp (XSTR (exp, 1), XSTR (*pterm, 1)))
2341 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2342 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2344 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2347 if (! strcmp (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2353 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2354 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2356 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2359 if (! strcmp (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2365 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2367 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2371 else if (GET_CODE (exp) == NOT)
2373 if (attr_equal_p (XEXP (exp, 0), *pterm))
2377 else if (GET_CODE (*pterm) == NOT)
2379 if (attr_equal_p (XEXP (*pterm, 0), exp))
2383 else if (attr_equal_p (exp, *pterm))
2389 /* Similiar to `simplify_and_tree', but for IOR trees. */
2392 simplify_or_tree (exp, pterm, insn_code, insn_index)
2395 int insn_code, insn_index;
2400 int left_eliminates_term, right_eliminates_term;
2402 if (GET_CODE (exp) == IOR)
2404 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2405 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2406 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2408 newexp = attr_rtx (GET_CODE (exp), left, right);
2410 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2414 else if (GET_CODE (exp) == AND)
2416 /* For the AND case, we do the same as above, except that we can
2417 only eliminate `term' if both sides of the AND would do so. */
2419 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2420 left_eliminates_term = (temp == false_rtx);
2423 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2424 right_eliminates_term = (temp == false_rtx);
2426 if (left_eliminates_term && right_eliminates_term)
2429 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2431 newexp = attr_rtx (GET_CODE (exp), left, right);
2433 exp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2437 if (attr_equal_p (exp, *pterm))
2440 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
2443 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
2446 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2447 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2448 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
2451 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2452 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
2453 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
2459 /* Given an expression, see if it can be simplified for a particular insn
2460 code based on the values of other attributes being tested. This can
2461 eliminate nested get_attr_... calls.
2463 Note that if an endless recursion is specified in the patterns, the
2464 optimization will loop. However, it will do so in precisely the cases where
2465 an infinite recursion loop could occur during compilation. It's better that
2469 simplify_test_exp (exp, insn_code, insn_index)
2471 int insn_code, insn_index;
2474 struct attr_desc *attr;
2475 struct attr_value *av;
2476 struct insn_ent *ie;
2479 char *spacer = (char *) obstack_finish (rtl_obstack);
2481 static rtx loser = 0;
2482 static int count = 0;
2483 static stopcount = 0;
2488 if (count == stopcount)
2491 /* Don't re-simplify something we already simplified. */
2492 if (RTX_UNCHANGING_P (exp) || MEM_IN_STRUCT_P (exp))
2495 switch (GET_CODE (exp))
2498 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2499 SIMPLIFY_ALTERNATIVE (left);
2500 if (left == false_rtx)
2502 obstack_free (rtl_obstack, spacer);
2505 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2506 SIMPLIFY_ALTERNATIVE (right);
2507 if (left == false_rtx)
2509 obstack_free (rtl_obstack, spacer);
2513 /* If either side is an IOR and we have (eq_attr "alternative" ..")
2514 present on both sides, apply the distributive law since this will
2515 yield simplifications. */
2516 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
2517 && compute_alternative_mask (left, IOR)
2518 && compute_alternative_mask (right, IOR))
2520 if (GET_CODE (left) == IOR)
2527 newexp = attr_rtx (IOR,
2528 attr_rtx (AND, left, XEXP (right, 0)),
2529 attr_rtx (AND, left, XEXP (right, 1)));
2531 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2534 /* Try with the term on both sides. */
2535 right = simplify_and_tree (right, &left, insn_code, insn_index);
2536 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2537 left = simplify_and_tree (left, &right, insn_code, insn_index);
2539 if (left == false_rtx || right == false_rtx)
2541 obstack_free (rtl_obstack, spacer);
2544 else if (left == true_rtx)
2548 else if (right == true_rtx)
2552 /* See if all or all but one of the insn's alternatives are specified
2553 in this tree. Optimize if so. */
2555 else if (insn_code >= 0
2556 && (GET_CODE (left) == AND
2557 || (GET_CODE (left) == NOT
2558 && GET_CODE (XEXP (left, 0)) == EQ_ATTR
2559 && XSTR (XEXP (left, 0), 0) == alternative_name)
2560 || GET_CODE (right) == AND
2561 || (GET_CODE (right) == NOT
2562 && GET_CODE (XEXP (right, 0)) == EQ_ATTR
2563 && XSTR (XEXP (right, 0), 0) == alternative_name)))
2565 i = compute_alternative_mask (exp, AND);
2566 if (i & ~insn_alternatives[insn_code])
2567 fatal ("Illegal alternative specified for pattern number %d",
2570 /* If all alternatives are excluded, this is false. */
2571 i ^= insn_alternatives[insn_code];
2574 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2576 /* If just one excluded, AND a comparison with that one to the
2577 front of the tree. The others will be eliminated by
2578 optimization. We do not want to do this if the insn has one
2579 alternative and we have tested none of them! */
2580 left = make_alternative_compare (i);
2581 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2582 newexp = attr_rtx (AND, left, right);
2584 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2588 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2590 newexp = attr_rtx (AND, left, right);
2591 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2596 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2597 SIMPLIFY_ALTERNATIVE (left);
2598 if (left == true_rtx)
2600 obstack_free (rtl_obstack, spacer);
2603 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2604 SIMPLIFY_ALTERNATIVE (right);
2605 if (right == true_rtx)
2607 obstack_free (rtl_obstack, spacer);
2611 right = simplify_or_tree (right, &left, insn_code, insn_index);
2612 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2613 left = simplify_or_tree (left, &right, insn_code, insn_index);
2615 if (right == true_rtx || left == true_rtx)
2617 obstack_free (rtl_obstack, spacer);
2620 else if (left == false_rtx)
2624 else if (right == false_rtx)
2629 /* Test for simple cases where the distributive law is useful. I.e.,
2630 convert (ior (and (x) (y))
2636 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2637 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
2639 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
2641 left = XEXP (left, 0);
2643 newexp = attr_rtx (AND, left, right);
2644 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2647 /* See if all or all but one of the insn's alternatives are specified
2648 in this tree. Optimize if so. */
2650 else if (insn_code >= 0
2651 && (GET_CODE (left) == IOR
2652 || (GET_CODE (left) == EQ_ATTR
2653 && XSTR (left, 0) == alternative_name)
2654 || GET_CODE (right) == IOR
2655 || (GET_CODE (right) == EQ_ATTR
2656 && XSTR (right, 0) == alternative_name)))
2658 i = compute_alternative_mask (exp, IOR);
2659 if (i & ~insn_alternatives[insn_code])
2660 fatal ("Illegal alternative specified for pattern number %d",
2663 /* If all alternatives are included, this is true. */
2664 i ^= insn_alternatives[insn_code];
2667 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2669 /* If just one excluded, IOR a comparison with that one to the
2670 front of the tree. The others will be eliminated by
2671 optimization. We do not want to do this if the insn has one
2672 alternative and we have tested none of them! */
2673 left = make_alternative_compare (i);
2674 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2675 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
2677 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2681 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2683 newexp = attr_rtx (IOR, left, right);
2684 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2689 if (GET_CODE (XEXP (exp, 0)) == NOT)
2691 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
2692 insn_code, insn_index);
2693 SIMPLIFY_ALTERNATIVE (left);
2697 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2698 SIMPLIFY_ALTERNATIVE (left);
2699 if (GET_CODE (left) == NOT)
2700 return XEXP (left, 0);
2702 if (left == false_rtx)
2704 obstack_free (rtl_obstack, spacer);
2707 else if (left == true_rtx)
2709 obstack_free (rtl_obstack, spacer);
2713 /* Try to apply De`Morgan's laws. */
2714 else if (GET_CODE (left) == IOR)
2716 newexp = attr_rtx (AND,
2717 attr_rtx (NOT, XEXP (left, 0)),
2718 attr_rtx (NOT, XEXP (left, 1)));
2720 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2722 else if (GET_CODE (left) == AND)
2724 newexp = attr_rtx (IOR,
2725 attr_rtx (NOT, XEXP (left, 0)),
2726 attr_rtx (NOT, XEXP (left, 1)));
2728 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2730 else if (left != XEXP (exp, 0))
2732 newexp = attr_rtx (NOT, left);
2737 if (current_alternative_string && XSTR (exp, 0) == alternative_name)
2738 return (XSTR (exp, 1) == current_alternative_string
2739 ? true_rtx : false_rtx);
2741 /* Look at the value for this insn code in the specified attribute.
2742 We normally can replace this comparison with the condition that
2743 would give this insn the values being tested for. */
2744 if (XSTR (exp, 0) != alternative_name
2745 && (attr = find_attr (XSTR (exp, 0), 0)) != NULL)
2746 for (av = attr->first_value; av; av = av->next)
2747 for (ie = av->first_insn; ie; ie = ie->next)
2748 if (ie->insn_code == insn_code)
2749 return evaluate_eq_attr (exp, av->value, insn_code, insn_index);
2752 /* We have already simplified this expression. Simplifying it again
2753 won't buy anything unless we weren't given a valid insn code
2754 to process (i.e., we are canonicalizing something.). */
2755 if (insn_code != -2 && current_alternative_string
2756 && ! RTX_UNCHANGING_P (newexp))
2757 return copy_rtx_unchanging (newexp);
2765 /* Optimize the attribute lists by seeing if we can determine conditional
2766 values from the known values of other attributes. This will save subroutine
2767 calls during the compilation. */
2772 struct attr_desc *attr;
2773 struct attr_value *av;
2774 struct insn_ent *ie, *nextie;
2776 int something_changed = 1;
2778 struct attr_value_list { struct attr_value *av;
2779 struct insn_ent *ie;
2780 struct attr_desc * attr;
2781 struct attr_value_list *next; };
2782 struct attr_value_list **insn_code_values;
2783 struct attr_value_list *iv;
2785 /* For each insn code, make a list of all the insn_ent's for it,
2786 for all values for all attributes. */
2788 /* Make 2 extra elements, for "code" values -2 and -1. */
2790 = (struct attr_value_list **) alloca ((insn_code_number + 2)
2791 * sizeof (struct attr_value_list *));
2792 bzero (insn_code_values,
2793 (insn_code_number + 2) * sizeof (struct attr_value_list *));
2794 /* Offset the table address so we can index by -2 or -1. */
2795 insn_code_values += 2;
2797 for (i = 0; i < MAX_ATTRS_INDEX; i++)
2798 for (attr = attrs[i]; attr; attr = attr->next)
2799 for (av = attr->first_value; av; av = av->next)
2800 for (ie = av->first_insn; ie; ie = ie->next)
2802 iv = ((struct attr_value_list *)
2803 alloca (sizeof (struct attr_value_list)));
2807 iv->next = insn_code_values[ie->insn_code];
2808 insn_code_values[ie->insn_code] = iv;
2811 /* Process one insn code at a time. */
2812 for (i = -2; i < insn_code_number; i++)
2814 /* Clear the MEM_IN_STRUCT_P flag everywhere relevant.
2815 We use it to mean "already simplified for this insn". */
2816 for (iv = insn_code_values[i]; iv; iv = iv->next)
2817 clear_struct_flag (iv->av->value);
2819 /* Loop until nothing changes for one iteration. */
2820 something_changed = 1;
2821 while (something_changed)
2823 something_changed = 0;
2824 for (iv = insn_code_values[i]; iv; iv = iv->next)
2826 struct obstack *old = rtl_obstack;
2827 char *spacer = (char *) obstack_finish (temp_obstack);
2832 if (GET_CODE (av->value) != COND)
2835 rtl_obstack = temp_obstack;
2836 #if 0 /* This was intended as a speed up, but it was slower. */
2837 if (insn_n_alternatives[ie->insn_code] > 6
2838 && count_sub_rtxs (av->value, 200) >= 200)
2839 newexp = simplify_by_alternatives (av->value, ie->insn_code,
2843 newexp = simplify_cond (av->value, ie->insn_code,
2847 if (newexp != av->value)
2849 newexp = attr_copy_rtx (newexp);
2850 remove_insn_ent (av, ie);
2851 av = get_attr_value (newexp, attr, ie->insn_code);
2853 insert_insn_ent (av, ie);
2854 something_changed = 1;
2856 obstack_free (temp_obstack, spacer);
2863 simplify_by_alternatives (exp, insn_code, insn_index)
2865 int insn_code, insn_index;
2868 int len = insn_n_alternatives[insn_code];
2869 rtx newexp = rtx_alloc (COND);
2873 XVEC (newexp, 0) = rtvec_alloc (len * 2);
2875 /* It will not matter what value we use as the default value
2876 of the new COND, since that default will never be used.
2877 Choose something of the right type. */
2878 for (ultimate = exp; GET_CODE (ultimate) == COND;)
2879 ultimate = XEXP (ultimate, 1);
2880 XEXP (newexp, 1) = ultimate;
2882 for (i = 0; i < insn_n_alternatives[insn_code]; i++)
2884 current_alternative_string = attr_numeral (i);
2885 XVECEXP (newexp, 0, i * 2) = make_alternative_compare (1 << i);
2886 XVECEXP (newexp, 0, i * 2 + 1)
2887 = simplify_cond (exp, insn_code, insn_index);
2890 current_alternative_string = 0;
2891 return simplify_cond (newexp, insn_code, insn_index);
2894 /* Clear the MEM_IN_STRUCT_P flag in EXP and its subexpressions. */
2896 clear_struct_flag (x)
2901 register enum rtx_code code;
2904 MEM_IN_STRUCT_P (x) = 0;
2905 if (RTX_UNCHANGING_P (x))
2908 code = GET_CODE (x);
2924 /* Compare the elements. If any pair of corresponding elements
2925 fail to match, return 0 for the whole things. */
2927 fmt = GET_RTX_FORMAT (code);
2928 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2934 for (j = 0; j < XVECLEN (x, i); j++)
2935 clear_struct_flag (XVECEXP (x, i, j));
2939 clear_struct_flag (XEXP (x, i));
2945 /* Return the number of RTX objects making up the expression X.
2946 But if we count more more than MAX objects, stop counting. */
2948 count_sub_rtxs (x, max)
2954 register enum rtx_code code;
2958 code = GET_CODE (x);
2974 /* Compare the elements. If any pair of corresponding elements
2975 fail to match, return 0 for the whole things. */
2977 fmt = GET_RTX_FORMAT (code);
2978 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2987 for (j = 0; j < XVECLEN (x, i); j++)
2988 total += count_sub_rtxs (XVECEXP (x, i, j), max);
2992 total += count_sub_rtxs (XEXP (x, i), max);
3000 /* Create table entries for DEFINE_ATTR. */
3006 struct attr_desc *attr;
3007 struct attr_value *av;
3011 /* Make a new attribute structure. Check for duplicate by looking at
3012 attr->default_val, since it is initialized by this routine. */
3013 attr = find_attr (XSTR (exp, 0), 1);
3014 if (attr->default_val)
3015 fatal ("Duplicate definition for `%s' attribute", attr->name);
3017 if (*XSTR (exp, 1) == '\0')
3018 attr->is_numeric = 1;
3021 name_ptr = XSTR (exp, 1);
3022 while ((p = next_comma_elt (&name_ptr)) != NULL)
3024 av = (struct attr_value *) xmalloc (sizeof (struct attr_value));
3025 av->value = attr_rtx (CONST_STRING, p);
3026 av->next = attr->first_value;
3027 attr->first_value = av;
3028 av->first_insn = NULL;
3030 av->has_asm_insn = 0;
3034 if (GET_CODE (XEXP (exp, 2)) == CONST)
3037 if (attr->is_numeric)
3038 fatal ("Constant attributes may not take numeric values");
3039 /* Get rid of the CONST node. It is allowed only at top-level. */
3040 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
3043 if (! strcmp (attr->name, "length") && ! attr->is_numeric)
3044 fatal ("`length' attribute must take numeric values");
3046 /* Set up the default value. */
3047 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
3048 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
3051 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
3052 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
3053 number of alternatives as this should be checked elsewhere. */
3056 count_alternatives (exp)
3062 if (GET_CODE (exp) == MATCH_OPERAND)
3063 return n_comma_elts (XSTR (exp, 2));
3065 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3066 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3071 n = count_alternatives (XEXP (exp, i));
3078 if (XVEC (exp, i) != NULL)
3079 for (j = 0; j < XVECLEN (exp, i); j++)
3081 n = count_alternatives (XVECEXP (exp, i, j));
3090 /* Returns non-zero if the given expression contains an EQ_ATTR with the
3091 `alternative' attribute. */
3094 compares_alternatives_p (exp)
3100 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
3103 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3104 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3109 if (compares_alternatives_p (XEXP (exp, i)))
3114 for (j = 0; j < XVECLEN (exp, i); j++)
3115 if (compares_alternatives_p (XVECEXP (exp, i, j)))
3123 /* Returns non-zero is INNER is contained in EXP. */
3126 contained_in_p (inner, exp)
3133 if (rtx_equal_p (inner, exp))
3136 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3137 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3142 if (contained_in_p (inner, XEXP (exp, i)))
3147 for (j = 0; j < XVECLEN (exp, i); j++)
3148 if (contained_in_p (inner, XVECEXP (exp, i, j)))
3156 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
3162 struct insn_def *id;
3164 id = (struct insn_def *) xmalloc (sizeof (struct insn_def));
3169 switch (GET_CODE (exp))
3172 id->insn_code = insn_code_number++;
3173 id->insn_index = insn_index_number++;
3174 id->num_alternatives = count_alternatives (exp);
3175 if (id->num_alternatives == 0)
3176 id->num_alternatives = 1;
3180 case DEFINE_PEEPHOLE:
3181 id->insn_code = insn_code_number++;
3182 id->insn_index = insn_index_number++;
3183 id->num_alternatives = count_alternatives (exp);
3184 if (id->num_alternatives == 0)
3185 id->num_alternatives = 1;
3189 case DEFINE_ASM_ATTRIBUTES:
3191 id->insn_index = -1;
3192 id->num_alternatives = 1;
3194 got_define_asm_attributes = 1;
3199 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
3200 true or annul false is specified, and make a `struct delay_desc'. */
3206 struct delay_desc *delay;
3209 if (XVECLEN (def, 1) % 3 != 0)
3210 fatal ("Number of elements in DEFINE_DELAY must be multiple of three.");
3212 for (i = 0; i < XVECLEN (def, 1); i += 3)
3214 if (XVECEXP (def, 1, i + 1))
3215 have_annul_true = 1;
3216 if (XVECEXP (def, 1, i + 2))
3217 have_annul_false = 1;
3220 delay = (struct delay_desc *) xmalloc (sizeof (struct delay_desc));
3222 delay->num = ++num_delays;
3223 delay->next = delays;
3227 /* Process a DEFINE_FUNCTION_UNIT.
3229 This gives information about a function unit contained in the CPU.
3230 We fill in a `struct function_unit_op' and a `struct function_unit'
3231 with information used later by `expand_unit'. */
3237 struct function_unit *unit;
3238 struct function_unit_op *op;
3240 /* See if we have already seen this function unit. If so, check that
3241 the multipicity and simultaneity values are the same. If not, make
3242 a structure for this function unit. */
3243 for (unit = units; unit; unit = unit->next)
3244 if (! strcmp (unit->name, XSTR (def, 0)))
3246 if (unit->multiplicity != XINT (def, 1)
3247 || unit->simultaneity != XINT (def, 2))
3248 fatal ("Differing specifications given for `%s' function unit.",
3255 unit = (struct function_unit *) xmalloc (sizeof (struct function_unit));
3256 unit->name = XSTR (def, 0);
3257 unit->multiplicity = XINT (def, 1);
3258 unit->simultaneity = XINT (def, 2);
3259 unit->num = num_units++;
3260 unit->num_opclasses = 0;
3261 unit->condexp = false_rtx;
3267 /* Make a new operation class structure entry and initialize it. */
3268 op = (struct function_unit_op *) xmalloc (sizeof (struct function_unit_op));
3269 op->condexp = XEXP (def, 3);
3270 op->num = unit->num_opclasses++;
3271 op->ready = XINT (def, 4);
3272 op->next = unit->ops;
3275 /* Set our busy expression based on whether or not an optional conflict
3276 vector was specified. */
3279 /* Compute the IOR of all the specified expressions. */
3280 rtx orexp = false_rtx;
3283 for (i = 0; i < XVECLEN (def, 6); i++)
3284 orexp = insert_right_side (IOR, orexp, XVECEXP (def, 6, i), -2);
3286 op->busyexp = attr_rtx (IF_THEN_ELSE, orexp,
3287 make_numeric_value (XINT (def, 5)),
3288 make_numeric_value (0));
3291 op->busyexp = make_numeric_value (XINT (def, 5));
3293 /* Merge our conditional into that of the function unit so we can determine
3294 which insns are used by the function unit. */
3295 unit->condexp = insert_right_side (IOR, unit->condexp, op->condexp, -2);
3298 /* Given a piece of RTX, print a C expression to test it's truth value.
3299 We use AND and IOR both for logical and bit-wise operations, so
3300 interpret them as logical unless they are inside a comparison expression.
3301 The second operand of this function will be non-zero in that case. */
3304 write_test_expr (exp, in_comparison)
3308 int comparison_operator = 0;
3310 struct attr_desc *attr;
3312 /* In order not to worry about operator precedence, surround our part of
3313 the expression with parentheses. */
3316 code = GET_CODE (exp);
3319 /* Binary operators. */
3321 case GE: case GT: case GEU: case GTU:
3322 case LE: case LT: case LEU: case LTU:
3323 comparison_operator = 1;
3325 case PLUS: case MINUS: case MULT: case DIV: case MOD:
3326 case AND: case IOR: case XOR:
3327 case LSHIFT: case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3328 write_test_expr (XEXP (exp, 0), in_comparison || comparison_operator);
3344 printf (" >= (unsigned) ");
3347 printf (" > (unsigned) ");
3356 printf (" <= (unsigned) ");
3359 printf (" < (unsigned) ");
3401 write_test_expr (XEXP (exp, 1), in_comparison || comparison_operator);
3405 /* Special-case (not (eq_attrq "alternative" "x")) */
3406 if (! in_comparison && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3407 && XSTR (XEXP (exp, 0), 0) == alternative_name)
3409 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
3413 /* Otherwise, fall through to normal unary operator. */
3415 /* Unary operators. */
3433 write_test_expr (XEXP (exp, 0), in_comparison);
3436 /* Comparison test of an attribute with a value. Most of these will
3437 have been removed by optimization. Handle "alternative"
3438 specially and give error if EQ_ATTR present inside a comparison. */
3441 fatal ("EQ_ATTR not valid inside comparison");
3443 if (XSTR (exp, 0) == alternative_name)
3445 printf ("which_alternative == %s", XSTR (exp, 1));
3449 attr = find_attr (XSTR (exp, 0), 0);
3450 if (! attr) abort ();
3451 printf ("get_attr_%s (insn) == ", attr->name);
3452 write_attr_valueq (attr, XSTR (exp, 1));
3455 /* See if an operand matches a predicate. */
3457 /* If only a mode is given, just ensure the mode matches the operand.
3458 If neither a mode nor predicate is given, error. */
3459 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
3461 if (GET_MODE (exp) == VOIDmode)
3462 fatal ("Null MATCH_OPERAND specified as test");
3464 printf ("GET_MODE (operands[%d]) == %smode",
3465 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3468 printf ("%s (operands[%d], %smode)",
3469 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3472 /* Constant integer. */
3474 printf ("%d", XINT (exp, 0));
3477 /* A random C expression. */
3479 printf ("%s", XSTR (exp, 0));
3482 /* The address of the branch target. */
3484 printf ("insn_addresses[INSN_UID (JUMP_LABEL (insn))]");
3487 /* The address of the current insn. It would be more consistent with
3488 other usage to make this the address of the NEXT insn, but this gets
3489 too confusing because of the ambiguity regarding the length of the
3492 printf ("insn_current_address");
3496 fatal ("bad RTX code `%s' in attribute calculation\n",
3497 GET_RTX_NAME (code));
3503 /* Given an attribute value, return the maximum CONST_STRING argument
3504 encountered. It is assumed that they are all numeric. */
3507 max_attr_value (exp)
3510 int current_max = 0;
3514 if (GET_CODE (exp) == CONST_STRING)
3515 return atoi (XSTR (exp, 0));
3517 else if (GET_CODE (exp) == COND)
3519 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3521 n = max_attr_value (XVECEXP (exp, 0, i + 1));
3522 if (n > current_max)
3526 n = max_attr_value (XEXP (exp, 1));
3527 if (n > current_max)
3537 /* Scan an attribute value, possibly a conditional, and record what actions
3538 will be required to do any conditional tests in it.
3541 `must_extract' if we need to extract the insn operands
3542 `must_constrain' if we must compute `which_alternative'
3543 `address_used' if an address expression was used
3547 walk_attr_value (exp)
3557 code = GET_CODE (exp);
3561 if (! RTX_UNCHANGING_P (exp))
3562 /* Since this is an arbitrary expression, it can look at anything.
3563 However, constant expressions do not depend on any particular
3565 must_extract = must_constrain = 1;
3573 if (XSTR (exp, 0) == alternative_name)
3574 must_extract = must_constrain = 1;
3583 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
3588 walk_attr_value (XEXP (exp, i));
3592 if (XVEC (exp, i) != NULL)
3593 for (j = 0; j < XVECLEN (exp, i); j++)
3594 walk_attr_value (XVECEXP (exp, i, j));
3599 /* Write out a function to obtain the attribute for a given INSN. */
3602 write_attr_get (attr)
3603 struct attr_desc *attr;
3605 struct attr_value *av, *common_av;
3607 /* Find the most used attribute value. Handle that as the `default' of the
3608 switch we will generate. */
3609 common_av = find_most_used (attr);
3611 /* Write out start of function, then all values with explicit `case' lines,
3612 then a `default', then the value with the most uses. */
3613 if (attr->is_numeric)
3616 printf ("enum attr_%s\n", attr->name);
3618 /* If the attribute name starts with a star, the remainder is the name of
3619 the subroutine to use, instead of `get_attr_...'. */
3620 if (attr->name[0] == '*')
3621 printf ("%s (insn)\n", &attr->name[1]);
3622 else if (attr->is_const == 0)
3623 printf ("get_attr_%s (insn)\n", attr->name);
3626 printf ("get_attr_%s ()\n", attr->name);
3629 for (av = attr->first_value; av; av = av->next)
3630 if (av->num_insns != 0)
3631 write_attr_set (attr, 2, av->value, "return", ";",
3632 true_rtx, av->first_insn->insn_code,
3633 av->first_insn->insn_index);
3638 printf (" rtx insn;\n");
3640 printf (" switch (recog_memoized (insn))\n");
3643 for (av = attr->first_value; av; av = av->next)
3644 if (av != common_av)
3645 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
3647 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
3648 printf (" }\n}\n\n");
3651 /* Given an AND tree of known true terms (because we are inside an `if' with
3652 that as the condition or are in an `else' clause) and an expression,
3653 replace any known true terms with TRUE. Use `simplify_and_tree' to do
3654 the bulk of the work. */
3657 eliminate_known_true (known_true, exp, insn_code, insn_index)
3660 int insn_code, insn_index;
3664 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
3666 if (GET_CODE (known_true) == AND)
3668 exp = eliminate_known_true (XEXP (known_true, 0), exp,
3669 insn_code, insn_index);
3670 exp = eliminate_known_true (XEXP (known_true, 1), exp,
3671 insn_code, insn_index);
3676 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
3682 /* Write out a series of tests and assignment statements to perform tests and
3683 sets of an attribute value. We are passed an indentation amount and prefix
3684 and suffix strings to write around each attribute value (e.g., "return"
3688 write_attr_set (attr, indent, value, prefix, suffix, known_true,
3689 insn_code, insn_index)
3690 struct attr_desc *attr;
3696 int insn_code, insn_index;
3698 if (GET_CODE (value) == CONST_STRING)
3700 write_indent (indent);
3701 printf ("%s ", prefix);
3702 write_attr_value (attr, value);
3703 printf ("%s\n", suffix);
3705 else if (GET_CODE (value) == COND)
3707 /* Assume the default value will be the default of the COND unless we
3708 find an always true expression. */
3709 rtx default_val = XEXP (value, 1);
3710 rtx our_known_true = known_true;
3715 for (i = 0; i < XVECLEN (value, 0); i += 2)
3720 testexp = eliminate_known_true (our_known_true,
3721 XVECEXP (value, 0, i),
3722 insn_code, insn_index);
3723 newexp = attr_rtx (NOT, testexp);
3724 newexp = insert_right_side (AND, our_known_true, newexp,
3725 insn_code, insn_index);
3727 /* If the test expression is always true or if the next `known_true'
3728 expression is always false, this is the last case, so break
3729 out and let this value be the `else' case. */
3730 if (testexp == true_rtx || newexp == false_rtx)
3732 default_val = XVECEXP (value, 0, i + 1);
3736 /* Compute the expression to pass to our recursive call as being
3738 inner_true = insert_right_side (AND, our_known_true,
3739 testexp, insn_code, insn_index);
3741 /* If this is always false, skip it. */
3742 if (inner_true == false_rtx)
3745 write_indent (indent);
3746 printf ("%sif ", first_if ? "" : "else ");
3748 write_test_expr (testexp, 0);
3750 write_indent (indent + 2);
3753 write_attr_set (attr, indent + 4,
3754 XVECEXP (value, 0, i + 1), prefix, suffix,
3755 inner_true, insn_code, insn_index);
3756 write_indent (indent + 2);
3758 our_known_true = newexp;
3763 write_indent (indent);
3765 write_indent (indent + 2);
3769 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
3770 prefix, suffix, our_known_true, insn_code, insn_index);
3774 write_indent (indent + 2);
3782 /* Write out the computation for one attribute value. */
3785 write_attr_case (attr, av, write_case_lines, prefix, suffix, indent, known_true)
3786 struct attr_desc *attr;
3787 struct attr_value *av;
3788 int write_case_lines;
3789 char *prefix, *suffix;
3793 struct insn_ent *ie;
3795 if (av->num_insns == 0)
3798 if (av->has_asm_insn)
3800 write_indent (indent);
3801 printf ("case -1:\n");
3802 write_indent (indent + 2);
3803 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
3804 write_indent (indent + 2);
3805 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
3806 write_indent (indent + 2);
3807 printf (" fatal_insn_not_found (insn);\n");
3810 if (write_case_lines)
3812 for (ie = av->first_insn; ie; ie = ie->next)
3813 if (ie->insn_code != -1)
3815 write_indent (indent);
3816 printf ("case %d:\n", ie->insn_code);
3821 write_indent (indent);
3822 printf ("default:\n");
3825 /* See what we have to do to handle output this value. */
3826 must_extract = must_constrain = address_used = 0;
3827 walk_attr_value (av->value);
3831 write_indent (indent + 2);
3832 printf ("insn_extract (insn);\n");
3837 #ifdef REGISTER_CONSTRAINTS
3838 write_indent (indent + 2);
3839 printf ("if (! constrain_operands (INSN_CODE (insn), reload_completed))\n");
3840 write_indent (indent + 2);
3841 printf (" fatal_insn_not_found (insn);\n");
3845 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
3846 known_true, av->first_insn->insn_code,
3847 av->first_insn->insn_index);
3849 if (strncmp (prefix, "return", 6))
3851 write_indent (indent + 2);
3852 printf ("break;\n");
3857 /* Utilities to write names in various forms. */
3860 write_attr_valueq (attr, s)
3861 struct attr_desc *attr;
3864 if (attr->is_numeric)
3868 write_upcase (attr->name);
3875 write_attr_value (attr, value)
3876 struct attr_desc *attr;
3879 if (GET_CODE (value) != CONST_STRING)
3882 write_attr_valueq (attr, XSTR (value, 0));
3890 if (*str < 'a' || *str > 'z')
3891 printf ("%c", *str++);
3893 printf ("%c", *str++ - 'a' + 'A');
3897 write_indent (indent)
3900 for (; indent > 8; indent -= 8)
3903 for (; indent; indent--)
3907 /* Write a subroutine that is given an insn that requires a delay slot, a
3908 delay slot ordinal, and a candidate insn. It returns non-zero if the
3909 candidate can be placed in the specified delay slot of the insn.
3911 We can write as many as three subroutines. `eligible_for_delay'
3912 handles normal delay slots, `eligible_for_annul_true' indicates that
3913 the specified insn can be annulled if the branch is true, and likewise
3914 for `eligible_for_annul_false'.
3916 KIND is a string distingushing these three cases ("delay", "annul_true",
3917 or "annul_false"). */
3920 write_eligible_delay (kind)
3923 struct delay_desc *delay;
3926 struct attr_desc *attr;
3927 struct attr_value *av, *common_av;
3930 /* Compute the maximum number of delay slots required. We use the delay
3931 ordinal times this number plus one, plus the slot number as an index into
3932 the appropriate predicate to test. */
3934 for (delay = delays, max_slots = 0; delay; delay = delay->next)
3935 if (XVECLEN (delay->def, 1) / 3 > max_slots)
3936 max_slots = XVECLEN (delay->def, 1) / 3;
3938 /* Write function prelude. */
3941 printf ("eligible_for_%s (delay_insn, slot, candidate_insn)\n", kind);
3942 printf (" rtx delay_insn;\n");
3943 printf (" int slot;\n");
3944 printf (" rtx candidate_insn;\n");
3946 printf (" rtx insn;\n");
3948 printf (" if (slot >= %d)\n", max_slots);
3949 printf (" abort ();\n");
3952 /* If more than one delay type, find out which type the delay insn is. */
3956 attr = find_attr ("*delay_type", 0);
3957 if (! attr) abort ();
3958 common_av = find_most_used (attr);
3960 printf (" insn = delay_insn;\n");
3961 printf (" switch (recog_memoized (insn))\n");
3964 sprintf (str, " * %d;\n break;", max_slots);
3965 for (av = attr->first_value; av; av = av->next)
3966 if (av != common_av)
3967 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
3969 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
3972 /* Ensure matched. Otherwise, shouldn't have been called. */
3973 printf (" if (slot < %d)\n", max_slots);
3974 printf (" abort ();\n\n");
3977 /* If just one type of delay slot, write simple switch. */
3978 if (num_delays == 1 && max_slots == 1)
3980 printf (" insn = candidate_insn;\n");
3981 printf (" switch (recog_memoized (insn))\n");
3984 attr = find_attr ("*delay_1_0", 0);
3985 if (! attr) abort ();
3986 common_av = find_most_used (attr);
3988 for (av = attr->first_value; av; av = av->next)
3989 if (av != common_av)
3990 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
3992 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
3998 /* Write a nested CASE. The first indicates which condition we need to
3999 test, and the inner CASE tests the condition. */
4000 printf (" insn = candidate_insn;\n");
4001 printf (" switch (slot)\n");
4004 for (delay = delays; delay; delay = delay->next)
4005 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
4007 printf (" case %d:\n",
4008 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
4009 printf (" switch (recog_memoized (insn))\n");
4012 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
4013 attr = find_attr (str, 0);
4014 if (! attr) abort ();
4015 common_av = find_most_used (attr);
4017 for (av = attr->first_value; av; av = av->next)
4018 if (av != common_av)
4019 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
4021 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
4025 printf (" default:\n");
4026 printf (" abort ();\n");
4033 /* Write routines to compute conflict cost for function units. Then write a
4034 table describing the available function units. */
4037 write_function_unit_info ()
4039 struct function_unit *unit;
4040 struct attr_desc *case_attr, *attr;
4041 struct attr_value *av, *common_av;
4047 /* Write out conflict routines for function units. Don't bother writing
4048 one if there is only one busy value. */
4050 for (unit = units; unit; unit = unit->next)
4052 /* See if only one case exists and if there is a constant value for
4053 that case. If so, we don't need a function. */
4054 str = (char *) alloca (strlen (unit->name) + 10);
4055 sprintf (str, "*%s_cases", unit->name);
4056 attr = find_attr (str, 0);
4057 if (! attr) abort ();
4058 value = find_single_value (attr);
4059 if (value && GET_CODE (value) == CONST_STRING)
4061 sprintf (str, "*%s_case_%s", unit->name, XSTR (value, 0));
4062 attr = find_attr (str, 0);
4063 if (! attr) abort ();
4064 value = find_single_value (attr);
4065 if (value && GET_CODE (value) == CONST_STRING)
4067 unit->needs_conflict_function = 0;
4068 unit->default_cost = value;
4073 /* The function first computes the case from the candidate insn. */
4074 unit->needs_conflict_function = 1;
4075 unit->default_cost = make_numeric_value (0);
4077 printf ("static int\n");
4078 printf ("%s_unit_conflict_cost (executing_insn, candidate_insn)\n",
4080 printf (" rtx executing_insn;\n");
4081 printf (" rtx candidate_insn;\n");
4083 printf (" rtx insn;\n");
4084 printf (" int casenum;\n\n");
4085 printf (" insn = candidate_insn;\n");
4086 printf (" switch (recog_memoized (insn))\n");
4089 /* Write the `switch' statement to get the case value. */
4090 sprintf (str, "*%s_cases", unit->name);
4091 case_attr = find_attr (str, 0);
4092 if (! case_attr) abort ();
4093 common_av = find_most_used (case_attr);
4095 for (av = case_attr->first_value; av; av = av->next)
4096 if (av != common_av)
4097 write_attr_case (case_attr, av, 1,
4098 "casenum =", ";", 4, unit->condexp);
4100 write_attr_case (case_attr, common_av, 0,
4101 "casenum =", ";", 4, unit->condexp);
4104 /* Now write an outer switch statement on each case. Then write
4105 the tests on the executing function within each. */
4106 printf (" insn = executing_insn;\n");
4107 printf (" switch (casenum)\n");
4110 for (i = 0; i < unit->num_opclasses; i++)
4112 /* Ensure using this case. */
4114 for (av = case_attr->first_value; av; av = av->next)
4116 && contained_in_p (make_numeric_value (i), av->value))
4122 printf (" case %d:\n", i);
4123 sprintf (str, "*%s_case_%d", unit->name, i);
4124 attr = find_attr (str, 0);
4125 if (! attr) abort ();
4127 /* If single value, just write it. */
4128 value = find_single_value (attr);
4130 write_attr_set (attr, 6, value, "return", ";\n", true_rtx, -2);
4133 common_av = find_most_used (attr);
4134 printf (" switch (recog_memoized (insn))\n");
4137 for (av = attr->first_value; av; av = av->next)
4138 if (av != common_av)
4139 write_attr_case (attr, av, 1,
4140 "return", ";", 8, unit->condexp);
4142 write_attr_case (attr, common_av, 0,
4143 "return", ";", 8, unit->condexp);
4148 printf (" }\n}\n\n");
4151 /* Now that all functions have been written, write the table describing
4152 the function units. The name is included for documenation purposes
4155 printf ("struct function_unit_desc function_units[] = {\n");
4157 for (unit = units; unit; unit = unit->next)
4159 printf (" {\"%s\", %d, %d, %d, %s, %s_unit_ready_cost, ",
4160 unit->name, 1 << unit->num, unit->multiplicity,
4161 unit->simultaneity, XSTR (unit->default_cost, 0), unit->name);
4163 if (unit->needs_conflict_function)
4164 printf ("%s_unit_conflict_cost", unit->name);
4174 /* This page contains miscellaneous utility routines. */
4176 /* Given a string, return the number of comma-separated elements in it.
4177 Return 0 for the null string. */
4188 for (n = 1; *s; s++)
4195 /* Given a pointer to a (char *), return a malloc'ed string containing the
4196 next comma-separated element. Advance the pointer to after the string
4197 scanned, or the end-of-string. Return NULL if at end of string. */
4200 next_comma_elt (pstr)
4209 /* Find end of string to compute length. */
4210 for (p = *pstr; *p != ',' && *p != '\0'; p++)
4213 out_str = attr_string (*pstr, p - *pstr);
4222 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
4223 is non-zero, build a new attribute, if one does not exist. */
4225 static struct attr_desc *
4226 find_attr (name, create)
4230 struct attr_desc *attr;
4233 /* Before we resort to using `strcmp', see if the string address matches
4234 anywhere. In most cases, it should have been canonicalized to do so. */
4235 if (name == alternative_name)
4238 index = name[0] & (MAX_ATTRS_INDEX - 1);
4239 for (attr = attrs[index]; attr; attr = attr->next)
4240 if (name == attr->name)
4243 /* Otherwise, do it the slow way. */
4244 for (attr = attrs[index]; attr; attr = attr->next)
4245 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
4251 attr = (struct attr_desc *) xmalloc (sizeof (struct attr_desc));
4252 attr->name = attr_string (name, strlen (name));
4253 attr->first_value = attr->default_val = NULL;
4254 attr->is_numeric = attr->is_const = attr->is_special = 0;
4255 attr->next = attrs[index];
4256 attrs[index] = attr;
4261 /* Create internal attribute with the given default value. */
4264 make_internal_attr (name, value, special)
4269 struct attr_desc *attr;
4271 attr = find_attr (name, 1);
4272 if (attr->default_val)
4275 attr->is_numeric = 1;
4277 attr->is_special = special;
4278 attr->default_val = get_attr_value (value, attr, -2);
4281 /* Find the most used value of an attribute. */
4283 static struct attr_value *
4284 find_most_used (attr)
4285 struct attr_desc *attr;
4287 struct attr_value *av;
4288 struct attr_value *most_used;
4294 for (av = attr->first_value; av; av = av->next)
4295 if (av->num_insns > nuses)
4296 nuses = av->num_insns, most_used = av;
4301 /* If an attribute only has a single value used, return it. Otherwise
4305 find_single_value (attr)
4306 struct attr_desc *attr;
4308 struct attr_value *av;
4311 unique_value = NULL;
4312 for (av = attr->first_value; av; av = av->next)
4318 unique_value = av->value;
4321 return unique_value;
4324 /* Return (attr_value "n") */
4327 make_numeric_value (n)
4330 static rtx int_values[20];
4337 if (n < 20 && int_values[n])
4338 return int_values[n];
4340 p = attr_printf ((n < 1000 ? 4 : HOST_BITS_PER_INT * 3 / 10 + 3), "%d", n);
4341 exp = attr_rtx (CONST_STRING, p);
4344 int_values[n] = exp;
4350 xrealloc (ptr, size)
4354 char *result = (char *) realloc (ptr, size);
4356 fatal ("virtual memory exhausted");
4364 register char *val = (char *) malloc (size);
4367 fatal ("virtual memory exhausted");
4372 copy_rtx_unchanging (orig)
4376 register RTX_CODE code;
4378 if (RTX_UNCHANGING_P (orig) || MEM_IN_STRUCT_P (orig))
4381 MEM_IN_STRUCT_P (orig) = 1;
4385 code = GET_CODE (orig);
4395 copy = rtx_alloc (code);
4396 PUT_MODE (copy, GET_MODE (orig));
4397 RTX_UNCHANGING_P (copy) = 1;
4399 bcopy (&XEXP (orig, 0), &XEXP (copy, 0),
4400 GET_RTX_LENGTH (GET_CODE (copy)) * sizeof (rtx));
4409 fprintf (stderr, "genattrtab: ");
4410 fprintf (stderr, s, a1, a2);
4411 fprintf (stderr, "\n");
4412 exit (FATAL_EXIT_CODE);
4415 /* More 'friendly' abort that prints the line and file.
4416 config.h can #define abort fancy_abort if you like that sort of thing. */
4421 fatal ("Internal gcc abort.");
4432 struct attr_desc *attr;
4433 struct attr_value *av;
4434 struct insn_def *id;
4438 obstack_init (rtl_obstack);
4439 obstack_init (hash_obstack);
4440 obstack_init (temp_obstack);
4443 fatal ("No input file name.");
4445 infile = fopen (argv[1], "r");
4449 exit (FATAL_EXIT_CODE);
4454 /* Set up true and false rtx's */
4455 true_rtx = rtx_alloc (CONST_INT);
4456 XINT (true_rtx, 0) = 1;
4457 false_rtx = rtx_alloc (CONST_INT);
4458 XINT (false_rtx, 0) = 0;
4459 RTX_UNCHANGING_P (true_rtx) = RTX_UNCHANGING_P (false_rtx) = 1;
4460 RTX_INTEGRATED_P (true_rtx) = RTX_INTEGRATED_P (false_rtx) = 1;
4462 alternative_name = attr_string ("alternative", strlen ("alternative"));
4464 printf ("/* Generated automatically by the program `genattrtab'\n\
4465 from the machine description file `md'. */\n\n");
4467 /* Read the machine description. */
4471 c = read_skip_spaces (infile);
4476 desc = read_rtx (infile);
4477 if (GET_CODE (desc) == DEFINE_INSN
4478 || GET_CODE (desc) == DEFINE_PEEPHOLE
4479 || GET_CODE (desc) == DEFINE_ASM_ATTRIBUTES)
4482 else if (GET_CODE (desc) == DEFINE_EXPAND)
4483 insn_code_number++, insn_index_number++;
4485 else if (GET_CODE (desc) == DEFINE_SPLIT)
4486 insn_code_number++, insn_index_number++;
4488 else if (GET_CODE (desc) == DEFINE_ATTR)
4491 insn_index_number++;
4494 else if (GET_CODE (desc) == DEFINE_DELAY)
4497 insn_index_number++;
4500 else if (GET_CODE (desc) == DEFINE_FUNCTION_UNIT)
4503 insn_index_number++;
4507 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
4508 if (! got_define_asm_attributes)
4510 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
4511 XVEC (tem, 0) = rtvec_alloc (0);
4515 /* Expand DEFINE_DELAY information into new attribute. */
4519 /* Expand DEFINE_FUNCTION_UNIT information into new attributes. */
4523 printf ("#include \"config.h\"\n");
4524 printf ("#include \"rtl.h\"\n");
4525 printf ("#include \"insn-config.h\"\n");
4526 printf ("#include \"recog.h\"\n");
4527 printf ("#include \"regs.h\"\n");
4528 printf ("#include \"real.h\"\n");
4529 printf ("#include \"output.h\"\n");
4530 printf ("#include \"insn-attr.h\"\n");
4532 printf ("#define operands recog_operand\n\n");
4534 /* Make `insn_alternatives'. */
4535 insn_alternatives = (int *) xmalloc (insn_code_number * sizeof (int));
4536 for (id = defs; id; id = id->next)
4537 if (id->insn_code >= 0)
4538 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
4540 /* Make `insn_n_alternatives'. */
4541 insn_n_alternatives = (int *) xmalloc (insn_code_number * sizeof (int));
4542 for (id = defs; id; id = id->next)
4543 if (id->insn_code >= 0)
4544 insn_n_alternatives[id->insn_code] = id->num_alternatives;
4546 /* Prepare to write out attribute subroutines by checking everything stored
4547 away and building the attribute cases. */
4550 for (i = 0; i < MAX_ATTRS_INDEX; i++)
4551 for (attr = attrs[i]; attr; attr = attr->next)
4553 attr->default_val->value
4554 = check_attr_value (attr->default_val->value, attr);
4558 /* Construct extra attributes for `length'. */
4559 make_length_attrs ();
4561 /* Perform any possible optimizations to speed up compilation. */
4564 /* Now write out all the `gen_attr_...' routines. Do these before the
4565 special routines (specifically before write_function_unit_info), so
4566 that they get defined before they are used. */
4568 for (i = 0; i < MAX_ATTRS_INDEX; i++)
4569 for (attr = attrs[i]; attr; attr = attr->next)
4571 if (! attr->is_special)
4572 write_attr_get (attr);
4575 /* Write out delay eligibility information, if DEFINE_DELAY present.
4576 (The function to compute the number of delay slots will be written
4580 write_eligible_delay ("delay");
4581 if (have_annul_true)
4582 write_eligible_delay ("annul_true");
4583 if (have_annul_false)
4584 write_eligible_delay ("annul_false");
4587 /* Write out information about function units. */
4589 write_function_unit_info ();
4592 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);