1 /* Generate code from machine description to compute values of attributes.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* This program handles insn attributes and the DEFINE_DELAY and
24 DEFINE_INSN_RESERVATION definitions.
26 It produces a series of functions named `get_attr_...', one for each insn
27 attribute. Each of these is given the rtx for an insn and returns a member
28 of the enum for the attribute.
30 These subroutines have the form of a `switch' on the INSN_CODE (via
31 `recog_memoized'). Each case either returns a constant attribute value
32 or a value that depends on tests on other attributes, the form of
33 operands, or some random C expression (encoded with a SYMBOL_REF
36 If the attribute `alternative', or a random C expression is present,
37 `constrain_operands' is called. If either of these cases of a reference to
38 an operand is found, `extract_insn' is called.
40 The special attribute `length' is also recognized. For this operand,
41 expressions involving the address of an operand or the current insn,
42 (address (pc)), are valid. In this case, an initial pass is made to
43 set all lengths that do not depend on address. Those that do are set to
44 the maximum length. Then each insn that depends on an address is checked
45 and possibly has its length changed. The process repeats until no further
46 changed are made. The resulting lengths are saved for use by
49 A special form of DEFINE_ATTR, where the expression for default value is a
50 CONST expression, indicates an attribute that is constant for a given run
51 of the compiler. The subroutine generated for these attributes has no
52 parameters as it does not depend on any particular insn. Constant
53 attributes are typically used to specify which variety of processor is
56 Internal attributes are defined to handle DEFINE_DELAY and
57 DEFINE_INSN_RESERVATION. Special routines are output for these cases.
59 This program works by keeping a list of possible values for each attribute.
60 These include the basic attribute choices, default values for attribute, and
61 all derived quantities.
63 As the description file is read, the definition for each insn is saved in a
64 `struct insn_def'. When the file reading is complete, a `struct insn_ent'
65 is created for each insn and chained to the corresponding attribute value,
66 either that specified, or the default.
68 An optimization phase is then run. This simplifies expressions for each
69 insn. EQ_ATTR tests are resolved, whenever possible, to a test that
70 indicates when the attribute has the specified value for the insn. This
71 avoids recursive calls during compilation.
73 The strategy used when processing DEFINE_DELAY definitions is to create
74 arbitrarily complex expressions and have the optimization simplify them.
76 Once optimization is complete, any required routines and definitions
79 An optimization that is not yet implemented is to hoist the constant
80 expressions entirely out of the routines and definitions that are written.
81 A way to do this is to iterate over all possible combinations of values
82 for constant attributes and generate a set of functions for that given
83 combination. An initialization function would be written that evaluates
84 the attributes and installs the corresponding set of routines and
85 definitions (each would be accessed through a pointer).
87 We use the flags in an RTX as follows:
88 `unchanging' (ATTR_IND_SIMPLIFIED_P): This rtx is fully simplified
89 independent of the insn code.
90 `in_struct' (ATTR_CURR_SIMPLIFIED_P): This rtx is fully simplified
91 for the insn code currently being processed (see optimize_attrs).
92 `return_val' (ATTR_PERMANENT_P): This rtx is permanent and unique
95 #define ATTR_IND_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), unchanging))
96 #define ATTR_CURR_SIMPLIFIED_P(RTX) (RTX_FLAG((RTX), in_struct))
97 #define ATTR_PERMANENT_P(RTX) (RTX_FLAG((RTX), return_val))
100 #define strcmp_check(S1, S2) ((S1) == (S2) \
102 : (gcc_assert (strcmp ((S1), (S2))), 1))
104 #define strcmp_check(S1, S2) ((S1) != (S2))
109 #include "coretypes.h"
115 #include "gensupport.h"
119 /* Flags for make_internal_attr's `special' parameter. */
121 #define ATTR_SPECIAL (1 << 0)
123 static struct obstack obstack1, obstack2;
124 static struct obstack *hash_obstack = &obstack1;
125 static struct obstack *temp_obstack = &obstack2;
127 /* enough space to reserve for printing out ints */
128 #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
130 /* Define structures used to record attributes and values. */
132 /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
133 encountered, we store all the relevant information into a
134 `struct insn_def'. This is done to allow attribute definitions to occur
135 anywhere in the file. */
139 struct insn_def *next; /* Next insn in chain. */
140 rtx def; /* The DEFINE_... */
141 int insn_code; /* Instruction number. */
142 int insn_index; /* Expression number in file, for errors. */
143 int lineno; /* Line number. */
144 int num_alternatives; /* Number of alternatives. */
145 int vec_idx; /* Index of attribute vector in `def'. */
148 /* Once everything has been read in, we store in each attribute value a list
149 of insn codes that have that value. Here is the structure used for the
154 struct insn_ent *next; /* Next in chain. */
155 struct insn_def *def; /* Instruction definition. */
158 /* Each value of an attribute (either constant or computed) is assigned a
159 structure which is used as the listhead of the insns that have that
164 rtx value; /* Value of attribute. */
165 struct attr_value *next; /* Next attribute value in chain. */
166 struct insn_ent *first_insn; /* First insn with this value. */
167 int num_insns; /* Number of insns with this value. */
168 int has_asm_insn; /* True if this value used for `asm' insns */
171 /* Structure for each attribute. */
175 char *name; /* Name of attribute. */
176 const char *enum_name; /* Enum name for DEFINE_ENUM_NAME. */
177 struct attr_desc *next; /* Next attribute. */
178 struct attr_value *first_value; /* First value of this attribute. */
179 struct attr_value *default_val; /* Default value for this attribute. */
180 int lineno : 24; /* Line number. */
181 unsigned is_numeric : 1; /* Values of this attribute are numeric. */
182 unsigned is_const : 1; /* Attribute value constant for each run. */
183 unsigned is_special : 1; /* Don't call `write_attr_set'. */
186 /* Structure for each DEFINE_DELAY. */
190 rtx def; /* DEFINE_DELAY expression. */
191 struct delay_desc *next; /* Next DEFINE_DELAY. */
192 int num; /* Number of DEFINE_DELAY, starting at 1. */
193 int lineno; /* Line number. */
196 struct attr_value_list
198 struct attr_value *av;
200 struct attr_desc *attr;
201 struct attr_value_list *next;
204 /* Listheads of above structures. */
206 /* This one is indexed by the first character of the attribute name. */
207 #define MAX_ATTRS_INDEX 256
208 static struct attr_desc *attrs[MAX_ATTRS_INDEX];
209 static struct insn_def *defs;
210 static struct delay_desc *delays;
211 struct attr_value_list **insn_code_values;
213 /* Other variables. */
215 static int insn_code_number;
216 static int insn_index_number;
217 static int got_define_asm_attributes;
218 static int must_extract;
219 static int must_constrain;
220 static int address_used;
221 static int length_used;
222 static int num_delays;
223 static int have_annul_true, have_annul_false;
224 static int num_insn_ents;
226 /* Stores, for each insn code, the number of constraint alternatives. */
228 static int *insn_n_alternatives;
230 /* Stores, for each insn code, a bitmap that has bits on for each possible
233 static int *insn_alternatives;
235 /* Used to simplify expressions. */
237 static rtx true_rtx, false_rtx;
239 /* Used to reduce calls to `strcmp' */
241 static const char *alternative_name;
242 static const char *length_str;
243 static const char *delay_type_str;
244 static const char *delay_1_0_str;
245 static const char *num_delay_slots_str;
247 /* Simplify an expression. Only call the routine if there is something to
249 #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
250 (ATTR_IND_SIMPLIFIED_P (EXP) || ATTR_CURR_SIMPLIFIED_P (EXP) ? (EXP) \
251 : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
253 #define DEF_ATTR_STRING(S) (attr_string ((S), strlen (S)))
255 /* Forward declarations of functions used before their definitions, only. */
256 static char *attr_string (const char *, int);
257 static char *attr_printf (unsigned int, const char *, ...)
259 static rtx make_numeric_value (int);
260 static struct attr_desc *find_attr (const char **, int);
261 static rtx mk_attr_alt (int);
262 static char *next_comma_elt (const char **);
263 static rtx insert_right_side (enum rtx_code, rtx, rtx, int, int);
264 static rtx copy_boolean (rtx);
265 static int compares_alternatives_p (rtx);
266 static void make_internal_attr (const char *, rtx, int);
267 static void insert_insn_ent (struct attr_value *, struct insn_ent *);
268 static void walk_attr_value (rtx);
269 static int max_attr_value (rtx, int*);
270 static int min_attr_value (rtx, int*);
271 static int or_attr_value (rtx, int*);
272 static rtx simplify_test_exp (rtx, int, int);
273 static rtx simplify_test_exp_in_temp (rtx, int, int);
274 static rtx copy_rtx_unchanging (rtx);
275 static bool attr_alt_subset_p (rtx, rtx);
276 static bool attr_alt_subset_of_compl_p (rtx, rtx);
277 static void clear_struct_flag (rtx);
278 static void write_attr_valueq (struct attr_desc *, const char *);
279 static struct attr_value *find_most_used (struct attr_desc *);
280 static void write_attr_set (struct attr_desc *, int, rtx,
281 const char *, const char *, rtx,
282 int, int, unsigned int);
283 static void write_attr_case (struct attr_desc *, struct attr_value *,
284 int, const char *, const char *, int, rtx);
285 static void write_attr_value (struct attr_desc *, rtx);
286 static void write_upcase (const char *);
287 static void write_indent (int);
288 static rtx identity_fn (rtx);
289 static rtx zero_fn (rtx);
290 static rtx one_fn (rtx);
291 static rtx max_fn (rtx);
292 static rtx min_fn (rtx);
294 #define oballoc(T) XOBNEW (hash_obstack, T)
295 #define oballocvec(T, N) XOBNEWVEC (hash_obstack, T, (N))
297 /* Hash table for sharing RTL and strings. */
299 /* Each hash table slot is a bucket containing a chain of these structures.
300 Strings are given negative hash codes; RTL expressions are given positive
305 struct attr_hash *next; /* Next structure in the bucket. */
306 int hashcode; /* Hash code of this rtx or string. */
309 char *str; /* The string (negative hash codes) */
310 rtx rtl; /* or the RTL recorded here. */
314 /* Now here is the hash table. When recording an RTL, it is added to
315 the slot whose index is the hash code mod the table size. Note
316 that the hash table is used for several kinds of RTL (see attr_rtx)
317 and for strings. While all these live in the same table, they are
318 completely independent, and the hash code is computed differently
321 #define RTL_HASH_SIZE 4093
322 static struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
324 /* Here is how primitive or already-shared RTL's hash
326 #define RTL_HASH(RTL) ((intptr_t) (RTL) & 0777777)
328 /* Add an entry to the hash table for RTL with hash code HASHCODE. */
331 attr_hash_add_rtx (int hashcode, rtx rtl)
335 h = XOBNEW (hash_obstack, struct attr_hash);
336 h->hashcode = hashcode;
338 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
339 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
342 /* Add an entry to the hash table for STRING with hash code HASHCODE. */
345 attr_hash_add_string (int hashcode, char *str)
349 h = XOBNEW (hash_obstack, struct attr_hash);
350 h->hashcode = -hashcode;
352 h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
353 attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
356 /* Generate an RTL expression, but avoid duplicates.
357 Set the ATTR_PERMANENT_P flag for these permanent objects.
359 In some cases we cannot uniquify; then we return an ordinary
360 impermanent rtx with ATTR_PERMANENT_P clear.
364 rtx attr_rtx (code, [element1, ..., elementn]) */
367 attr_rtx_1 (enum rtx_code code, va_list p)
369 rtx rt_val = NULL_RTX;/* RTX to return to caller... */
372 struct obstack *old_obstack = rtl_obstack;
374 /* For each of several cases, search the hash table for an existing entry.
375 Use that entry if one is found; otherwise create a new RTL and add it
378 if (GET_RTX_CLASS (code) == RTX_UNARY)
380 rtx arg0 = va_arg (p, rtx);
382 /* A permanent object cannot point to impermanent ones. */
383 if (! ATTR_PERMANENT_P (arg0))
385 rt_val = rtx_alloc (code);
386 XEXP (rt_val, 0) = arg0;
390 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
391 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
392 if (h->hashcode == hashcode
393 && GET_CODE (h->u.rtl) == code
394 && XEXP (h->u.rtl, 0) == arg0)
399 rtl_obstack = hash_obstack;
400 rt_val = rtx_alloc (code);
401 XEXP (rt_val, 0) = arg0;
404 else if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
405 || GET_RTX_CLASS (code) == RTX_COMM_ARITH
406 || GET_RTX_CLASS (code) == RTX_COMPARE
407 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
409 rtx arg0 = va_arg (p, rtx);
410 rtx arg1 = va_arg (p, rtx);
412 /* A permanent object cannot point to impermanent ones. */
413 if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
415 rt_val = rtx_alloc (code);
416 XEXP (rt_val, 0) = arg0;
417 XEXP (rt_val, 1) = arg1;
421 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
422 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
423 if (h->hashcode == hashcode
424 && GET_CODE (h->u.rtl) == code
425 && XEXP (h->u.rtl, 0) == arg0
426 && XEXP (h->u.rtl, 1) == arg1)
431 rtl_obstack = hash_obstack;
432 rt_val = rtx_alloc (code);
433 XEXP (rt_val, 0) = arg0;
434 XEXP (rt_val, 1) = arg1;
437 else if (GET_RTX_LENGTH (code) == 1
438 && GET_RTX_FORMAT (code)[0] == 's')
440 char *arg0 = va_arg (p, char *);
442 arg0 = DEF_ATTR_STRING (arg0);
444 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
445 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
446 if (h->hashcode == hashcode
447 && GET_CODE (h->u.rtl) == code
448 && XSTR (h->u.rtl, 0) == arg0)
453 rtl_obstack = hash_obstack;
454 rt_val = rtx_alloc (code);
455 XSTR (rt_val, 0) = arg0;
458 else if (GET_RTX_LENGTH (code) == 2
459 && GET_RTX_FORMAT (code)[0] == 's'
460 && GET_RTX_FORMAT (code)[1] == 's')
462 char *arg0 = va_arg (p, char *);
463 char *arg1 = va_arg (p, char *);
465 hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
466 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
467 if (h->hashcode == hashcode
468 && GET_CODE (h->u.rtl) == code
469 && XSTR (h->u.rtl, 0) == arg0
470 && XSTR (h->u.rtl, 1) == arg1)
475 rtl_obstack = hash_obstack;
476 rt_val = rtx_alloc (code);
477 XSTR (rt_val, 0) = arg0;
478 XSTR (rt_val, 1) = arg1;
481 else if (code == CONST_INT)
483 HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
493 int i; /* Array indices... */
494 const char *fmt; /* Current rtx's format... */
496 rt_val = rtx_alloc (code); /* Allocate the storage space. */
498 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
499 for (i = 0; i < GET_RTX_LENGTH (code); i++)
503 case '0': /* Unused field. */
506 case 'i': /* An integer? */
507 XINT (rt_val, i) = va_arg (p, int);
510 case 'w': /* A wide integer? */
511 XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
514 case 's': /* A string? */
515 XSTR (rt_val, i) = va_arg (p, char *);
518 case 'e': /* An expression? */
519 case 'u': /* An insn? Same except when printing. */
520 XEXP (rt_val, i) = va_arg (p, rtx);
523 case 'E': /* An RTX vector? */
524 XVEC (rt_val, i) = va_arg (p, rtvec);
534 rtl_obstack = old_obstack;
535 attr_hash_add_rtx (hashcode, rt_val);
536 ATTR_PERMANENT_P (rt_val) = 1;
541 attr_rtx (enum rtx_code code, ...)
547 result = attr_rtx_1 (code, p);
552 /* Create a new string printed with the printf line arguments into a space
553 of at most LEN bytes:
555 rtx attr_printf (len, format, [arg1, ..., argn]) */
558 attr_printf (unsigned int len, const char *fmt, ...)
565 gcc_assert (len < sizeof str); /* Leave room for \0. */
567 vsprintf (str, fmt, p);
570 return DEF_ATTR_STRING (str);
574 attr_eq (const char *name, const char *value)
576 return attr_rtx (EQ_ATTR, DEF_ATTR_STRING (name), DEF_ATTR_STRING (value));
582 return XSTR (make_numeric_value (n), 0);
585 /* Return a permanent (possibly shared) copy of a string STR (not assumed
586 to be null terminated) with LEN bytes. */
589 attr_string (const char *str, int len)
596 /* Compute the hash code. */
597 hashcode = (len + 1) * 613 + (unsigned) str[0];
598 for (i = 1; i < len; i += 2)
599 hashcode = ((hashcode * 613) + (unsigned) str[i]);
601 hashcode = -hashcode;
603 /* Search the table for the string. */
604 for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
605 if (h->hashcode == -hashcode && h->u.str[0] == str[0]
606 && !strncmp (h->u.str, str, len))
607 return h->u.str; /* <-- return if found. */
609 /* Not found; create a permanent copy and add it to the hash table. */
610 new_str = XOBNEWVAR (hash_obstack, char, len + 1);
611 memcpy (new_str, str, len);
613 attr_hash_add_string (hashcode, new_str);
615 return new_str; /* Return the new string. */
618 /* Check two rtx's for equality of contents,
619 taking advantage of the fact that if both are hashed
620 then they can't be equal unless they are the same object. */
623 attr_equal_p (rtx x, rtx y)
625 return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
626 && rtx_equal_p (x, y)));
629 /* Copy an attribute value expression,
630 descending to all depths, but not copying any
631 permanent hashed subexpressions. */
634 attr_copy_rtx (rtx orig)
639 const char *format_ptr;
641 /* No need to copy a permanent object. */
642 if (ATTR_PERMANENT_P (orig))
645 code = GET_CODE (orig);
663 copy = rtx_alloc (code);
664 PUT_MODE (copy, GET_MODE (orig));
665 ATTR_IND_SIMPLIFIED_P (copy) = ATTR_IND_SIMPLIFIED_P (orig);
666 ATTR_CURR_SIMPLIFIED_P (copy) = ATTR_CURR_SIMPLIFIED_P (orig);
667 ATTR_PERMANENT_P (copy) = ATTR_PERMANENT_P (orig);
669 format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
671 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
673 switch (*format_ptr++)
676 XEXP (copy, i) = XEXP (orig, i);
677 if (XEXP (orig, i) != NULL)
678 XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
683 XVEC (copy, i) = XVEC (orig, i);
684 if (XVEC (orig, i) != NULL)
686 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
687 for (j = 0; j < XVECLEN (copy, i); j++)
688 XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
694 XINT (copy, i) = XINT (orig, i);
698 XWINT (copy, i) = XWINT (orig, i);
703 XSTR (copy, i) = XSTR (orig, i);
713 /* Given a test expression for an attribute, ensure it is validly formed.
714 IS_CONST indicates whether the expression is constant for each compiler
715 run (a constant expression may not test any particular insn).
717 Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
718 and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
719 test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
721 Update the string address in EQ_ATTR expression to be the same used
722 in the attribute (or `alternative_name') to speed up subsequent
723 `find_attr' calls and eliminate most `strcmp' calls.
725 Return the new expression, if any. */
728 check_attr_test (rtx exp, int is_const, int lineno)
730 struct attr_desc *attr;
731 struct attr_value *av;
732 const char *name_ptr, *p;
735 switch (GET_CODE (exp))
738 /* Handle negation test. */
739 if (XSTR (exp, 1)[0] == '!')
740 return check_attr_test (attr_rtx (NOT,
741 attr_eq (XSTR (exp, 0),
745 else if (n_comma_elts (XSTR (exp, 1)) == 1)
747 attr = find_attr (&XSTR (exp, 0), 0);
750 if (! strcmp (XSTR (exp, 0), "alternative"))
751 return mk_attr_alt (1 << atoi (XSTR (exp, 1)));
753 fatal ("unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
756 if (is_const && ! attr->is_const)
757 fatal ("constant expression uses insn attribute `%s' in EQ_ATTR",
760 /* Copy this just to make it permanent,
761 so expressions using it can be permanent too. */
762 exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
764 /* It shouldn't be possible to simplify the value given to a
765 constant attribute, so don't expand this until it's time to
766 write the test expression. */
768 ATTR_IND_SIMPLIFIED_P (exp) = 1;
770 if (attr->is_numeric)
772 for (p = XSTR (exp, 1); *p; p++)
774 fatal ("attribute `%s' takes only numeric values",
779 for (av = attr->first_value; av; av = av->next)
780 if (GET_CODE (av->value) == CONST_STRING
781 && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
785 fatal ("unknown value `%s' for `%s' attribute",
786 XSTR (exp, 1), XSTR (exp, 0));
791 if (! strcmp (XSTR (exp, 0), "alternative"))
795 name_ptr = XSTR (exp, 1);
796 while ((p = next_comma_elt (&name_ptr)) != NULL)
797 set |= 1 << atoi (p);
799 return mk_attr_alt (set);
803 /* Make an IOR tree of the possible values. */
805 name_ptr = XSTR (exp, 1);
806 while ((p = next_comma_elt (&name_ptr)) != NULL)
808 newexp = attr_eq (XSTR (exp, 0), p);
809 orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
812 return check_attr_test (orexp, is_const, lineno);
821 /* Either TRUE or FALSE. */
829 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
830 XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
834 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
839 fatal ("RTL operator \"%s\" not valid in constant attribute test",
840 GET_RTX_NAME (GET_CODE (exp)));
841 /* These cases can't be simplified. */
842 ATTR_IND_SIMPLIFIED_P (exp) = 1;
845 case LE: case LT: case GT: case GE:
846 case LEU: case LTU: case GTU: case GEU:
848 if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
849 && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
850 exp = attr_rtx (GET_CODE (exp),
851 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
852 attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
853 /* These cases can't be simplified. */
854 ATTR_IND_SIMPLIFIED_P (exp) = 1;
860 /* These cases are valid for constant attributes, but can't be
862 exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
863 ATTR_IND_SIMPLIFIED_P (exp) = 1;
867 fatal ("RTL operator \"%s\" not valid in attribute test",
868 GET_RTX_NAME (GET_CODE (exp)));
874 /* Given an expression, ensure that it is validly formed and that all named
875 attribute values are valid for the given attribute. Issue a fatal error
876 if not. If no attribute is specified, assume a numeric attribute.
878 Return a perhaps modified replacement expression for the value. */
881 check_attr_value (rtx exp, struct attr_desc *attr)
883 struct attr_value *av;
887 switch (GET_CODE (exp))
890 if (attr && ! attr->is_numeric)
892 error_with_line (attr->lineno,
893 "CONST_INT not valid for non-numeric attribute %s",
898 if (INTVAL (exp) < 0)
900 error_with_line (attr->lineno,
901 "negative numeric value specified for attribute %s",
908 if (! strcmp (XSTR (exp, 0), "*"))
911 if (attr == 0 || attr->is_numeric)
917 error_with_line (attr ? attr->lineno : 0,
918 "non-numeric value for numeric attribute %s",
919 attr ? attr->name : "internal");
925 for (av = attr->first_value; av; av = av->next)
926 if (GET_CODE (av->value) == CONST_STRING
927 && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
931 error_with_line (attr->lineno,
932 "unknown value `%s' for `%s' attribute",
933 XSTR (exp, 0), attr ? attr->name : "internal");
937 XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
938 attr ? attr->is_const : 0,
939 attr ? attr->lineno : 0);
940 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
941 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
949 if (attr && !attr->is_numeric)
951 error_with_line (attr->lineno,
952 "invalid operation `%s' for non-numeric"
953 " attribute value", GET_RTX_NAME (GET_CODE (exp)));
960 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
961 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
970 XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
974 if (XVECLEN (exp, 0) % 2 != 0)
976 error_with_line (attr->lineno,
977 "first operand of COND must have even length");
981 for (i = 0; i < XVECLEN (exp, 0); i += 2)
983 XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
984 attr ? attr->is_const : 0,
985 attr ? attr->lineno : 0);
986 XVECEXP (exp, 0, i + 1)
987 = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
990 XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
995 struct attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
997 error_with_line (attr ? attr->lineno : 0,
998 "unknown attribute `%s' in ATTR",
1000 else if (attr && attr->is_const && ! attr2->is_const)
1001 error_with_line (attr->lineno,
1002 "non-constant attribute `%s' referenced from `%s'",
1003 XSTR (exp, 0), attr->name);
1005 && attr->is_numeric != attr2->is_numeric)
1006 error_with_line (attr->lineno,
1007 "numeric attribute mismatch calling `%s' from `%s'",
1008 XSTR (exp, 0), attr->name);
1013 /* A constant SYMBOL_REF is valid as a constant attribute test and
1014 is expanded later by make_canonical into a COND. In a non-constant
1015 attribute test, it is left be. */
1016 return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
1019 error_with_line (attr ? attr->lineno : 0,
1020 "invalid operation `%s' for attribute value",
1021 GET_RTX_NAME (GET_CODE (exp)));
1028 /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
1029 It becomes a COND with each test being (eq_attr "alternative" "n") */
1032 convert_set_attr_alternative (rtx exp, struct insn_def *id)
1034 int num_alt = id->num_alternatives;
1038 if (XVECLEN (exp, 1) != num_alt)
1040 error_with_line (id->lineno,
1041 "bad number of entries in SET_ATTR_ALTERNATIVE");
1045 /* Make a COND with all tests but the last. Select the last value via the
1047 condexp = rtx_alloc (COND);
1048 XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
1050 for (i = 0; i < num_alt - 1; i++)
1053 p = attr_numeral (i);
1055 XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
1056 XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
1059 XEXP (condexp, 1) = XVECEXP (exp, 1, i);
1061 return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
1064 /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
1065 list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
1068 convert_set_attr (rtx exp, struct insn_def *id)
1071 const char *name_ptr;
1075 /* See how many alternative specified. */
1076 n = n_comma_elts (XSTR (exp, 1));
1078 return attr_rtx (SET,
1079 attr_rtx (ATTR, XSTR (exp, 0)),
1080 attr_rtx (CONST_STRING, XSTR (exp, 1)));
1082 newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
1083 XSTR (newexp, 0) = XSTR (exp, 0);
1084 XVEC (newexp, 1) = rtvec_alloc (n);
1086 /* Process each comma-separated name. */
1087 name_ptr = XSTR (exp, 1);
1089 while ((p = next_comma_elt (&name_ptr)) != NULL)
1090 XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
1092 return convert_set_attr_alternative (newexp, id);
1095 /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
1096 and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
1102 struct insn_def *id;
1103 struct attr_desc *attr;
1107 for (id = defs; id; id = id->next)
1109 if (XVEC (id->def, id->vec_idx) == NULL)
1112 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1114 value = XVECEXP (id->def, id->vec_idx, i);
1115 switch (GET_CODE (value))
1118 if (GET_CODE (XEXP (value, 0)) != ATTR)
1120 error_with_line (id->lineno, "bad attribute set");
1125 case SET_ATTR_ALTERNATIVE:
1126 value = convert_set_attr_alternative (value, id);
1130 value = convert_set_attr (value, id);
1134 error_with_line (id->lineno, "invalid attribute code %s",
1135 GET_RTX_NAME (GET_CODE (value)));
1138 if (value == NULL_RTX)
1141 if ((attr = find_attr (&XSTR (XEXP (value, 0), 0), 0)) == NULL)
1143 error_with_line (id->lineno, "unknown attribute %s",
1144 XSTR (XEXP (value, 0), 0));
1148 XVECEXP (id->def, id->vec_idx, i) = value;
1149 XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
1154 /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
1155 expressions by converting them into a COND. This removes cases from this
1156 program. Also, replace an attribute value of "*" with the default attribute
1160 make_canonical (struct attr_desc *attr, rtx exp)
1165 switch (GET_CODE (exp))
1168 exp = make_numeric_value (INTVAL (exp));
1172 if (! strcmp (XSTR (exp, 0), "*"))
1174 if (attr == 0 || attr->default_val == 0)
1175 fatal ("(attr_value \"*\") used in invalid context");
1176 exp = attr->default_val->value;
1179 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1184 if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
1186 /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
1187 This makes the COND something that won't be considered an arbitrary
1188 expression by walk_attr_value. */
1189 ATTR_IND_SIMPLIFIED_P (exp) = 1;
1190 exp = check_attr_value (exp, attr);
1194 newexp = rtx_alloc (COND);
1195 XVEC (newexp, 0) = rtvec_alloc (2);
1196 XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
1197 XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
1199 XEXP (newexp, 1) = XEXP (exp, 2);
1202 /* Fall through to COND case since this is now a COND. */
1209 /* First, check for degenerate COND. */
1210 if (XVECLEN (exp, 0) == 0)
1211 return make_canonical (attr, XEXP (exp, 1));
1212 defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
1214 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1216 XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
1217 XVECEXP (exp, 0, i + 1)
1218 = make_canonical (attr, XVECEXP (exp, 0, i + 1));
1219 if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
1235 copy_boolean (rtx exp)
1237 if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
1238 return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
1239 copy_boolean (XEXP (exp, 1)));
1240 if (GET_CODE (exp) == MATCH_OPERAND)
1242 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1243 XSTR (exp, 2) = DEF_ATTR_STRING (XSTR (exp, 2));
1245 else if (GET_CODE (exp) == EQ_ATTR)
1247 XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
1248 XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
1254 /* Given a value and an attribute description, return a `struct attr_value *'
1255 that represents that value. This is either an existing structure, if the
1256 value has been previously encountered, or a newly-created structure.
1258 `insn_code' is the code of an insn whose attribute has the specified
1259 value (-2 if not processing an insn). We ensure that all insns for
1260 a given value have the same number of alternatives if the value checks
1263 static struct attr_value *
1264 get_attr_value (rtx value, struct attr_desc *attr, int insn_code)
1266 struct attr_value *av;
1269 value = make_canonical (attr, value);
1270 if (compares_alternatives_p (value))
1272 if (insn_code < 0 || insn_alternatives == NULL)
1273 fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
1275 num_alt = insn_alternatives[insn_code];
1278 for (av = attr->first_value; av; av = av->next)
1279 if (rtx_equal_p (value, av->value)
1280 && (num_alt == 0 || av->first_insn == NULL
1281 || insn_alternatives[av->first_insn->def->insn_code]))
1284 av = oballoc (struct attr_value);
1286 av->next = attr->first_value;
1287 attr->first_value = av;
1288 av->first_insn = NULL;
1290 av->has_asm_insn = 0;
1295 /* After all DEFINE_DELAYs have been read in, create internal attributes
1296 to generate the required routines.
1298 First, we compute the number of delay slots for each insn (as a COND of
1299 each of the test expressions in DEFINE_DELAYs). Then, if more than one
1300 delay type is specified, we compute a similar function giving the
1301 DEFINE_DELAY ordinal for each insn.
1303 Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
1304 tells whether a given insn can be in that delay slot.
1306 Normal attribute filling and optimization expands these to contain the
1307 information needed to handle delay slots. */
1310 expand_delays (void)
1312 struct delay_desc *delay;
1318 /* First, generate data for `num_delay_slots' function. */
1320 condexp = rtx_alloc (COND);
1321 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1322 XEXP (condexp, 1) = make_numeric_value (0);
1324 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1326 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1327 XVECEXP (condexp, 0, i + 1)
1328 = make_numeric_value (XVECLEN (delay->def, 1) / 3);
1331 make_internal_attr (num_delay_slots_str, condexp, ATTR_NONE);
1333 /* If more than one delay type, do the same for computing the delay type. */
1336 condexp = rtx_alloc (COND);
1337 XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
1338 XEXP (condexp, 1) = make_numeric_value (0);
1340 for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
1342 XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
1343 XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
1346 make_internal_attr (delay_type_str, condexp, ATTR_SPECIAL);
1349 /* For each delay possibility and delay slot, compute an eligibility
1350 attribute for non-annulled insns and for each type of annulled (annul
1351 if true and annul if false). */
1352 for (delay = delays; delay; delay = delay->next)
1354 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
1356 condexp = XVECEXP (delay->def, 1, i);
1358 condexp = false_rtx;
1359 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1360 make_numeric_value (1), make_numeric_value (0));
1362 p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
1363 "*delay_%d_%d", delay->num, i / 3);
1364 make_internal_attr (p, newexp, ATTR_SPECIAL);
1366 if (have_annul_true)
1368 condexp = XVECEXP (delay->def, 1, i + 1);
1369 if (condexp == 0) condexp = false_rtx;
1370 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1371 make_numeric_value (1),
1372 make_numeric_value (0));
1373 p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
1374 "*annul_true_%d_%d", delay->num, i / 3);
1375 make_internal_attr (p, newexp, ATTR_SPECIAL);
1378 if (have_annul_false)
1380 condexp = XVECEXP (delay->def, 1, i + 2);
1381 if (condexp == 0) condexp = false_rtx;
1382 newexp = attr_rtx (IF_THEN_ELSE, condexp,
1383 make_numeric_value (1),
1384 make_numeric_value (0));
1385 p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
1386 "*annul_false_%d_%d", delay->num, i / 3);
1387 make_internal_attr (p, newexp, ATTR_SPECIAL);
1393 /* Once all attributes and insns have been read and checked, we construct for
1394 each attribute value a list of all the insns that have that value for
1398 fill_attr (struct attr_desc *attr)
1400 struct attr_value *av;
1401 struct insn_ent *ie;
1402 struct insn_def *id;
1406 /* Don't fill constant attributes. The value is independent of
1407 any particular insn. */
1411 for (id = defs; id; id = id->next)
1413 /* If no value is specified for this insn for this attribute, use the
1416 if (XVEC (id->def, id->vec_idx))
1417 for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
1418 if (! strcmp_check (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
1420 value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
1423 av = attr->default_val;
1425 av = get_attr_value (value, attr, id->insn_code);
1427 ie = oballoc (struct insn_ent);
1429 insert_insn_ent (av, ie);
1433 /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
1434 test that checks relative positions of insns (uses MATCH_DUP or PC).
1435 If so, replace it with what is obtained by passing the expression to
1436 ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
1437 recursively on each value (including the default value). Otherwise,
1438 return the value returned by NO_ADDRESS_FN applied to EXP. */
1441 substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
1442 rtx (*address_fn) (rtx))
1447 if (GET_CODE (exp) == COND)
1449 /* See if any tests use addresses. */
1451 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1452 walk_attr_value (XVECEXP (exp, 0, i));
1455 return (*address_fn) (exp);
1457 /* Make a new copy of this COND, replacing each element. */
1458 newexp = rtx_alloc (COND);
1459 XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
1460 for (i = 0; i < XVECLEN (exp, 0); i += 2)
1462 XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
1463 XVECEXP (newexp, 0, i + 1)
1464 = substitute_address (XVECEXP (exp, 0, i + 1),
1465 no_address_fn, address_fn);
1468 XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
1469 no_address_fn, address_fn);
1474 else if (GET_CODE (exp) == IF_THEN_ELSE)
1477 walk_attr_value (XEXP (exp, 0));
1479 return (*address_fn) (exp);
1481 return attr_rtx (IF_THEN_ELSE,
1482 substitute_address (XEXP (exp, 0),
1483 no_address_fn, address_fn),
1484 substitute_address (XEXP (exp, 1),
1485 no_address_fn, address_fn),
1486 substitute_address (XEXP (exp, 2),
1487 no_address_fn, address_fn));
1490 return (*no_address_fn) (exp);
1493 /* Make new attributes from the `length' attribute. The following are made,
1494 each corresponding to a function called from `shorten_branches' or
1497 *insn_default_length This is the length of the insn to be returned
1498 by `get_attr_length' before `shorten_branches'
1499 has been called. In each case where the length
1500 depends on relative addresses, the largest
1501 possible is used. This routine is also used
1502 to compute the initial size of the insn.
1504 *insn_variable_length_p This returns 1 if the insn's length depends
1505 on relative addresses, zero otherwise.
1507 *insn_current_length This is only called when it is known that the
1508 insn has a variable length and returns the
1509 current length, based on relative addresses.
1513 make_length_attrs (void)
1515 static const char *new_names[] =
1517 "*insn_default_length",
1519 "*insn_variable_length_p",
1520 "*insn_current_length"
1522 static rtx (*const no_address_fn[]) (rtx)
1523 = {identity_fn,identity_fn, zero_fn, zero_fn};
1524 static rtx (*const address_fn[]) (rtx)
1525 = {max_fn, min_fn, one_fn, identity_fn};
1527 struct attr_desc *length_attr, *new_attr;
1528 struct attr_value *av, *new_av;
1529 struct insn_ent *ie, *new_ie;
1531 /* See if length attribute is defined. If so, it must be numeric. Make
1532 it special so we don't output anything for it. */
1533 length_attr = find_attr (&length_str, 0);
1534 if (length_attr == 0)
1537 if (! length_attr->is_numeric)
1538 fatal ("length attribute must be numeric");
1540 length_attr->is_const = 0;
1541 length_attr->is_special = 1;
1543 /* Make each new attribute, in turn. */
1544 for (i = 0; i < ARRAY_SIZE (new_names); i++)
1546 make_internal_attr (new_names[i],
1547 substitute_address (length_attr->default_val->value,
1548 no_address_fn[i], address_fn[i]),
1550 new_attr = find_attr (&new_names[i], 0);
1551 for (av = length_attr->first_value; av; av = av->next)
1552 for (ie = av->first_insn; ie; ie = ie->next)
1554 new_av = get_attr_value (substitute_address (av->value,
1557 new_attr, ie->def->insn_code);
1558 new_ie = oballoc (struct insn_ent);
1559 new_ie->def = ie->def;
1560 insert_insn_ent (new_av, new_ie);
1565 /* Utility functions called from above routine. */
1568 identity_fn (rtx exp)
1574 zero_fn (rtx exp ATTRIBUTE_UNUSED)
1576 return make_numeric_value (0);
1580 one_fn (rtx exp ATTRIBUTE_UNUSED)
1582 return make_numeric_value (1);
1589 return make_numeric_value (max_attr_value (exp, &unknown));
1596 return make_numeric_value (min_attr_value (exp, &unknown));
1600 write_length_unit_log (void)
1602 struct attr_desc *length_attr = find_attr (&length_str, 0);
1603 struct attr_value *av;
1604 struct insn_ent *ie;
1605 unsigned int length_unit_log, length_or;
1608 if (length_attr == 0)
1610 length_or = or_attr_value (length_attr->default_val->value, &unknown);
1611 for (av = length_attr->first_value; av; av = av->next)
1612 for (ie = av->first_insn; ie; ie = ie->next)
1613 length_or |= or_attr_value (av->value, &unknown);
1616 length_unit_log = 0;
1619 length_or = ~length_or;
1620 for (length_unit_log = 0; length_or & 1; length_or >>= 1)
1623 printf ("EXPORTED_CONST int length_unit_log = %u;\n", length_unit_log);
1626 /* Take a COND expression and see if any of the conditions in it can be
1627 simplified. If any are known true or known false for the particular insn
1628 code, the COND can be further simplified.
1630 Also call ourselves on any COND operations that are values of this COND.
1632 We do not modify EXP; rather, we make and return a new rtx. */
1635 simplify_cond (rtx exp, int insn_code, int insn_index)
1638 /* We store the desired contents here,
1639 then build a new expression if they don't match EXP. */
1640 rtx defval = XEXP (exp, 1);
1641 rtx new_defval = XEXP (exp, 1);
1642 int len = XVECLEN (exp, 0);
1643 rtx *tests = XNEWVEC (rtx, len);
1647 /* This lets us free all storage allocated below, if appropriate. */
1648 obstack_finish (rtl_obstack);
1650 memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
1652 /* See if default value needs simplification. */
1653 if (GET_CODE (defval) == COND)
1654 new_defval = simplify_cond (defval, insn_code, insn_index);
1656 /* Simplify the subexpressions, and see what tests we can get rid of. */
1658 for (i = 0; i < len; i += 2)
1660 rtx newtest, newval;
1662 /* Simplify this test. */
1663 newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
1666 newval = tests[i + 1];
1667 /* See if this value may need simplification. */
1668 if (GET_CODE (newval) == COND)
1669 newval = simplify_cond (newval, insn_code, insn_index);
1671 /* Look for ways to delete or combine this test. */
1672 if (newtest == true_rtx)
1674 /* If test is true, make this value the default
1675 and discard this + any following tests. */
1677 defval = tests[i + 1];
1678 new_defval = newval;
1681 else if (newtest == false_rtx)
1683 /* If test is false, discard it and its value. */
1684 for (j = i; j < len - 2; j++)
1685 tests[j] = tests[j + 2];
1690 else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
1692 /* If this value and the value for the prev test are the same,
1696 = insert_right_side (IOR, tests[i - 2], newtest,
1697 insn_code, insn_index);
1699 /* Delete this test/value. */
1700 for (j = i; j < len - 2; j++)
1701 tests[j] = tests[j + 2];
1707 tests[i + 1] = newval;
1710 /* If the last test in a COND has the same value
1711 as the default value, that test isn't needed. */
1713 while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
1716 /* See if we changed anything. */
1717 if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
1720 for (i = 0; i < len; i++)
1721 if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
1729 if (GET_CODE (defval) == COND)
1730 ret = simplify_cond (defval, insn_code, insn_index);
1738 rtx newexp = rtx_alloc (COND);
1740 XVEC (newexp, 0) = rtvec_alloc (len);
1741 memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
1742 XEXP (newexp, 1) = new_defval;
1749 /* Remove an insn entry from an attribute value. */
1752 remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
1754 struct insn_ent *previe;
1756 if (av->first_insn == ie)
1757 av->first_insn = ie->next;
1760 for (previe = av->first_insn; previe->next != ie; previe = previe->next)
1762 previe->next = ie->next;
1766 if (ie->def->insn_code == -1)
1767 av->has_asm_insn = 0;
1772 /* Insert an insn entry in an attribute value list. */
1775 insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
1777 ie->next = av->first_insn;
1778 av->first_insn = ie;
1780 if (ie->def->insn_code == -1)
1781 av->has_asm_insn = 1;
1786 /* This is a utility routine to take an expression that is a tree of either
1787 AND or IOR expressions and insert a new term. The new term will be
1788 inserted at the right side of the first node whose code does not match
1789 the root. A new node will be created with the root's code. Its left
1790 side will be the old right side and its right side will be the new
1793 If the `term' is itself a tree, all its leaves will be inserted. */
1796 insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
1800 /* Avoid consing in some special cases. */
1801 if (code == AND && term == true_rtx)
1803 if (code == AND && term == false_rtx)
1805 if (code == AND && exp == true_rtx)
1807 if (code == AND && exp == false_rtx)
1809 if (code == IOR && term == true_rtx)
1811 if (code == IOR && term == false_rtx)
1813 if (code == IOR && exp == true_rtx)
1815 if (code == IOR && exp == false_rtx)
1817 if (attr_equal_p (exp, term))
1820 if (GET_CODE (term) == code)
1822 exp = insert_right_side (code, exp, XEXP (term, 0),
1823 insn_code, insn_index);
1824 exp = insert_right_side (code, exp, XEXP (term, 1),
1825 insn_code, insn_index);
1830 if (GET_CODE (exp) == code)
1832 rtx new_rtx = insert_right_side (code, XEXP (exp, 1),
1833 term, insn_code, insn_index);
1834 if (new_rtx != XEXP (exp, 1))
1835 /* Make a copy of this expression and call recursively. */
1836 newexp = attr_rtx (code, XEXP (exp, 0), new_rtx);
1842 /* Insert the new term. */
1843 newexp = attr_rtx (code, exp, term);
1846 return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
1849 /* If we have an expression which AND's a bunch of
1850 (not (eq_attrq "alternative" "n"))
1851 terms, we may have covered all or all but one of the possible alternatives.
1852 If so, we can optimize. Similarly for IOR's of EQ_ATTR.
1854 This routine is passed an expression and either AND or IOR. It returns a
1855 bitmask indicating which alternatives are mentioned within EXP. */
1858 compute_alternative_mask (rtx exp, enum rtx_code code)
1861 if (GET_CODE (exp) == code)
1862 return compute_alternative_mask (XEXP (exp, 0), code)
1863 | compute_alternative_mask (XEXP (exp, 1), code);
1865 else if (code == AND && GET_CODE (exp) == NOT
1866 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
1867 && XSTR (XEXP (exp, 0), 0) == alternative_name)
1868 string = XSTR (XEXP (exp, 0), 1);
1870 else if (code == IOR && GET_CODE (exp) == EQ_ATTR
1871 && XSTR (exp, 0) == alternative_name)
1872 string = XSTR (exp, 1);
1874 else if (GET_CODE (exp) == EQ_ATTR_ALT)
1876 if (code == AND && XINT (exp, 1))
1877 return XINT (exp, 0);
1879 if (code == IOR && !XINT (exp, 1))
1880 return XINT (exp, 0);
1888 return 1 << (string[0] - '0');
1889 return 1 << atoi (string);
1892 /* Given I, a single-bit mask, return RTX to compare the `alternative'
1893 attribute with the value represented by that bit. */
1896 make_alternative_compare (int mask)
1898 return mk_attr_alt (mask);
1901 /* If we are processing an (eq_attr "attr" "value") test, we find the value
1902 of "attr" for this insn code. From that value, we can compute a test
1903 showing when the EQ_ATTR will be true. This routine performs that
1904 computation. If a test condition involves an address, we leave the EQ_ATTR
1905 intact because addresses are only valid for the `length' attribute.
1907 EXP is the EQ_ATTR expression and ATTR is the attribute to which
1908 it refers. VALUE is the value of that attribute for the insn
1909 corresponding to INSN_CODE and INSN_INDEX. */
1912 evaluate_eq_attr (rtx exp, struct attr_desc *attr, rtx value,
1913 int insn_code, int insn_index)
1920 while (GET_CODE (value) == ATTR)
1922 struct attr_value *av = NULL;
1924 attr = find_attr (&XSTR (value, 0), 0);
1926 if (insn_code_values)
1928 struct attr_value_list *iv;
1929 for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
1930 if (iv->attr == attr)
1938 struct insn_ent *ie;
1939 for (av = attr->first_value; av; av = av->next)
1940 for (ie = av->first_insn; ie; ie = ie->next)
1941 if (ie->def->insn_code == insn_code)
1951 switch (GET_CODE (value))
1954 if (! strcmp_check (XSTR (value, 0), XSTR (exp, 1)))
1965 gcc_assert (GET_CODE (exp) == EQ_ATTR);
1966 prefix = attr->enum_name ? attr->enum_name : attr->name;
1967 string = ACONCAT ((prefix, "_", XSTR (exp, 1), NULL));
1968 for (p = string; *p; p++)
1971 newexp = attr_rtx (EQ, value,
1972 attr_rtx (SYMBOL_REF,
1973 DEF_ATTR_STRING (string)));
1978 /* We construct an IOR of all the cases for which the
1979 requested attribute value is present. Since we start with
1980 FALSE, if it is not present, FALSE will be returned.
1982 Each case is the AND of the NOT's of the previous conditions with the
1983 current condition; in the default case the current condition is TRUE.
1985 For each possible COND value, call ourselves recursively.
1987 The extra TRUE and FALSE expressions will be eliminated by another
1988 call to the simplification routine. */
1993 for (i = 0; i < XVECLEN (value, 0); i += 2)
1995 rtx this_cond = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
1996 insn_code, insn_index);
1998 right = insert_right_side (AND, andexp, this_cond,
1999 insn_code, insn_index);
2000 right = insert_right_side (AND, right,
2001 evaluate_eq_attr (exp, attr,
2004 insn_code, insn_index),
2005 insn_code, insn_index);
2006 orexp = insert_right_side (IOR, orexp, right,
2007 insn_code, insn_index);
2009 /* Add this condition into the AND expression. */
2010 newexp = attr_rtx (NOT, this_cond);
2011 andexp = insert_right_side (AND, andexp, newexp,
2012 insn_code, insn_index);
2015 /* Handle the default case. */
2016 right = insert_right_side (AND, andexp,
2017 evaluate_eq_attr (exp, attr, XEXP (value, 1),
2018 insn_code, insn_index),
2019 insn_code, insn_index);
2020 newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
2027 /* If uses an address, must return original expression. But set the
2028 ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again. */
2031 walk_attr_value (newexp);
2035 if (! ATTR_IND_SIMPLIFIED_P (exp))
2036 return copy_rtx_unchanging (exp);
2043 /* This routine is called when an AND of a term with a tree of AND's is
2044 encountered. If the term or its complement is present in the tree, it
2045 can be replaced with TRUE or FALSE, respectively.
2047 Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
2048 be true and hence are complementary.
2050 There is one special case: If we see
2051 (and (not (eq_attr "att" "v1"))
2052 (eq_attr "att" "v2"))
2053 this can be replaced by (eq_attr "att" "v2"). To do this we need to
2054 replace the term, not anything in the AND tree. So we pass a pointer to
2058 simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2063 int left_eliminates_term, right_eliminates_term;
2065 if (GET_CODE (exp) == AND)
2067 left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2068 right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2069 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2071 newexp = attr_rtx (AND, left, right);
2073 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2077 else if (GET_CODE (exp) == IOR)
2079 /* For the IOR case, we do the same as above, except that we can
2080 only eliminate `term' if both sides of the IOR would do so. */
2082 left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2083 left_eliminates_term = (temp == true_rtx);
2086 right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2087 right_eliminates_term = (temp == true_rtx);
2089 if (left_eliminates_term && right_eliminates_term)
2092 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2094 newexp = attr_rtx (IOR, left, right);
2096 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2100 /* Check for simplifications. Do some extra checking here since this
2101 routine is called so many times. */
2106 else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
2109 else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
2112 else if (GET_CODE (exp) == EQ_ATTR_ALT && GET_CODE (*pterm) == EQ_ATTR_ALT)
2114 if (attr_alt_subset_p (*pterm, exp))
2117 if (attr_alt_subset_of_compl_p (*pterm, exp))
2120 if (attr_alt_subset_p (exp, *pterm))
2126 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
2128 if (XSTR (exp, 0) != XSTR (*pterm, 0))
2131 if (! strcmp_check (XSTR (exp, 1), XSTR (*pterm, 1)))
2137 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2138 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
2140 if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
2143 if (! strcmp_check (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
2149 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2150 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
2152 if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
2155 if (! strcmp_check (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
2161 else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
2163 if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
2167 else if (GET_CODE (exp) == NOT)
2169 if (attr_equal_p (XEXP (exp, 0), *pterm))
2173 else if (GET_CODE (*pterm) == NOT)
2175 if (attr_equal_p (XEXP (*pterm, 0), exp))
2179 else if (attr_equal_p (exp, *pterm))
2185 /* Similar to `simplify_and_tree', but for IOR trees. */
2188 simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
2193 int left_eliminates_term, right_eliminates_term;
2195 if (GET_CODE (exp) == IOR)
2197 left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
2198 right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
2199 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2201 newexp = attr_rtx (GET_CODE (exp), left, right);
2203 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2207 else if (GET_CODE (exp) == AND)
2209 /* For the AND case, we do the same as above, except that we can
2210 only eliminate `term' if both sides of the AND would do so. */
2212 left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
2213 left_eliminates_term = (temp == false_rtx);
2216 right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
2217 right_eliminates_term = (temp == false_rtx);
2219 if (left_eliminates_term && right_eliminates_term)
2222 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2224 newexp = attr_rtx (GET_CODE (exp), left, right);
2226 exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
2230 if (attr_equal_p (exp, *pterm))
2233 else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
2236 else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
2239 else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
2240 && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
2241 && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
2244 else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
2245 && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
2246 && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
2252 /* Compute approximate cost of the expression. Used to decide whether
2253 expression is cheap enough for inline. */
2255 attr_rtx_cost (rtx x)
2261 code = GET_CODE (x);
2274 /* Alternatives don't result into function call. */
2275 if (!strcmp_check (XSTR (x, 0), alternative_name))
2282 const char *fmt = GET_RTX_FORMAT (code);
2283 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2289 for (j = 0; j < XVECLEN (x, i); j++)
2290 cost += attr_rtx_cost (XVECEXP (x, i, j));
2293 cost += attr_rtx_cost (XEXP (x, i));
2303 /* Simplify test expression and use temporary obstack in order to avoid
2304 memory bloat. Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
2305 and avoid unnecessary copying if possible. */
2308 simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
2311 struct obstack *old;
2312 if (ATTR_IND_SIMPLIFIED_P (exp))
2315 rtl_obstack = temp_obstack;
2316 x = simplify_test_exp (exp, insn_code, insn_index);
2318 if (x == exp || rtl_obstack == temp_obstack)
2320 return attr_copy_rtx (x);
2323 /* Returns true if S1 is a subset of S2. */
2326 attr_alt_subset_p (rtx s1, rtx s2)
2328 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2331 return !(XINT (s1, 0) &~ XINT (s2, 0));
2334 return !(XINT (s1, 0) & XINT (s2, 0));
2340 return !(XINT (s2, 0) &~ XINT (s1, 0));
2347 /* Returns true if S1 is a subset of complement of S2. */
2350 attr_alt_subset_of_compl_p (rtx s1, rtx s2)
2352 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2355 return !(XINT (s1, 0) & XINT (s2, 0));
2358 return !(XINT (s1, 0) & ~XINT (s2, 0));
2361 return !(XINT (s2, 0) &~ XINT (s1, 0));
2371 /* Return EQ_ATTR_ALT expression representing intersection of S1 and S2. */
2374 attr_alt_intersection (rtx s1, rtx s2)
2376 rtx result = rtx_alloc (EQ_ATTR_ALT);
2378 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2381 XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
2384 XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
2387 XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
2390 XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
2395 XINT (result, 1) = XINT (s1, 1) & XINT (s2, 1);
2400 /* Return EQ_ATTR_ALT expression representing union of S1 and S2. */
2403 attr_alt_union (rtx s1, rtx s2)
2405 rtx result = rtx_alloc (EQ_ATTR_ALT);
2407 switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
2410 XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
2413 XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
2416 XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
2419 XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
2425 XINT (result, 1) = XINT (s1, 1) | XINT (s2, 1);
2429 /* Return EQ_ATTR_ALT expression representing complement of S. */
2432 attr_alt_complement (rtx s)
2434 rtx result = rtx_alloc (EQ_ATTR_ALT);
2436 XINT (result, 0) = XINT (s, 0);
2437 XINT (result, 1) = 1 - XINT (s, 1);
2442 /* Return EQ_ATTR_ALT expression representing set containing elements set
2448 rtx result = rtx_alloc (EQ_ATTR_ALT);
2450 XINT (result, 0) = e;
2451 XINT (result, 1) = 0;
2456 /* Given an expression, see if it can be simplified for a particular insn
2457 code based on the values of other attributes being tested. This can
2458 eliminate nested get_attr_... calls.
2460 Note that if an endless recursion is specified in the patterns, the
2461 optimization will loop. However, it will do so in precisely the cases where
2462 an infinite recursion loop could occur during compilation. It's better that
2466 simplify_test_exp (rtx exp, int insn_code, int insn_index)
2469 struct attr_desc *attr;
2470 struct attr_value *av;
2471 struct insn_ent *ie;
2472 struct attr_value_list *iv;
2475 bool left_alt, right_alt;
2477 /* Don't re-simplify something we already simplified. */
2478 if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
2481 switch (GET_CODE (exp))
2484 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2485 if (left == false_rtx)
2487 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2488 if (right == false_rtx)
2491 if (GET_CODE (left) == EQ_ATTR_ALT
2492 && GET_CODE (right) == EQ_ATTR_ALT)
2494 exp = attr_alt_intersection (left, right);
2495 return simplify_test_exp (exp, insn_code, insn_index);
2498 /* If either side is an IOR and we have (eq_attr "alternative" ..")
2499 present on both sides, apply the distributive law since this will
2500 yield simplifications. */
2501 if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
2502 && compute_alternative_mask (left, IOR)
2503 && compute_alternative_mask (right, IOR))
2505 if (GET_CODE (left) == IOR)
2512 newexp = attr_rtx (IOR,
2513 attr_rtx (AND, left, XEXP (right, 0)),
2514 attr_rtx (AND, left, XEXP (right, 1)));
2516 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2519 /* Try with the term on both sides. */
2520 right = simplify_and_tree (right, &left, insn_code, insn_index);
2521 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2522 left = simplify_and_tree (left, &right, insn_code, insn_index);
2524 if (left == false_rtx || right == false_rtx)
2526 else if (left == true_rtx)
2530 else if (right == true_rtx)
2534 /* See if all or all but one of the insn's alternatives are specified
2535 in this tree. Optimize if so. */
2537 if (GET_CODE (left) == NOT)
2538 left_alt = (GET_CODE (XEXP (left, 0)) == EQ_ATTR
2539 && XSTR (XEXP (left, 0), 0) == alternative_name);
2541 left_alt = (GET_CODE (left) == EQ_ATTR_ALT
2544 if (GET_CODE (right) == NOT)
2545 right_alt = (GET_CODE (XEXP (right, 0)) == EQ_ATTR
2546 && XSTR (XEXP (right, 0), 0) == alternative_name);
2548 right_alt = (GET_CODE (right) == EQ_ATTR_ALT
2549 && XINT (right, 1));
2552 && (GET_CODE (left) == AND
2554 || GET_CODE (right) == AND
2557 i = compute_alternative_mask (exp, AND);
2558 if (i & ~insn_alternatives[insn_code])
2559 fatal ("invalid alternative specified for pattern number %d",
2562 /* If all alternatives are excluded, this is false. */
2563 i ^= insn_alternatives[insn_code];
2566 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2568 /* If just one excluded, AND a comparison with that one to the
2569 front of the tree. The others will be eliminated by
2570 optimization. We do not want to do this if the insn has one
2571 alternative and we have tested none of them! */
2572 left = make_alternative_compare (i);
2573 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2574 newexp = attr_rtx (AND, left, right);
2576 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2580 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2582 newexp = attr_rtx (AND, left, right);
2583 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2588 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2589 if (left == true_rtx)
2591 right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
2592 if (right == true_rtx)
2595 if (GET_CODE (left) == EQ_ATTR_ALT
2596 && GET_CODE (right) == EQ_ATTR_ALT)
2598 exp = attr_alt_union (left, right);
2599 return simplify_test_exp (exp, insn_code, insn_index);
2602 right = simplify_or_tree (right, &left, insn_code, insn_index);
2603 if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
2604 left = simplify_or_tree (left, &right, insn_code, insn_index);
2606 if (right == true_rtx || left == true_rtx)
2608 else if (left == false_rtx)
2612 else if (right == false_rtx)
2617 /* Test for simple cases where the distributive law is useful. I.e.,
2618 convert (ior (and (x) (y))
2624 else if (GET_CODE (left) == AND && GET_CODE (right) == AND
2625 && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
2627 newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
2629 left = XEXP (left, 0);
2631 newexp = attr_rtx (AND, left, right);
2632 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2635 /* See if all or all but one of the insn's alternatives are specified
2636 in this tree. Optimize if so. */
2638 else if (insn_code >= 0
2639 && (GET_CODE (left) == IOR
2640 || (GET_CODE (left) == EQ_ATTR_ALT
2642 || (GET_CODE (left) == EQ_ATTR
2643 && XSTR (left, 0) == alternative_name)
2644 || GET_CODE (right) == IOR
2645 || (GET_CODE (right) == EQ_ATTR_ALT
2646 && !XINT (right, 1))
2647 || (GET_CODE (right) == EQ_ATTR
2648 && XSTR (right, 0) == alternative_name)))
2650 i = compute_alternative_mask (exp, IOR);
2651 if (i & ~insn_alternatives[insn_code])
2652 fatal ("invalid alternative specified for pattern number %d",
2655 /* If all alternatives are included, this is true. */
2656 i ^= insn_alternatives[insn_code];
2659 else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
2661 /* If just one excluded, IOR a comparison with that one to the
2662 front of the tree. The others will be eliminated by
2663 optimization. We do not want to do this if the insn has one
2664 alternative and we have tested none of them! */
2665 left = make_alternative_compare (i);
2666 right = simplify_and_tree (exp, &left, insn_code, insn_index);
2667 newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
2669 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2673 if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
2675 newexp = attr_rtx (IOR, left, right);
2676 return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2681 if (GET_CODE (XEXP (exp, 0)) == NOT)
2683 left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
2684 insn_code, insn_index);
2688 left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
2689 if (GET_CODE (left) == NOT)
2690 return XEXP (left, 0);
2692 if (left == false_rtx)
2694 if (left == true_rtx)
2697 if (GET_CODE (left) == EQ_ATTR_ALT)
2699 exp = attr_alt_complement (left);
2700 return simplify_test_exp (exp, insn_code, insn_index);
2703 /* Try to apply De`Morgan's laws. */
2704 if (GET_CODE (left) == IOR)
2706 newexp = attr_rtx (AND,
2707 attr_rtx (NOT, XEXP (left, 0)),
2708 attr_rtx (NOT, XEXP (left, 1)));
2710 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2712 else if (GET_CODE (left) == AND)
2714 newexp = attr_rtx (IOR,
2715 attr_rtx (NOT, XEXP (left, 0)),
2716 attr_rtx (NOT, XEXP (left, 1)));
2718 newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
2720 else if (left != XEXP (exp, 0))
2722 newexp = attr_rtx (NOT, left);
2728 return XINT (exp, 1) ? true_rtx : false_rtx;
2732 if (XSTR (exp, 0) == alternative_name)
2734 newexp = mk_attr_alt (1 << atoi (XSTR (exp, 1)));
2738 /* Look at the value for this insn code in the specified attribute.
2739 We normally can replace this comparison with the condition that
2740 would give this insn the values being tested for. */
2742 && (attr = find_attr (&XSTR (exp, 0), 0)) != NULL)
2747 if (insn_code_values)
2749 for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
2750 if (iv->attr == attr)
2758 for (av = attr->first_value; av; av = av->next)
2759 for (ie = av->first_insn; ie; ie = ie->next)
2760 if (ie->def->insn_code == insn_code)
2767 x = evaluate_eq_attr (exp, attr, av->value,
2768 insn_code, insn_index);
2769 x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
2770 if (attr_rtx_cost(x) < 20)
2780 /* We have already simplified this expression. Simplifying it again
2781 won't buy anything unless we weren't given a valid insn code
2782 to process (i.e., we are canonicalizing something.). */
2784 && ! ATTR_IND_SIMPLIFIED_P (newexp))
2785 return copy_rtx_unchanging (newexp);
2790 /* Optimize the attribute lists by seeing if we can determine conditional
2791 values from the known values of other attributes. This will save subroutine
2792 calls during the compilation. */
2795 optimize_attrs (void)
2797 struct attr_desc *attr;
2798 struct attr_value *av;
2799 struct insn_ent *ie;
2802 struct attr_value_list *ivbuf;
2803 struct attr_value_list *iv;
2805 /* For each insn code, make a list of all the insn_ent's for it,
2806 for all values for all attributes. */
2808 if (num_insn_ents == 0)
2811 /* Make 2 extra elements, for "code" values -2 and -1. */
2812 insn_code_values = XCNEWVEC (struct attr_value_list *, insn_code_number + 2);
2814 /* Offset the table address so we can index by -2 or -1. */
2815 insn_code_values += 2;
2817 iv = ivbuf = XNEWVEC (struct attr_value_list, num_insn_ents);
2819 for (i = 0; i < MAX_ATTRS_INDEX; i++)
2820 for (attr = attrs[i]; attr; attr = attr->next)
2821 for (av = attr->first_value; av; av = av->next)
2822 for (ie = av->first_insn; ie; ie = ie->next)
2827 iv->next = insn_code_values[ie->def->insn_code];
2828 insn_code_values[ie->def->insn_code] = iv;
2832 /* Sanity check on num_insn_ents. */
2833 gcc_assert (iv == ivbuf + num_insn_ents);
2835 /* Process one insn code at a time. */
2836 for (i = -2; i < insn_code_number; i++)
2838 /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
2839 We use it to mean "already simplified for this insn". */
2840 for (iv = insn_code_values[i]; iv; iv = iv->next)
2841 clear_struct_flag (iv->av->value);
2843 for (iv = insn_code_values[i]; iv; iv = iv->next)
2845 struct obstack *old = rtl_obstack;
2850 if (GET_CODE (av->value) != COND)
2853 rtl_obstack = temp_obstack;
2855 while (GET_CODE (newexp) == COND)
2857 rtx newexp2 = simplify_cond (newexp, ie->def->insn_code,
2858 ie->def->insn_index);
2859 if (newexp2 == newexp)
2865 if (newexp != av->value)
2867 newexp = attr_copy_rtx (newexp);
2868 remove_insn_ent (av, ie);
2869 av = get_attr_value (newexp, attr, ie->def->insn_code);
2871 insert_insn_ent (av, ie);
2877 free (insn_code_values - 2);
2878 insn_code_values = NULL;
2881 /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions. */
2884 clear_struct_flag (rtx x)
2891 ATTR_CURR_SIMPLIFIED_P (x) = 0;
2892 if (ATTR_IND_SIMPLIFIED_P (x))
2895 code = GET_CODE (x);
2915 /* Compare the elements. If any pair of corresponding elements
2916 fail to match, return 0 for the whole things. */
2918 fmt = GET_RTX_FORMAT (code);
2919 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2925 for (j = 0; j < XVECLEN (x, i); j++)
2926 clear_struct_flag (XVECEXP (x, i, j));
2930 clear_struct_flag (XEXP (x, i));
2936 /* Add attribute value NAME to the beginning of ATTR's list. */
2939 add_attr_value (struct attr_desc *attr, const char *name)
2941 struct attr_value *av;
2943 av = oballoc (struct attr_value);
2944 av->value = attr_rtx (CONST_STRING, name);
2945 av->next = attr->first_value;
2946 attr->first_value = av;
2947 av->first_insn = NULL;
2949 av->has_asm_insn = 0;
2952 /* Create table entries for DEFINE_ATTR or DEFINE_ENUM_ATTR. */
2955 gen_attr (rtx exp, int lineno)
2957 struct enum_type *et;
2958 struct enum_value *ev;
2959 struct attr_desc *attr;
2960 const char *name_ptr;
2963 /* Make a new attribute structure. Check for duplicate by looking at
2964 attr->default_val, since it is initialized by this routine. */
2965 attr = find_attr (&XSTR (exp, 0), 1);
2966 if (attr->default_val)
2968 error_with_line (lineno, "duplicate definition for attribute %s",
2970 message_with_line (attr->lineno, "previous definition");
2973 attr->lineno = lineno;
2975 if (GET_CODE (exp) == DEFINE_ENUM_ATTR)
2977 attr->enum_name = XSTR (exp, 1);
2978 et = lookup_enum_type (XSTR (exp, 1));
2979 if (!et || !et->md_p)
2980 error_with_line (lineno, "No define_enum called `%s' defined",
2982 for (ev = et->values; ev; ev = ev->next)
2983 add_attr_value (attr, ev->name);
2985 else if (*XSTR (exp, 1) == '\0')
2986 attr->is_numeric = 1;
2989 name_ptr = XSTR (exp, 1);
2990 while ((p = next_comma_elt (&name_ptr)) != NULL)
2991 add_attr_value (attr, p);
2994 if (GET_CODE (XEXP (exp, 2)) == CONST)
2997 if (attr->is_numeric)
2998 error_with_line (lineno,
2999 "constant attributes may not take numeric values");
3001 /* Get rid of the CONST node. It is allowed only at top-level. */
3002 XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
3005 if (! strcmp_check (attr->name, length_str) && ! attr->is_numeric)
3006 error_with_line (lineno, "`length' attribute must take numeric values");
3008 /* Set up the default value. */
3009 XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
3010 attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
3013 /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
3014 alternatives in the constraints. Assume all MATCH_OPERANDs have the same
3015 number of alternatives as this should be checked elsewhere. */
3018 count_alternatives (rtx exp)
3023 if (GET_CODE (exp) == MATCH_OPERAND)
3024 return n_comma_elts (XSTR (exp, 2));
3026 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3027 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3032 n = count_alternatives (XEXP (exp, i));
3039 if (XVEC (exp, i) != NULL)
3040 for (j = 0; j < XVECLEN (exp, i); j++)
3042 n = count_alternatives (XVECEXP (exp, i, j));
3051 /* Returns nonzero if the given expression contains an EQ_ATTR with the
3052 `alternative' attribute. */
3055 compares_alternatives_p (rtx exp)
3060 if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
3063 for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
3064 i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
3069 if (compares_alternatives_p (XEXP (exp, i)))
3074 for (j = 0; j < XVECLEN (exp, i); j++)
3075 if (compares_alternatives_p (XVECEXP (exp, i, j)))
3083 /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
3086 gen_insn (rtx exp, int lineno)
3088 struct insn_def *id;
3090 id = oballoc (struct insn_def);
3094 id->lineno = lineno;
3096 switch (GET_CODE (exp))
3099 id->insn_code = insn_code_number;
3100 id->insn_index = insn_index_number;
3101 id->num_alternatives = count_alternatives (exp);
3102 if (id->num_alternatives == 0)
3103 id->num_alternatives = 1;
3107 case DEFINE_PEEPHOLE:
3108 id->insn_code = insn_code_number;
3109 id->insn_index = insn_index_number;
3110 id->num_alternatives = count_alternatives (exp);
3111 if (id->num_alternatives == 0)
3112 id->num_alternatives = 1;
3116 case DEFINE_ASM_ATTRIBUTES:
3118 id->insn_index = -1;
3119 id->num_alternatives = 1;
3121 got_define_asm_attributes = 1;
3129 /* Process a DEFINE_DELAY. Validate the vector length, check if annul
3130 true or annul false is specified, and make a `struct delay_desc'. */
3133 gen_delay (rtx def, int lineno)
3135 struct delay_desc *delay;
3138 if (XVECLEN (def, 1) % 3 != 0)
3140 error_with_line (lineno,
3141 "number of elements in DEFINE_DELAY must"
3142 " be multiple of three");
3146 for (i = 0; i < XVECLEN (def, 1); i += 3)
3148 if (XVECEXP (def, 1, i + 1))
3149 have_annul_true = 1;
3150 if (XVECEXP (def, 1, i + 2))
3151 have_annul_false = 1;
3154 delay = oballoc (struct delay_desc);
3156 delay->num = ++num_delays;
3157 delay->next = delays;
3158 delay->lineno = lineno;
3162 /* Names of attributes that could be possibly cached. */
3163 static const char *cached_attrs[32];
3164 /* Number of such attributes. */
3165 static int cached_attr_count;
3166 /* Bitmasks of possibly cached attributes. */
3167 static unsigned int attrs_seen_once, attrs_seen_more_than_once;
3168 static unsigned int attrs_to_cache;
3169 static unsigned int attrs_cached_inside, attrs_cached_after;
3171 /* Finds non-const attributes that could be possibly cached.
3172 When create is TRUE, fills in cached_attrs array.
3173 Computes ATTRS_SEEN_ONCE and ATTRS_SEEN_MORE_THAN_ONCE
3177 find_attrs_to_cache (rtx exp, bool create)
3181 struct attr_desc *attr;
3186 switch (GET_CODE (exp))
3189 if (GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3190 find_attrs_to_cache (XEXP (exp, 0), create);
3194 name = XSTR (exp, 0);
3195 if (name == alternative_name)
3197 for (i = 0; i < cached_attr_count; i++)
3198 if (name == cached_attrs[i])
3200 if ((attrs_seen_once & (1U << i)) != 0)
3201 attrs_seen_more_than_once |= (1U << i);
3203 attrs_seen_once |= (1U << i);
3208 attr = find_attr (&name, 0);
3212 if (cached_attr_count == 32)
3214 cached_attrs[cached_attr_count] = XSTR (exp, 0);
3215 attrs_seen_once |= (1U << cached_attr_count);
3216 cached_attr_count++;
3221 find_attrs_to_cache (XEXP (exp, 0), create);
3222 find_attrs_to_cache (XEXP (exp, 1), create);
3226 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3227 find_attrs_to_cache (XVECEXP (exp, 0, i), create);
3235 /* Given a piece of RTX, print a C expression to test its truth value.
3236 We use AND and IOR both for logical and bit-wise operations, so
3237 interpret them as logical unless they are inside a comparison expression. */
3239 /* Interpret AND/IOR as bit-wise operations instead of logical. */
3240 #define FLG_BITWISE 1
3241 /* Set if cached attribute will be known initialized in else block after
3242 this condition. This is true for LHS of toplevel && and || and
3243 even for RHS of ||, but not for RHS of &&. */
3245 /* Set if cached attribute will be known initialized in then block after
3246 this condition. This is true for LHS of toplevel && and || and
3247 even for RHS of &&, but not for RHS of ||. */
3248 #define FLG_INSIDE 4
3249 /* Cleared when an operand of &&. */
3250 #define FLG_OUTSIDE_AND 8
3253 write_test_expr (rtx exp, unsigned int attrs_cached, int flags)
3255 int comparison_operator = 0;
3257 struct attr_desc *attr;
3259 /* In order not to worry about operator precedence, surround our part of
3260 the expression with parentheses. */
3263 code = GET_CODE (exp);
3266 /* Binary operators. */
3269 printf ("(unsigned) ");
3275 comparison_operator = FLG_BITWISE;
3277 case PLUS: case MINUS: case MULT: case DIV: case MOD:
3278 case AND: case IOR: case XOR:
3279 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3280 if ((code != AND && code != IOR) || (flags & FLG_BITWISE))
3282 flags &= ~(FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND);
3283 write_test_expr (XEXP (exp, 0), attrs_cached,
3284 flags | comparison_operator);
3289 flags &= ~FLG_OUTSIDE_AND;
3290 if (GET_CODE (XEXP (exp, 0)) == code
3291 || GET_CODE (XEXP (exp, 0)) == EQ_ATTR
3292 || (GET_CODE (XEXP (exp, 0)) == NOT
3293 && GET_CODE (XEXP (XEXP (exp, 0), 0)) == EQ_ATTR))
3295 = write_test_expr (XEXP (exp, 0), attrs_cached, flags);
3297 write_test_expr (XEXP (exp, 0), attrs_cached, flags);
3314 printf (" >= (unsigned) ");
3317 printf (" > (unsigned) ");
3326 printf (" <= (unsigned) ");
3329 printf (" < (unsigned) ");
3347 if (flags & FLG_BITWISE)
3353 if (flags & FLG_BITWISE)
3374 /* For if (something && (cached_x = get_attr_x (insn)) == X)
3375 cached_x is only known to be initialized in then block. */
3376 flags &= ~FLG_AFTER;
3378 else if (code == IOR)
3380 if (flags & FLG_OUTSIDE_AND)
3381 /* For if (something || (cached_x = get_attr_x (insn)) == X)
3382 cached_x is only known to be initialized in else block
3383 and else if conditions. */
3384 flags &= ~FLG_INSIDE;
3386 /* For if ((something || (cached_x = get_attr_x (insn)) == X)
3388 cached_x is not know to be initialized anywhere. */
3389 flags &= ~(FLG_AFTER | FLG_INSIDE);
3391 if ((code == AND || code == IOR)
3392 && (GET_CODE (XEXP (exp, 1)) == code
3393 || GET_CODE (XEXP (exp, 1)) == EQ_ATTR
3394 || (GET_CODE (XEXP (exp, 1)) == NOT
3395 && GET_CODE (XEXP (XEXP (exp, 1), 0)) == EQ_ATTR)))
3397 = write_test_expr (XEXP (exp, 1), attrs_cached, flags);
3399 write_test_expr (XEXP (exp, 1), attrs_cached,
3400 flags | comparison_operator);
3404 /* Special-case (not (eq_attrq "alternative" "x")) */
3405 if (! (flags & FLG_BITWISE) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
3407 if (XSTR (XEXP (exp, 0), 0) == alternative_name)
3409 printf ("which_alternative != %s", XSTR (XEXP (exp, 0), 1));
3414 attrs_cached = write_test_expr (XEXP (exp, 0), attrs_cached, flags);
3418 /* Otherwise, fall through to normal unary operator. */
3420 /* Unary operators. */
3425 if (flags & FLG_BITWISE)
3440 flags &= ~(FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND);
3441 write_test_expr (XEXP (exp, 0), attrs_cached, flags);
3446 int set = XINT (exp, 0), bit = 0;
3448 if (flags & FLG_BITWISE)
3449 fatal ("EQ_ATTR_ALT not valid inside comparison");
3452 fatal ("Empty EQ_ATTR_ALT should be optimized out");
3454 if (!(set & (set - 1)))
3456 if (!(set & 0xffff))
3479 printf ("which_alternative %s= %d",
3480 XINT (exp, 1) ? "!" : "=", bit);
3484 printf ("%s((1 << which_alternative) & %#x)",
3485 XINT (exp, 1) ? "!" : "", set);
3490 /* Comparison test of an attribute with a value. Most of these will
3491 have been removed by optimization. Handle "alternative"
3492 specially and give error if EQ_ATTR present inside a comparison. */
3494 if (flags & FLG_BITWISE)
3495 fatal ("EQ_ATTR not valid inside comparison");
3497 if (XSTR (exp, 0) == alternative_name)
3499 printf ("which_alternative == %s", XSTR (exp, 1));
3503 attr = find_attr (&XSTR (exp, 0), 0);
3506 /* Now is the time to expand the value of a constant attribute. */
3509 write_test_expr (evaluate_eq_attr (exp, attr,
3510 attr->default_val->value, -2, -2),
3516 for (i = 0; i < cached_attr_count; i++)
3517 if (attr->name == cached_attrs[i])
3519 if (i < cached_attr_count && (attrs_cached & (1U << i)) != 0)
3520 printf ("cached_%s", attr->name);
3521 else if (i < cached_attr_count && (attrs_to_cache & (1U << i)) != 0)
3523 printf ("(cached_%s = get_attr_%s (insn))",
3524 attr->name, attr->name);
3525 if (flags & FLG_AFTER)
3526 attrs_cached_after |= (1U << i);
3527 if (flags & FLG_INSIDE)
3528 attrs_cached_inside |= (1U << i);
3529 attrs_cached |= (1U << i);
3532 printf ("get_attr_%s (insn)", attr->name);
3534 write_attr_valueq (attr, XSTR (exp, 1));
3538 /* Comparison test of flags for define_delays. */
3540 if (flags & FLG_BITWISE)
3541 fatal ("ATTR_FLAG not valid inside comparison");
3542 printf ("(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
3545 /* See if an operand matches a predicate. */
3547 /* If only a mode is given, just ensure the mode matches the operand.
3548 If neither a mode nor predicate is given, error. */
3549 if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
3551 if (GET_MODE (exp) == VOIDmode)
3552 fatal ("null MATCH_OPERAND specified as test");
3554 printf ("GET_MODE (operands[%d]) == %smode",
3555 XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3558 printf ("%s (operands[%d], %smode)",
3559 XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
3562 /* Constant integer. */
3564 printf (HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
3567 /* A random C expression. */
3569 print_c_condition (XSTR (exp, 0));
3572 /* The address of the branch target. */
3574 printf ("INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
3575 XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
3579 /* The address of the current insn. We implement this actually as the
3580 address of the current insn for backward branches, but the last
3581 address of the next insn for forward branches, and both with
3582 adjustments that account for the worst-case possible stretching of
3583 intervening alignments between this insn and its destination. */
3584 printf ("insn_current_reference_address (insn)");
3588 printf ("%s", XSTR (exp, 0));
3592 write_test_expr (XEXP (exp, 0), attrs_cached, 0);
3594 write_test_expr (XEXP (exp, 1), attrs_cached, FLG_BITWISE);
3596 write_test_expr (XEXP (exp, 2), attrs_cached, FLG_BITWISE);
3600 fatal ("bad RTX code `%s' in attribute calculation\n",
3601 GET_RTX_NAME (code));
3605 return attrs_cached;
3608 /* Given an attribute value, return the maximum CONST_STRING argument
3609 encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
3612 max_attr_value (rtx exp, int *unknownp)
3617 switch (GET_CODE (exp))
3620 current_max = atoi (XSTR (exp, 0));
3624 current_max = max_attr_value (XEXP (exp, 1), unknownp);
3625 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3627 n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3628 if (n > current_max)
3634 current_max = max_attr_value (XEXP (exp, 1), unknownp);
3635 n = max_attr_value (XEXP (exp, 2), unknownp);
3636 if (n > current_max)
3642 current_max = INT_MAX;
3649 /* Given an attribute value, return the minimum CONST_STRING argument
3650 encountered. Set *UNKNOWNP and return 0 if the value is unknown. */
3653 min_attr_value (rtx exp, int *unknownp)
3658 switch (GET_CODE (exp))
3661 current_min = atoi (XSTR (exp, 0));
3665 current_min = min_attr_value (XEXP (exp, 1), unknownp);
3666 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3668 n = min_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3669 if (n < current_min)
3675 current_min = min_attr_value (XEXP (exp, 1), unknownp);
3676 n = min_attr_value (XEXP (exp, 2), unknownp);
3677 if (n < current_min)
3683 current_min = INT_MAX;
3690 /* Given an attribute value, return the result of ORing together all
3691 CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
3692 if the numeric value is not known. */
3695 or_attr_value (rtx exp, int *unknownp)
3700 switch (GET_CODE (exp))
3703 current_or = atoi (XSTR (exp, 0));
3707 current_or = or_attr_value (XEXP (exp, 1), unknownp);
3708 for (i = 0; i < XVECLEN (exp, 0); i += 2)
3709 current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
3713 current_or = or_attr_value (XEXP (exp, 1), unknownp);
3714 current_or |= or_attr_value (XEXP (exp, 2), unknownp);
3726 /* Scan an attribute value, possibly a conditional, and record what actions
3727 will be required to do any conditional tests in it.
3730 `must_extract' if we need to extract the insn operands
3731 `must_constrain' if we must compute `which_alternative'
3732 `address_used' if an address expression was used
3733 `length_used' if an (eq_attr "length" ...) was used
3737 walk_attr_value (rtx exp)
3746 code = GET_CODE (exp);
3750 if (! ATTR_IND_SIMPLIFIED_P (exp))
3751 /* Since this is an arbitrary expression, it can look at anything.
3752 However, constant expressions do not depend on any particular
3754 must_extract = must_constrain = 1;
3762 must_extract = must_constrain = 1;
3766 if (XSTR (exp, 0) == alternative_name)
3767 must_extract = must_constrain = 1;
3768 else if (strcmp_check (XSTR (exp, 0), length_str) == 0)
3788 for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
3793 walk_attr_value (XEXP (exp, i));
3797 if (XVEC (exp, i) != NULL)
3798 for (j = 0; j < XVECLEN (exp, i); j++)
3799 walk_attr_value (XVECEXP (exp, i, j));
3804 /* Write out a function to obtain the attribute for a given INSN. */
3807 write_attr_get (struct attr_desc *attr)
3809 struct attr_value *av, *common_av;
3812 /* Find the most used attribute value. Handle that as the `default' of the
3813 switch we will generate. */
3814 common_av = find_most_used (attr);
3816 /* Write out start of function, then all values with explicit `case' lines,
3817 then a `default', then the value with the most uses. */
3818 if (attr->enum_name)
3819 printf ("enum %s\n", attr->enum_name);
3820 else if (!attr->is_numeric)
3821 printf ("enum attr_%s\n", attr->name);
3825 /* If the attribute name starts with a star, the remainder is the name of
3826 the subroutine to use, instead of `get_attr_...'. */
3827 if (attr->name[0] == '*')
3828 printf ("%s (rtx insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
3829 else if (attr->is_const == 0)
3830 printf ("get_attr_%s (rtx insn ATTRIBUTE_UNUSED)\n", attr->name);
3833 printf ("get_attr_%s (void)\n", attr->name);
3836 for (av = attr->first_value; av; av = av->next)
3837 if (av->num_insns == 1)
3838 write_attr_set (attr, 2, av->value, "return", ";",
3839 true_rtx, av->first_insn->def->insn_code,
3840 av->first_insn->def->insn_index, 0);
3841 else if (av->num_insns != 0)
3842 write_attr_set (attr, 2, av->value, "return", ";",
3843 true_rtx, -2, 0, 0);
3851 /* Find attributes that are worth caching in the conditions. */
3852 cached_attr_count = 0;
3853 attrs_seen_more_than_once = 0;
3854 for (av = attr->first_value; av; av = av->next)
3856 attrs_seen_once = 0;
3857 find_attrs_to_cache (av->value, true);
3859 /* Remove those that aren't worth caching from the array. */
3860 for (i = 0, j = 0; i < cached_attr_count; i++)
3861 if ((attrs_seen_more_than_once & (1U << i)) != 0)
3863 const char *name = cached_attrs[i];
3864 struct attr_desc *cached_attr;
3866 cached_attrs[j] = name;
3867 cached_attr = find_attr (&name, 0);
3868 gcc_assert (cached_attr && cached_attr->is_const == 0);
3869 if (cached_attr->enum_name)
3870 printf (" enum %s", cached_attr->enum_name);
3871 else if (!cached_attr->is_numeric)
3872 printf (" enum attr_%s", cached_attr->name);
3875 printf (" cached_%s ATTRIBUTE_UNUSED;\n", name);
3878 cached_attr_count = j;
3879 if (cached_attr_count)
3882 printf (" switch (recog_memoized (insn))\n");
3885 for (av = attr->first_value; av; av = av->next)
3886 if (av != common_av)
3887 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
3889 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
3890 printf (" }\n}\n\n");
3891 cached_attr_count = 0;
3894 /* Given an AND tree of known true terms (because we are inside an `if' with
3895 that as the condition or are in an `else' clause) and an expression,
3896 replace any known true terms with TRUE. Use `simplify_and_tree' to do
3897 the bulk of the work. */
3900 eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
3904 known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
3906 if (GET_CODE (known_true) == AND)
3908 exp = eliminate_known_true (XEXP (known_true, 0), exp,
3909 insn_code, insn_index);
3910 exp = eliminate_known_true (XEXP (known_true, 1), exp,
3911 insn_code, insn_index);
3916 exp = simplify_and_tree (exp, &term, insn_code, insn_index);
3922 /* Write out a series of tests and assignment statements to perform tests and
3923 sets of an attribute value. We are passed an indentation amount and prefix
3924 and suffix strings to write around each attribute value (e.g., "return"
3928 write_attr_set (struct attr_desc *attr, int indent, rtx value,
3929 const char *prefix, const char *suffix, rtx known_true,
3930 int insn_code, int insn_index, unsigned int attrs_cached)
3932 if (GET_CODE (value) == COND)
3934 /* Assume the default value will be the default of the COND unless we
3935 find an always true expression. */
3936 rtx default_val = XEXP (value, 1);
3937 rtx our_known_true = known_true;
3942 if (cached_attr_count)
3944 attrs_seen_once = 0;
3945 attrs_seen_more_than_once = 0;
3946 for (i = 0; i < XVECLEN (value, 0); i += 2)
3947 find_attrs_to_cache (XVECEXP (value, 0, i), false);
3948 attrs_to_cache |= attrs_seen_more_than_once;
3951 for (i = 0; i < XVECLEN (value, 0); i += 2)
3956 testexp = eliminate_known_true (our_known_true,
3957 XVECEXP (value, 0, i),
3958 insn_code, insn_index);
3959 newexp = attr_rtx (NOT, testexp);
3960 newexp = insert_right_side (AND, our_known_true, newexp,
3961 insn_code, insn_index);
3963 /* If the test expression is always true or if the next `known_true'
3964 expression is always false, this is the last case, so break
3965 out and let this value be the `else' case. */
3966 if (testexp == true_rtx || newexp == false_rtx)
3968 default_val = XVECEXP (value, 0, i + 1);
3972 /* Compute the expression to pass to our recursive call as being
3974 inner_true = insert_right_side (AND, our_known_true,
3975 testexp, insn_code, insn_index);
3977 /* If this is always false, skip it. */
3978 if (inner_true == false_rtx)
3981 attrs_cached_inside = attrs_cached;
3982 attrs_cached_after = attrs_cached;
3983 write_indent (indent);
3984 printf ("%sif ", first_if ? "" : "else ");
3986 write_test_expr (testexp, attrs_cached,
3987 (FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND));
3988 attrs_cached = attrs_cached_after;
3990 write_indent (indent + 2);
3993 write_attr_set (attr, indent + 4,
3994 XVECEXP (value, 0, i + 1), prefix, suffix,
3995 inner_true, insn_code, insn_index,
3996 attrs_cached_inside);
3997 write_indent (indent + 2);
3999 our_known_true = newexp;
4004 write_indent (indent);
4006 write_indent (indent + 2);
4010 write_attr_set (attr, first_if ? indent : indent + 4, default_val,
4011 prefix, suffix, our_known_true, insn_code, insn_index,
4016 write_indent (indent + 2);
4022 write_indent (indent);
4023 printf ("%s ", prefix);
4024 write_attr_value (attr, value);
4025 printf ("%s\n", suffix);
4029 /* Write a series of case statements for every instruction in list IE.
4030 INDENT is the amount of indentation to write before each case. */
4033 write_insn_cases (struct insn_ent *ie, int indent)
4035 for (; ie != 0; ie = ie->next)
4036 if (ie->def->insn_code != -1)
4038 write_indent (indent);
4039 if (GET_CODE (ie->def->def) == DEFINE_PEEPHOLE)
4040 printf ("case %d: /* define_peephole, line %d */\n",
4041 ie->def->insn_code, ie->def->lineno);
4043 printf ("case %d: /* %s */\n",
4044 ie->def->insn_code, XSTR (ie->def->def, 0));
4048 /* Write out the computation for one attribute value. */
4051 write_attr_case (struct attr_desc *attr, struct attr_value *av,
4052 int write_case_lines, const char *prefix, const char *suffix,
4053 int indent, rtx known_true)
4055 if (av->num_insns == 0)
4058 if (av->has_asm_insn)
4060 write_indent (indent);
4061 printf ("case -1:\n");
4062 write_indent (indent + 2);
4063 printf ("if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
4064 write_indent (indent + 2);
4065 printf (" && asm_noperands (PATTERN (insn)) < 0)\n");
4066 write_indent (indent + 2);
4067 printf (" fatal_insn_not_found (insn);\n");
4070 if (write_case_lines)
4071 write_insn_cases (av->first_insn, indent);
4074 write_indent (indent);
4075 printf ("default:\n");
4078 /* See what we have to do to output this value. */
4079 must_extract = must_constrain = address_used = 0;
4080 walk_attr_value (av->value);
4084 write_indent (indent + 2);
4085 printf ("extract_constrain_insn_cached (insn);\n");
4087 else if (must_extract)
4089 write_indent (indent + 2);
4090 printf ("extract_insn_cached (insn);\n");
4094 if (av->num_insns == 1)
4095 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
4096 known_true, av->first_insn->def->insn_code,
4097 av->first_insn->def->insn_index, 0);
4099 write_attr_set (attr, indent + 2, av->value, prefix, suffix,
4100 known_true, -2, 0, 0);
4102 if (strncmp (prefix, "return", 6))
4104 write_indent (indent + 2);
4105 printf ("break;\n");
4110 /* Utilities to write in various forms. */
4113 write_attr_valueq (struct attr_desc *attr, const char *s)
4115 if (attr->is_numeric)
4121 if (num > 9 || num < 0)
4122 printf (" /* %#x */", num);
4126 write_upcase (attr->enum_name ? attr->enum_name : attr->name);
4133 write_attr_value (struct attr_desc *attr, rtx value)
4137 switch (GET_CODE (value))
4140 write_attr_valueq (attr, XSTR (value, 0));
4144 printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
4148 print_c_condition (XSTR (value, 0));
4153 struct attr_desc *attr2 = find_attr (&XSTR (value, 0), 0);
4154 if (attr->enum_name)
4155 printf ("(enum %s)", attr->enum_name);
4156 else if (!attr->is_numeric)
4157 printf ("(enum attr_%s)", attr->name);
4158 else if (!attr2->is_numeric)
4161 printf ("get_attr_%s (%s)", attr2->name,
4162 (attr2->is_const ? "" : "insn"));
4183 write_attr_value (attr, XEXP (value, 0));
4187 write_attr_value (attr, XEXP (value, 1));
4196 write_upcase (const char *str)
4200 /* The argument of TOUPPER should not have side effects. */
4201 putchar (TOUPPER(*str));
4207 write_indent (int indent)
4209 for (; indent > 8; indent -= 8)
4212 for (; indent; indent--)
4216 /* Write a subroutine that is given an insn that requires a delay slot, a
4217 delay slot ordinal, and a candidate insn. It returns nonzero if the
4218 candidate can be placed in the specified delay slot of the insn.
4220 We can write as many as three subroutines. `eligible_for_delay'
4221 handles normal delay slots, `eligible_for_annul_true' indicates that
4222 the specified insn can be annulled if the branch is true, and likewise
4223 for `eligible_for_annul_false'.
4225 KIND is a string distinguishing these three cases ("delay", "annul_true",
4226 or "annul_false"). */
4229 write_eligible_delay (const char *kind)
4231 struct delay_desc *delay;
4235 struct attr_desc *attr;
4236 struct attr_value *av, *common_av;
4239 /* Compute the maximum number of delay slots required. We use the delay
4240 ordinal times this number plus one, plus the slot number as an index into
4241 the appropriate predicate to test. */
4243 for (delay = delays, max_slots = 0; delay; delay = delay->next)
4244 if (XVECLEN (delay->def, 1) / 3 > max_slots)
4245 max_slots = XVECLEN (delay->def, 1) / 3;
4247 /* Write function prelude. */
4250 printf ("eligible_for_%s (rtx delay_insn ATTRIBUTE_UNUSED, int slot, rtx candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
4253 printf (" rtx insn;\n");
4255 printf (" gcc_assert (slot < %d);\n", max_slots);
4257 /* Allow dbr_schedule to pass labels, etc. This can happen if try_split
4258 converts a compound instruction into a loop. */
4259 printf (" if (!INSN_P (candidate_insn))\n");
4260 printf (" return 0;\n");
4263 /* If more than one delay type, find out which type the delay insn is. */
4267 attr = find_attr (&delay_type_str, 0);
4269 common_av = find_most_used (attr);
4271 printf (" insn = delay_insn;\n");
4272 printf (" switch (recog_memoized (insn))\n");
4275 sprintf (str, " * %d;\n break;", max_slots);
4276 for (av = attr->first_value; av; av = av->next)
4277 if (av != common_av)
4278 write_attr_case (attr, av, 1, "slot +=", str, 4, true_rtx);
4280 write_attr_case (attr, common_av, 0, "slot +=", str, 4, true_rtx);
4283 /* Ensure matched. Otherwise, shouldn't have been called. */
4284 printf (" gcc_assert (slot >= %d);\n\n", max_slots);
4287 /* If just one type of delay slot, write simple switch. */
4288 if (num_delays == 1 && max_slots == 1)
4290 printf (" insn = candidate_insn;\n");
4291 printf (" switch (recog_memoized (insn))\n");
4294 attr = find_attr (&delay_1_0_str, 0);
4296 common_av = find_most_used (attr);
4298 for (av = attr->first_value; av; av = av->next)
4299 if (av != common_av)
4300 write_attr_case (attr, av, 1, "return", ";", 4, true_rtx);
4302 write_attr_case (attr, common_av, 0, "return", ";", 4, true_rtx);
4308 /* Write a nested CASE. The first indicates which condition we need to
4309 test, and the inner CASE tests the condition. */
4310 printf (" insn = candidate_insn;\n");
4311 printf (" switch (slot)\n");
4314 for (delay = delays; delay; delay = delay->next)
4315 for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
4317 printf (" case %d:\n",
4318 (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
4319 printf (" switch (recog_memoized (insn))\n");
4322 sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
4324 attr = find_attr (&pstr, 0);
4326 common_av = find_most_used (attr);
4328 for (av = attr->first_value; av; av = av->next)
4329 if (av != common_av)
4330 write_attr_case (attr, av, 1, "return", ";", 8, true_rtx);
4332 write_attr_case (attr, common_av, 0, "return", ";", 8, true_rtx);
4336 printf (" default:\n");
4337 printf (" gcc_unreachable ();\n");
4344 /* This page contains miscellaneous utility routines. */
4346 /* Given a pointer to a (char *), return a malloc'ed string containing the
4347 next comma-separated element. Advance the pointer to after the string
4348 scanned, or the end-of-string. Return NULL if at end of string. */
4351 next_comma_elt (const char **pstr)
4355 start = scan_comma_elt (pstr);
4360 return attr_string (start, *pstr - start);
4363 /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
4364 is nonzero, build a new attribute, if one does not exist. *NAME_P is
4365 replaced by a pointer to a canonical copy of the string. */
4367 static struct attr_desc *
4368 find_attr (const char **name_p, int create)
4370 struct attr_desc *attr;
4372 const char *name = *name_p;
4374 /* Before we resort to using `strcmp', see if the string address matches
4375 anywhere. In most cases, it should have been canonicalized to do so. */
4376 if (name == alternative_name)
4379 index = name[0] & (MAX_ATTRS_INDEX - 1);
4380 for (attr = attrs[index]; attr; attr = attr->next)
4381 if (name == attr->name)
4384 /* Otherwise, do it the slow way. */
4385 for (attr = attrs[index]; attr; attr = attr->next)
4386 if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
4388 *name_p = attr->name;
4395 attr = oballoc (struct attr_desc);
4396 attr->name = DEF_ATTR_STRING (name);
4397 attr->enum_name = 0;
4398 attr->first_value = attr->default_val = NULL;
4399 attr->is_numeric = attr->is_const = attr->is_special = 0;
4400 attr->next = attrs[index];
4401 attrs[index] = attr;
4403 *name_p = attr->name;
4408 /* Create internal attribute with the given default value. */
4411 make_internal_attr (const char *name, rtx value, int special)
4413 struct attr_desc *attr;
4415 attr = find_attr (&name, 1);
4416 gcc_assert (!attr->default_val);
4418 attr->is_numeric = 1;
4420 attr->is_special = (special & ATTR_SPECIAL) != 0;
4421 attr->default_val = get_attr_value (value, attr, -2);
4424 /* Find the most used value of an attribute. */
4426 static struct attr_value *
4427 find_most_used (struct attr_desc *attr)
4429 struct attr_value *av;
4430 struct attr_value *most_used;
4436 for (av = attr->first_value; av; av = av->next)
4437 if (av->num_insns > nuses)
4438 nuses = av->num_insns, most_used = av;
4443 /* Return (attr_value "n") */
4446 make_numeric_value (int n)
4448 static rtx int_values[20];
4452 gcc_assert (n >= 0);
4454 if (n < 20 && int_values[n])
4455 return int_values[n];
4457 p = attr_printf (MAX_DIGITS, "%d", n);
4458 exp = attr_rtx (CONST_STRING, p);
4461 int_values[n] = exp;
4467 copy_rtx_unchanging (rtx orig)
4469 if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
4472 ATTR_CURR_SIMPLIFIED_P (orig) = 1;
4476 /* Determine if an insn has a constant number of delay slots, i.e., the
4477 number of delay slots is not a function of the length of the insn. */
4480 write_const_num_delay_slots (void)
4482 struct attr_desc *attr = find_attr (&num_delay_slots_str, 0);
4483 struct attr_value *av;
4487 printf ("int\nconst_num_delay_slots (rtx insn)\n");
4489 printf (" switch (recog_memoized (insn))\n");
4492 for (av = attr->first_value; av; av = av->next)
4495 walk_attr_value (av->value);
4497 write_insn_cases (av->first_insn, 4);
4500 printf (" default:\n");
4501 printf (" return 1;\n");
4502 printf (" }\n}\n\n");
4506 /* Synthetic attributes used by insn-automata.c and the scheduler.
4507 These are primarily concerned with (define_insn_reservation)
4512 struct insn_reserv *next;
4515 int default_latency;
4518 /* Sequence number of this insn. */
4521 /* Whether a (define_bypass) construct names this insn in its
4526 static struct insn_reserv *all_insn_reservs = 0;
4527 static struct insn_reserv **last_insn_reserv_p = &all_insn_reservs;
4528 static size_t n_insn_reservs;
4530 /* Store information from a DEFINE_INSN_RESERVATION for future
4531 attribute generation. */
4533 gen_insn_reserv (rtx def)
4535 struct insn_reserv *decl = oballoc (struct insn_reserv);
4537 decl->name = DEF_ATTR_STRING (XSTR (def, 0));
4538 decl->default_latency = XINT (def, 1);
4539 decl->condexp = check_attr_test (XEXP (def, 2), 0, 0);
4540 decl->insn_num = n_insn_reservs;
4541 decl->bypassed = false;
4544 *last_insn_reserv_p = decl;
4545 last_insn_reserv_p = &decl->next;
4549 /* Store information from a DEFINE_BYPASS for future attribute
4550 generation. The only thing we care about is the list of output
4551 insns, which will later be used to tag reservation structures with
4552 a 'bypassed' bit. */
4556 struct bypass_list *next;
4557 const char *pattern;
4560 static struct bypass_list *all_bypasses;
4561 static size_t n_bypasses;
4564 gen_bypass_1 (const char *s, size_t len)
4566 struct bypass_list *b;
4571 s = attr_string (s, len);
4572 for (b = all_bypasses; b; b = b->next)
4573 if (s == b->pattern)
4574 return; /* already got that one */
4576 b = oballoc (struct bypass_list);
4578 b->next = all_bypasses;
4584 gen_bypass (rtx def)
4586 const char *p, *base;
4588 for (p = base = XSTR (def, 1); *p; p++)
4591 gen_bypass_1 (base, p - base);
4594 while (ISSPACE (*p));
4597 gen_bypass_1 (base, p - base);
4600 /* Find and mark all of the bypassed insns. */
4602 process_bypasses (void)
4604 struct bypass_list *b;
4605 struct insn_reserv *r;
4607 /* The reservation list is likely to be much longer than the bypass
4609 for (r = all_insn_reservs; r; r = r->next)
4610 for (b = all_bypasses; b; b = b->next)
4611 if (fnmatch (b->pattern, r->name, 0) == 0)
4615 /* Check that attribute NAME is used in define_insn_reservation condition
4616 EXP. Return true if it is. */
4618 check_tune_attr (const char *name, rtx exp)
4620 switch (GET_CODE (exp))
4623 if (check_tune_attr (name, XEXP (exp, 0)))
4625 return check_tune_attr (name, XEXP (exp, 1));
4628 return (check_tune_attr (name, XEXP (exp, 0))
4629 && check_tune_attr (name, XEXP (exp, 1)));
4632 return XSTR (exp, 0) == name;
4639 /* Try to find a const attribute (usually cpu or tune) that is used
4640 in all define_insn_reservation conditions. */
4641 static struct attr_desc *
4642 find_tune_attr (rtx exp)
4644 struct attr_desc *attr;
4646 switch (GET_CODE (exp))
4650 attr = find_tune_attr (XEXP (exp, 0));
4653 return find_tune_attr (XEXP (exp, 1));
4656 if (XSTR (exp, 0) == alternative_name)
4659 attr = find_attr (&XSTR (exp, 0), 0);
4662 if (attr->is_const && !attr->is_special)
4664 struct insn_reserv *decl;
4666 for (decl = all_insn_reservs; decl; decl = decl->next)
4667 if (! check_tune_attr (attr->name, decl->condexp))
4678 /* Create all of the attributes that describe automaton properties. */
4680 make_automaton_attrs (void)
4683 struct insn_reserv *decl;
4684 rtx code_exp, lats_exp, byps_exp;
4685 struct attr_desc *tune_attr;
4687 if (n_insn_reservs == 0)
4690 tune_attr = find_tune_attr (all_insn_reservs->condexp);
4691 if (tune_attr != NULL)
4693 rtx *condexps = XNEWVEC (rtx, n_insn_reservs * 3);
4694 struct attr_value *val;
4697 gcc_assert (tune_attr->is_const
4698 && !tune_attr->is_special
4699 && !tune_attr->is_numeric);
4700 for (val = tune_attr->first_value; val; val = val->next)
4702 if (val == tune_attr->default_val)
4704 gcc_assert (GET_CODE (val->value) == CONST_STRING);
4705 printf ("static int internal_dfa_insn_code_%s (rtx);\n"
4706 "static int insn_default_latency_%s (rtx);\n",
4707 XSTR (val->value, 0), XSTR (val->value, 0));
4711 printf ("int (*internal_dfa_insn_code) (rtx);\n");
4712 printf ("int (*insn_default_latency) (rtx);\n");
4715 printf ("init_sched_attrs (void)\n");
4718 for (val = tune_attr->first_value; val; val = val->next)
4722 rtx test = attr_rtx (EQ_ATTR, tune_attr->name, XSTR (val->value, 0));
4724 if (val == tune_attr->default_val)
4726 for (decl = all_insn_reservs, i = 0;
4732 = simplify_and_tree (decl->condexp, &ctest, -2, 0);
4733 if (condexp == false_rtx)
4735 if (condexp == true_rtx)
4737 condexps[i] = condexp;
4738 condexps[i + 1] = make_numeric_value (decl->insn_num);
4739 condexps[i + 2] = make_numeric_value (decl->default_latency);
4743 code_exp = rtx_alloc (COND);
4744 lats_exp = rtx_alloc (COND);
4747 XVEC (code_exp, 0) = rtvec_alloc (j);
4748 XVEC (lats_exp, 0) = rtvec_alloc (j);
4752 XEXP (code_exp, 1) = make_numeric_value (decl->insn_num);
4753 XEXP (lats_exp, 1) = make_numeric_value (decl->default_latency);
4757 XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
4758 XEXP (lats_exp, 1) = make_numeric_value (0);
4765 XVECEXP (code_exp, 0, j) = condexps[i];
4766 XVECEXP (lats_exp, 0, j) = condexps[i];
4768 XVECEXP (code_exp, 0, j + 1) = condexps[i + 1];
4769 XVECEXP (lats_exp, 0, j + 1) = condexps[i + 2];
4772 name = XNEWVEC (char,
4773 sizeof ("*internal_dfa_insn_code_")
4774 + strlen (XSTR (val->value, 0)));
4775 strcpy (name, "*internal_dfa_insn_code_");
4776 strcat (name, XSTR (val->value, 0));
4777 make_internal_attr (name, code_exp, ATTR_NONE);
4778 strcpy (name, "*insn_default_latency_");
4779 strcat (name, XSTR (val->value, 0));
4780 make_internal_attr (name, lats_exp, ATTR_NONE);
4789 printf (" else if (");
4790 write_test_expr (test, 0, 0);
4793 printf (" internal_dfa_insn_code\n");
4794 printf (" = internal_dfa_insn_code_%s;\n",
4795 XSTR (val->value, 0));
4796 printf (" insn_default_latency\n");
4797 printf (" = insn_default_latency_%s;\n",
4798 XSTR (val->value, 0));
4803 printf (" gcc_unreachable ();\n");
4807 XDELETEVEC (condexps);
4811 code_exp = rtx_alloc (COND);
4812 lats_exp = rtx_alloc (COND);
4814 XVEC (code_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
4815 XVEC (lats_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
4817 XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
4818 XEXP (lats_exp, 1) = make_numeric_value (0);
4820 for (decl = all_insn_reservs, i = 0;
4822 decl = decl->next, i += 2)
4824 XVECEXP (code_exp, 0, i) = decl->condexp;
4825 XVECEXP (lats_exp, 0, i) = decl->condexp;
4827 XVECEXP (code_exp, 0, i+1) = make_numeric_value (decl->insn_num);
4828 XVECEXP (lats_exp, 0, i+1)
4829 = make_numeric_value (decl->default_latency);
4831 make_internal_attr ("*internal_dfa_insn_code", code_exp, ATTR_NONE);
4832 make_internal_attr ("*insn_default_latency", lats_exp, ATTR_NONE);
4835 if (n_bypasses == 0)
4836 byps_exp = make_numeric_value (0);
4839 process_bypasses ();
4841 byps_exp = rtx_alloc (COND);
4842 XVEC (byps_exp, 0) = rtvec_alloc (n_bypasses * 2);
4843 XEXP (byps_exp, 1) = make_numeric_value (0);
4844 for (decl = all_insn_reservs, i = 0;
4849 XVECEXP (byps_exp, 0, i) = decl->condexp;
4850 XVECEXP (byps_exp, 0, i+1) = make_numeric_value (1);
4855 make_internal_attr ("*bypass_p", byps_exp, ATTR_NONE);
4859 main (int argc, char **argv)
4862 struct attr_desc *attr;
4863 struct insn_def *id;
4867 progname = "genattrtab";
4869 if (!init_rtx_reader_args (argc, argv))
4870 return (FATAL_EXIT_CODE);
4872 obstack_init (hash_obstack);
4873 obstack_init (temp_obstack);
4875 /* Set up true and false rtx's */
4876 true_rtx = rtx_alloc (CONST_INT);
4877 XWINT (true_rtx, 0) = 1;
4878 false_rtx = rtx_alloc (CONST_INT);
4879 XWINT (false_rtx, 0) = 0;
4880 ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
4881 ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
4883 alternative_name = DEF_ATTR_STRING ("alternative");
4884 length_str = DEF_ATTR_STRING ("length");
4885 delay_type_str = DEF_ATTR_STRING ("*delay_type");
4886 delay_1_0_str = DEF_ATTR_STRING ("*delay_1_0");
4887 num_delay_slots_str = DEF_ATTR_STRING ("*num_delay_slots");
4889 printf ("/* Generated automatically by the program `genattrtab'\n\
4890 from the machine description file `md'. */\n\n");
4892 /* Read the machine description. */
4898 desc = read_md_rtx (&lineno, &insn_code_number);
4902 switch (GET_CODE (desc))
4905 case DEFINE_PEEPHOLE:
4906 case DEFINE_ASM_ATTRIBUTES:
4907 gen_insn (desc, lineno);
4911 case DEFINE_ENUM_ATTR:
4912 gen_attr (desc, lineno);
4916 gen_delay (desc, lineno);
4919 case DEFINE_INSN_RESERVATION:
4920 gen_insn_reserv (desc);
4930 if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
4931 insn_index_number++;
4935 return FATAL_EXIT_CODE;
4939 /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
4940 if (! got_define_asm_attributes)
4942 tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
4943 XVEC (tem, 0) = rtvec_alloc (0);
4947 /* Expand DEFINE_DELAY information into new attribute. */
4951 printf ("#include \"config.h\"\n");
4952 printf ("#include \"system.h\"\n");
4953 printf ("#include \"coretypes.h\"\n");
4954 printf ("#include \"tm.h\"\n");
4955 printf ("#include \"rtl.h\"\n");
4956 printf ("#include \"insn-attr.h\"\n");
4957 printf ("#include \"tm_p.h\"\n");
4958 printf ("#include \"insn-config.h\"\n");
4959 printf ("#include \"recog.h\"\n");
4960 printf ("#include \"regs.h\"\n");
4961 printf ("#include \"output.h\"\n");
4962 printf ("#include \"diagnostic-core.h\"\n");
4963 printf ("#include \"flags.h\"\n");
4964 printf ("#include \"function.h\"\n");
4966 printf ("#define operands recog_data.operand\n\n");
4968 /* Make `insn_alternatives'. */
4969 insn_alternatives = oballocvec (int, insn_code_number);
4970 for (id = defs; id; id = id->next)
4971 if (id->insn_code >= 0)
4972 insn_alternatives[id->insn_code] = (1 << id->num_alternatives) - 1;
4974 /* Make `insn_n_alternatives'. */
4975 insn_n_alternatives = oballocvec (int, insn_code_number);
4976 for (id = defs; id; id = id->next)
4977 if (id->insn_code >= 0)
4978 insn_n_alternatives[id->insn_code] = id->num_alternatives;
4980 /* Construct extra attributes for automata. */
4981 make_automaton_attrs ();
4983 /* Prepare to write out attribute subroutines by checking everything stored
4984 away and building the attribute cases. */
4988 for (i = 0; i < MAX_ATTRS_INDEX; i++)
4989 for (attr = attrs[i]; attr; attr = attr->next)
4990 attr->default_val->value
4991 = check_attr_value (attr->default_val->value, attr);
4994 return FATAL_EXIT_CODE;
4996 for (i = 0; i < MAX_ATTRS_INDEX; i++)
4997 for (attr = attrs[i]; attr; attr = attr->next)
5000 /* Construct extra attributes for `length'. */
5001 make_length_attrs ();
5003 /* Perform any possible optimizations to speed up compilation. */
5006 /* Now write out all the `gen_attr_...' routines. Do these before the
5007 special routines so that they get defined before they are used. */
5009 for (i = 0; i < MAX_ATTRS_INDEX; i++)
5010 for (attr = attrs[i]; attr; attr = attr->next)
5012 if (! attr->is_special && ! attr->is_const)
5013 write_attr_get (attr);
5016 /* Write out delay eligibility information, if DEFINE_DELAY present.
5017 (The function to compute the number of delay slots will be written
5021 write_eligible_delay ("delay");
5022 if (have_annul_true)
5023 write_eligible_delay ("annul_true");
5024 if (have_annul_false)
5025 write_eligible_delay ("annul_false");
5028 /* Write out constant delay slot info. */
5029 write_const_num_delay_slots ();
5031 write_length_unit_log ();
5034 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);